Passing the name of an image object to a function

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
steve10
Posts: 85
Joined: Fri May 13, 2011 9:38 am

Passing the name of an image object to a function

Post by steve10 » Mon Jun 27, 2011 12:03 am

I have a button that On Mousedown is supposed to start an action on an image. However, the target image changes so the function is part of the card. Syntax like myFunction(image "myImage") isn't working. How do I accomplish my goal. One thing to note - I'm not trying to return any values, so is there an alternative to using a function?

Steve

SparkOut
Posts: 2943
Joined: Sun Sep 23, 2007 4:58 pm

Re: Passing the name of an image object to a function

Post by SparkOut » Mon Jun 27, 2011 12:21 am

You can use "the target" to reference the origin of the message, in its various flavours, such as "the short name of the target" or "the long id of the target".

You could also put the handler script into an invisible button (say) and use the long id of that to reference the "behavior" of each image. When you use behaviors (sic... can we have a synonym that is spelt correctly please? :wink: ) each object that uses that behavior script has its context virtualised so that "me" refers to the target object in question.

And look up in the documentation about the distinction between a "command" and a "function".

Hope that helps
SparkOut

steve10
Posts: 85
Joined: Fri May 13, 2011 9:38 am

Re: Passing the name of an image object to a function

Post by steve10 » Mon Jun 27, 2011 1:18 am

Thanks SparkOut. I had looked at the Dictionary for command and that didn't help. Check the documentation? Me? :) Now functions and commands are making more sense.
SparkOut wrote:You can use "the target" to reference the origin of the message, in its various flavours, such as "the short name of the target" or "the long id of the target".
By that I thought you meant put it in a variable, but that didn't work completely. Here's the code from the button:

Code: Select all

on mouseDown
   put image "outer" into obStart
   put image "outer2" into obEnd
   ResetToFirst obStart, obEnd
end mouseDown
So far it seems okay, but I suspect the put commands are not what you meant by "...use "the target" to reference the origin of the message...". It called ResetToFirst, but didn't get very far. Here's the code:

Code: Select all

command ResetToFirst obStart, obEnd
   set the height of obStart to the formattedHeight of obStart
   set the width of obStart to the formattedWidth of obStart
   set the height of obEnd to the formattedHeight of obEnd
   set the width of obEnd to the formattedWidth of obEnd   
end ResetToFirst
This was the error: execution error at line 2 (Chunk: error in object expression) near "‰PNG", char 53

Steve

SparkOut
Posts: 2943
Joined: Sun Sep 23, 2007 4:58 pm

Re: Passing the name of an image object to a function

Post by SparkOut » Mon Jun 27, 2011 9:08 am

You need something more like

Code: Select all

on mouseDown
  -- get a reference to the target object, which will be the object which generated the message caught by this handler
  -- so if this handler is in the card script the target could be any object on the card...
  -- which means that you should also do some validation at the very least, if you must use "mouseDown" in a card script
  put the long name of the target into obStart
  -- check the target is actually an image
  if word 1 of obStart is "image" then
    -- depending on whether this is fixed or dependent on the target we need to make a reference
    -- to the ending object
    -- assuming we append "2" to the name of the start object we can copy it and ...
    put obStart into obEnd
    -- then stick the digit "2" in the right place
    put  "2" before char -1  of word 2 of obEnd
    -- obviously you will have to have your own method for determining the names of the objects in question
    resetToFirst obStart, obEnd
  else
    pass mouseDown
  end if
end mouseDown
Your resetToFirst command looks alright - it just needs to be given strings that identify the object in question to the engine, which the revisions above should hopefully do.

I'm not exactly clear what you're doing, but you may find a behavior a suitable alternative. It would certainly be easier to differentiate between the objects that are supposed to have a handler respond to the mouseDown and those that aren't.

steve10
Posts: 85
Joined: Fri May 13, 2011 9:38 am

Re: Passing the name of an image object to a function

Post by steve10 » Mon Jun 27, 2011 5:14 pm

Thanks again SparkOut.

To give context, this is just some test code so I can understand how to do a few things in my first project. That project resizes and changes the appearance of several images at different times. After finishing, it returns the images to their original size, leaving the other changes in place. In the real project, the command will be called in code, but for these tests I'm using buttons to start the changes and then revert.

