Page 1 of 1

dynamically addressing objects

Posted: Wed Oct 17, 2012 3:39 am
by joel.epsteinBUS31vi
Hi all -

I'm really enjoying my adventure learning LiveCode. Making good progress, but still have some lingering questions. Here's a rather basic one that I'd appreciate some help with:

I have created the following handler:

Code: Select all

on showImage which
   
   put gHowMany + 1 into gHowMany
   
    if gHowMany = 1 then
      set the filename of image "image1" of card "Main" to the filename of which
      set the visible of image "image1" of card "Main" to true
    else if gHowMany = 2 then
      set the filename of image "image2" of card "Main" to the filename of which
      set the visible of image "image2" of card "Main" to true
    else
      set the filename of image "image3" of card "Main" to the filename of which
      set the visible of image "image4" of card "Main" to true
    end if
	
end showImage
This works just fine. No problems. But it seems like a rather inefficient method of coding.
What I'm wondering, is if there's a way to dynamically create most of this code.
After all, most of it is just repeated, with the only difference being the appending of the number (i.e., "imageX")
And I have the value of that number stored in the global variable.

So... My question, therefore, is how to dynamically create the reference to the image?

Thanks so much...

Joel

Re: dynamically addressing objects

Posted: Wed Oct 17, 2012 3:49 am
by sturgis
perhaps something like this is an example similar to what you're looking for.

Code: Select all

on mouseUp
   put 1 into tNum -- just put this code into a button and only have 1 field named image1 but it should be a decent example
   answer file "pick an image" --lets me pick an image, places it into the it variable

## in this case since the engine knows you're setting a filename all you have to do is pass it the filename string like so.
## and as you see you can use parens to force eval of the image name string concatenation. 
   set the filename of image ("image" & tnum) to it -- could be more explicit like you were and include the card name etc.

end mouseUp
Not positive the parens are required for this to work but its a good habit in my opinion.
joel.epsteinBUS31vi wrote:Hi all -

I'm really enjoying my adventure learning LiveCode. Making good progress, but still have some lingering questions. Here's a rather basic one that I'd appreciate some help with:

I have created the following handler:

Code: Select all

on showImage which
   
   put gHowMany + 1 into gHowMany
   
    if gHowMany = 1 then
      set the filename of image "image1" of card "Main" to the filename of which
      set the visible of image "image1" of card "Main" to true
    else if gHowMany = 2 then
      set the filename of image "image2" of card "Main" to the filename of which
      set the visible of image "image2" of card "Main" to true
    else
      set the filename of image "image3" of card "Main" to the filename of which
      set the visible of image "image4" of card "Main" to true
    end if
	
end showImage
This works just fine. No problems. But it seems like a rather inefficient method of coding.
What I'm wondering, is if there's a way to dynamically create most of this code.
After all, most of it is just repeated, with the only difference being the appending of the number (i.e., "imageX")
And I have the value of that number stored in the global variable.

So... My question, therefore, is how to dynamically create the reference to the image?

Thanks so much...

Joel

Re: dynamically addressing objects

Posted: Wed Oct 17, 2012 4:18 am
by joel.epsteinBUS31vi
Fantastic! I didn't know it would be so easy. Thanks for your speedy reply.
Peace.
Joel

Re: dynamically addressing objects

Posted: Wed Oct 17, 2012 10:41 am
by Klaus
Hi friends,
Not positive the parens are required for this to work but its a good habit in my opinion.
In case like these (concatenating names of objects) parens are definitivley required!


Best

Klaus

Re: dynamically addressing objects

Posted: Wed Oct 17, 2012 5:30 pm
by mwieder
In case like these (concatenating names of objects) parens are definitivley required!
Indeed. Otherwise the compiler has to try to guess whether you meant

Code: Select all

(the filename of image "image") & tnum
or

Code: Select all

the filename of image ("image" & tnum)

Re: dynamically addressing objects

Posted: Wed Oct 17, 2012 5:32 pm
by sturgis
I was having a moment when I wrote the last sentence apparently. *smacks self on the noggin* i knew that!