Iterate through objects in a group

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
lexogram
Posts: 13
Joined: Fri Oct 17, 2014 6:58 pm

Iterate through objects in a group

Post by lexogram » Sun Oct 19, 2014 7:23 pm

Hi LiveCoders!

I have 6 graphic objects in a group (dots in a Braille pattern, to be precise).

I can get the number of objects in the group using

Code: Select all

put the number of graphics in group "Pattern"
.

What's the syntax I should use to iterate through each graphic so that I can set its properties?

I've tried...

Code: Select all

repeat for each graphic tGraphic in group "Pattern"
  put the number of tGraphic
end repeat
... but that gives me a script error.

An alternative solution might be to iterate through all the graphics on the card, and ask each if its parent is group "Pattern". What syntax could I use for that?

Thanks in advance,

James

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2718
Joined: Sat Dec 22, 2007 5:35 pm
Location: Genève
Contact:

Re: Iterate through objects in a group

Post by jmburnod » Sun Oct 19, 2014 7:35 pm

Hi lexogram,
I think "the num of controls is one way.
Something like that:

Code: Select all

on mouseUp
  put AllControlInGroup(grMyGroup,"graphic")
end mouseUp

function AllControlInGroup pGroup,pType
   put empty into rAllControlInCr
   repeat with i = 1 to the num of controls of group pGroup
      put i & "," & the  name of control i of group pGroup & "," & cr after rAllControlInCr
   end repeat
   delete char -1 of rAllControlInCd
   if pType <> empty then
      put "*" & pType & "*" into tFilter
      filter rAllControlInCr with tFilter
   end if
   return rAllControlInCr
end AllControlInGroup
Best regards
Jean-Marc
https://alternatic.ch

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9823
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Iterate through objects in a group

Post by FourthWorld » Sun Oct 19, 2014 8:01 pm

lexogram wrote:What's the syntax I should use to iterate through each graphic so that I can set its properties?

I've tried...

Code: Select all

repeat for each graphic tGraphic in group "Pattern"
  put the number of tGraphic
end repeat
... but that gives me a script error.
Jean-Marc's function is very complete, but the solution for your handler may possibly be simpler if you could note what the script error was that you encountered.

It may be that simply appending your statement to specify that you're only interested in controls within the group is all you need. Also, we don't yet have a "repeat for each" form for traversing controls (though it would indeed be nice and I believe is on the to-do list), so I'm guessing the error occurs in the repeat statement, yes?

If so, you can modify that to use "repeat with", and with that you automatically get the control number:

Code: Select all

repeat with i = 1 to the number of grcs in group "Pattern"
  put i
end repeat
If you need more specific information about each control you can get its long ID, remembering to specify that you're looking at objects only within the group:

Code: Select all

repeat with i = 1 to the number of grcs in group "Pattern"
  put the long id of grc i of grp "Pattern"
end repeat
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Klaus
Posts: 13824
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Iterate through objects in a group

Post by Klaus » Sun Oct 19, 2014 8:54 pm

It would be nice if "repeat for each..." would also work this way! 8)

lexogram
Posts: 13
Joined: Fri Oct 17, 2014 6:58 pm

Re: Iterate through objects in a group

Post by lexogram » Sun Oct 19, 2014 9:59 pm

Thanks to Jean-Marc for his quick and helpful reply. I have got the group script working now.

The script error I get with...

Code: Select all

repeat for each graphic tGraphic in group "Pattern"
  put the number of tGraphic
end repeat
... is:

Script compile error:
Error description: repeat: bad termination condition

It is good to know that a "repeat for each" form for traversing controls is on the To Do list.

Vanceone
Posts: 37
Joined: Sat Dec 15, 2012 7:27 pm

Re: Iterate through objects in a group

Post by Vanceone » Wed Jan 07, 2015 8:29 pm

I was going to post a new topic, but this seems related enough.

Is there a bug with the num of controls? I tried to

Code: Select all

repeat with i = 1 to the num of controls of me
 --stuff
end repeat
inside a group script. It works wonderfully.... except that it is always returning all the controls except the last one. It's like it's not finding that last control. Add a new control to the group, suddenly the previous one is found. So the num of controls is returning all but the last one.

I worked around it by doing a repeat for each line l in the ChildControlID's of me loop instead, but still. Am I imagining things or is that command broken? Livecode 7.0.1 on Mavericks, btw.

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3999
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: Iterate through objects in a group

Post by bn » Wed Jan 07, 2015 10:37 pm

Hi Vance,

I tried to replicate your setUp: a group with 4 controls and in the group script

Code: Select all

on doCount
   repeat with i = 1 to the number of controls of me
      add 1 to tCount
   end repeat
   put tCount into field "fREs"
end doCount
field "fRes" is outside of the group and the handler is triggered by a button from outside the group.

I always get the correct answer. Using 7.0.1 and Mavericks

Are what are you doing when you do -- stuff, are you deleting?

Kind regards
Bernd

Vanceone
Posts: 37
Joined: Sat Dec 15, 2012 7:27 pm

Re: Iterate through objects in a group

Post by Vanceone » Thu Jan 08, 2015 2:23 am

Thanks for testing, Bernd.

What I was trying to do was to set the rect of the controls of a background group; adjusting the placement in a resize handler that is called either directly or on preOpenCard. Basically, just laying out the controls. I'm beginning to wonder if I have some deeper issues, because I also cannot set any custom property of a group via the name of a group, only by its id. So for instance I can "Get the uType of group "backgroundControl" but "set the uType of grp "backgroundControl" to "barButton" " does not work either. (it's triggering a "can't find background" error) I have to set the uType of grp id 1725 instead. This is not restricted to just groups; sometimes just setting a custom property on anything is coughing up that same error; a restart seems to fix it.

So I'm not sure what is going on, actually.

This is my current code (that works):

Code: Select all

 local tControlsOfMe
   set the bottom of img "footer.png" of me to (the height of this stack )
   set the top of img "chrome_template_bg.png" of me to the top of this card
   set the top of img "header.png" of me to the top of this card
   put the childControlIDs of me into tControlsOfMe
   repeat for each line l in tControlsOfMe
      if the uType of control id l is "barButton" then
         set the top of control id l to (the top of img "footer.png" of me + 10)
      end if
      
   end repeat
My first try was a repeat with i=1 to the num of controls of me
and it worked great... except that the last control was never actually reached.

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3999
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: Iterate through objects in a group

Post by bn » Thu Jan 08, 2015 11:07 am

Hi Vance,
This is not restricted to just groups; sometimes just setting a custom property on anything is coughing up that same error; a restart seems to fix it.
that sounds funny.
When that happens I would copy e.g. just the group to a new clean stack and try the same on this card. Maybe it is a duplicate of a hidden control that lurks somewhere, maybe a missing "of me" in case of groups when addressing them as "control x".
If the same behavior is happening in the new stack/card and you find a recipe to consistently reproduce I would report is as a bug.

I have seen a problem with not resolving a named reference in groups correctly (Bug 10644) but that was reproducible every time and has been fixed in 6.0. And it was a fairly specific condition.
But then there is Bug 14214 where mispelling causes a problem regarding reference and can't find background.

Sorry, I have no idea what is going on.

Kind regards
Bernd

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”