I've successfully created the code for the image manipulations as it applies to a specified image, such as: image "myimage". However, I need to recycle the code for all of the images, so I need to put image "myimage" in a variable.
SparkOut wrote:get a reference to the target object
I suspect this is where I'm going wrong. What do you mean? Perhaps you could provide an example?
SparkOut wrote:put the long name of the target into obStart
That's what I thought I was doing with this code: put image "outer" into obStart
SparkOut wrote:check the target is actually an image
I'm not sure how to check, but it is an imported png. I suspect that's why part of the error message is "...near "‰PNG"...". Of course, checking in the real script is probably a good idea.

I'm not sure what you mean by the rest of it, but it may be worth pointing out that I didn't really need to image "outer2" and obEnd in this example. I violated the KISS principle! Outer and outer2 are different images and the use of "2' holds no significance. I'm not trying to put obStart into obEnd and copying doesn't seem to apply.

Steve

Klaus
Posts: 14182
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Passing the name of an image object to a function

Post by Klaus » Mon Jun 27, 2011 5:31 pm

Hi Steve,

you need to supply the "long name" or the "long ID" to your function/handler!

This should work:

Code: Select all

on mouseDown
   put the long ID of image "outer" into obStart
   put the long ID of image "outer2" into obEnd
   ## or "the long name of image "xxx"

   ResetToFirst obStart, obEnd
end mouseDown

Best

Klaus

steve10
Posts: 85
Joined: Fri May 13, 2011 9:38 am

Re: Passing the name of an image object to a function

Post by steve10 » Mon Jun 27, 2011 5:48 pm

Thanks very much Klaus. That did the trick. :) I guess I better study up on long names and long IDs.

Steve

SparkOut
Posts: 2943
Joined: Sun Sep 23, 2007 4:58 pm

Re: Passing the name of an image object to a function

Post by SparkOut » Tue Jun 28, 2011 1:36 am

steve10 wrote:
SparkOut wrote:get a reference to the target object
I suspect this is where I'm going wrong. What do you mean? Perhaps you could provide an example?
I thought that was an example ;)
The lines you are quoting are comments in the handler that precede the actual codeline, to describe what we were doing. So the "get a reference to the target object" comment referred to the code line "put the long name of the target into obStart", which is actual syntax.
steve10 wrote:
SparkOut wrote:put the long name of the target into obStart
That's what I thought I was doing with this code: put image "outer" into obStart
"put the long name of the target into obStart" was the actual syntax to do this. I was under the impression that you had a number of different images, each with their own name and id, and one group or card script with the mouseDown handler that would be caught and is intended to deal with the different images the same way, but with a reference to whichever on actually had the mouseDown action initiated. So say it was image "outer" that had the mouseDown event happen, and not the image "canoe" or image "balloon" that cold equally have been handled. When the mouseDown message reaches the card script handler we can find which of the images it was which originated the message by use of the keyword "target". So we were able to "put the long name of the target into obStart" which means that in this case the long name of image "outer" of (the current card...) was stored.
steve10 wrote:
SparkOut wrote:check the target is actually an image
I'm not sure how to check, but it is an imported png. I suspect that's why part of the error message is "...near "‰PNG"...". Of course, checking in the real script is probably a good idea.
"-- check the target is actually an image" was a comment to describe this codeline: if word 1 of obStart is "image" then...
Having retrieved the long name of the target, obStart will look something like:

image "outer" of card 1002 of stack "path and name of/myTestStack.livecode"

so if the first word (word 1) of that long name (held in obStart) is "image" then we know it is an image that generated the message. I put that test in because I didn't think you would want the mouseDown handler to trigger off the resetToFirst handler for any field or other object on the card, or the card itself.
steve10 wrote:I'm not sure what you mean by the rest of it, but it may be worth pointing out that I didn't really need to image "outer2" and obEnd in this example. I violated the KISS principle! Outer and outer2 are different images and the use of "2' holds no significance. I'm not trying to put obStart into obEnd and copying doesn't seem to apply.

Steve
Ah, well the rest of it isn't likely to be too relevant (either). I was trying to help you achieve being able to find a way programmatically to reference which image was the source of a message. I believe I misunderstood you and you were simply trying to get the "reference" to the object (image "outer") to be passed to the resetToFirst handler. Which, as you have now seen from Klaus' post, is quite simple to do once you have been shown the syntax you need.

Post Reply