dynamically addressing objects

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
joel.epsteinBUS31vi
Posts: 135
Joined: Thu Sep 13, 2012 10:25 pm

dynamically addressing objects

Post by joel.epsteinBUS31vi » Wed Oct 17, 2012 3:39 am

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

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: dynamically addressing objects

Post by sturgis » Wed Oct 17, 2012 3:49 am

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

joel.epsteinBUS31vi
Posts: 135
Joined: Thu Sep 13, 2012 10:25 pm

Re: dynamically addressing objects

Post by joel.epsteinBUS31vi » Wed Oct 17, 2012 4:18 am

Fantastic! I didn't know it would be so easy. Thanks for your speedy reply.
Peace.
Joel

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

Re: dynamically addressing objects

Post by Klaus » Wed Oct 17, 2012 10:41 am

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

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: dynamically addressing objects

Post by mwieder » Wed Oct 17, 2012 5:30 pm

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)

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: dynamically addressing objects

Post by sturgis » Wed Oct 17, 2012 5:32 pm

I was having a moment when I wrote the last sentence apparently. *smacks self on the noggin* i knew that!

Post Reply