Page 1 of 1

applying commands to multiple objects

Posted: Wed Nov 13, 2013 12:10 pm
by gfquad
Hi, I'm new to Livecode and I'm trying to streamline some of my code. Is there a way to apply the same command to multiple objects? e.g. I want to set the height of several images to a variable which is defined earlier in the code. Currently my code is like this, where tNW is a variable I've previously set:

set the height of image "pic1" to tNW
set the height of image "pic2" to tNW
set the height of image "pic3" to tNW
set the height of image "pic4" to tNW

Surely there is way to write the code in one line to set the height of all the pics to tNW? I can't seem to find a way to do it of if it's even important?

Thanks

Re: applying commands to multiple objects

Posted: Wed Nov 13, 2013 12:19 pm
by jmburnod
Hi gfquad,
Welcome in this forum
Is there a way to apply the same command to multiple objects?
Yes :D

Code: Select all

on setHeightMyControls pHeight
   repeat with i = 1 to 4
      set the height of image ("pic" & i) to pHeight
   end repeat
end setHeightMyControls
Best regards
Jean-Marc

Re: applying commands to multiple objects

Posted: Wed Nov 13, 2013 12:27 pm
by gfquad
Thanks Jean-Marc,
Does that only work with numbered objects? What if the images were called "history", "setup", "info" etc?
Thanks
Geoff

Re: applying commands to multiple objects

Posted: Wed Nov 13, 2013 12:43 pm
by Dixie

Code: Select all

on mouseDown
   put "histoty,setup.info" into theList
   
      repeat with count = 1 to the number of items of theList
            set the height of image (item count of theList) to pHeight
      end repeat
end mouseDown

Re: applying commands to multiple objects

Posted: Wed Nov 13, 2013 12:46 pm
by jmburnod
LiveCode is very clever
You can use a list of controls as parameter.
I use "control" instead "image" to use the script for all control (fld, btn, image, graphic)

Code: Select all

on setHeightMyControlsByName pHeight,pListName -- pListname = a list of controls one per line.
   repeat with i = 1 to the num of lines of pListName
      set the height of control (line i of pListName) to pHeight
   end repeat
end setHeightMyControlsByName

 or faster

on setHeightMyControlsByName pHeight,pListName
   repeat for each line tControl in pListName
      set the height of control tControl to pHeight
   end repeat
end setHeightMyControlsByName

Re: applying commands to multiple objects

Posted: Wed Nov 13, 2013 2:08 pm
by gfquad
Thanks both, I've used a combination of your methods, it's been really helpful.

Re: applying commands to multiple objects

Posted: Wed Nov 13, 2013 4:24 pm
by dunbarx
What Dixie and Jean-Marc have shown you is that LC can construct object references by assembling or deconstructing strings in clever ways. These strings are then interpreted by the engine.

It is a thing of beauty. Do practice with these, stepping through the handlers they have offered, and watch how each new iteration through the loop creates just the reference you needed, so that the "set" command has just the information it needs to address each object in turn.

Craig Newman