Working with Groups in script

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: Klaus, FourthWorld, heatherlaine, kevinmiller

Post Reply
DaltonCalford
Posts: 22
Joined: Fri May 06, 2011 7:33 pm

Working with Groups in script

Post by DaltonCalford » Sun Jul 17, 2011 5:15 pm

How do I address controls within the current group vs those in other groups?

I have a set of simple controls that I have a mouseup script defined. I then copied and pasted the group onto the same card. When I click on the copied group, the script changes the properties of the original group instead of the current group.
With other languages, you can address such things using parent.control type syntax but I can not find any code samples/documentation that addresses this.

Thanks

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

Re: Working with Groups in script

Post by Klaus » Sun Jul 17, 2011 5:43 pm

Hi Dalton,

jut add a "descriptor":
...
put fld "field 666" OF GRP "your group here" into tText
...


Best

Klaus

DaltonCalford
Posts: 22
Joined: Fri May 06, 2011 7:33 pm

Re: Working with Groups in script

Post by DaltonCalford » Sun Jul 17, 2011 8:10 pm

Hi Klaus,

I will have a few hundred copies of each group and I want to use the same script without having to modify each one - I tried looking up the syntax of OF GRP in the livecode dictionary but I could not find any reference to it.

I am looking to do something like this - can you show me a link to the proper syntax or working sample?

put fld "field 666" OF GRP "SELF" into tText

In this way, I can use the same block of code for all the copies of all the groups without having to make any group specific changes.

Thanks

Dalton
Klaus wrote:Hi Dalton,

jut add a "descriptor":
...
put fld "field 666" OF GRP "your group here" into tText
...


Best

Klaus

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

Re: Working with Groups in script

Post by Klaus » Sun Jul 17, 2011 8:29 pm

Hi Dalton,

sorry, I always use abbreviations, so this might have created a little confusion :D

I mean of course to add the name (or ID) of the group where the conmtrol resides:
...
field xxx of group "name of your group here"
## field xxx of grp ID ID_of_your_group_here
...
When you have a lot of groups with he same name, hmm... this would be rather sub-optimal 8)
What are you trying to do?
Maybe adding a behaviour to your groups might be a better solution?

Ah, and welcome to the forum! :)

Best

Klaus

Edit:
Please check these stacks here to get the basics of LiveCode:
http://www.runrev.com/developers/lesson ... nferences/

DaltonCalford
Posts: 22
Joined: Fri May 06, 2011 7:33 pm

Re: Working with Groups in script

Post by DaltonCalford » Sun Jul 17, 2011 8:51 pm

Hi Klaus,

I have a group of 5 controls in a single group. I click on the group and depending on the current settings of the current group, the settings will change and perhaps even change the settings on other groups.
That group is repeated hundreds of time on screen. I can either individually create the controls, giving individual names to each and coding individual scripts for each, or, I can write a single group, code to reference the current group and just copy the group as many times as I need it.

Unfortunately, I can not see how I reference the current groups personal controls via a script without directly naming the group. I do not want to have to give each copied group an individual name, as that means I would have to go in and edit each groups individual script.

I am trying to reuse the code in a very generic way. If this was java, or Delphi I would know how to do it thanks to the "parent" or "Self" references ie, to reference a text label that is sitting on a panel (similar to a group in livecode) it would be self.mylabel :='value';

So, I need to be able to use references where I do not have to specify the group name, but have it reference the group that fired the mouseup event.

Thanks

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

Re: Working with Groups in script

Post by SparkOut » Sun Jul 17, 2011 9:46 pm

I confess I am not absolutely clear where from where you are going to be referencing things. If you are going to set the text of a field in a given group, you will have to know some means of resolving the name or id of the field within that group, and by definition, the name or id of the containing group. So if the point from which you reference the field is outside the group, then you would have to reseolved that already. If you are referring to "self" as I believe you are, then "me" will probably be the object reference you want. You can use "the target" to find the origin of a message, "me" to find the object becoming the context within which a handler is executed and "the owner of me" to find out about its parentage. (You can extend that with "the long id of the owner of me" or "the short name of the owner of me" etc etc).
Also, as Klaus mentioned, this situation lends itself to the use of behaviours. If you put the handlers into an object that can be referenced from the original group, and set the behaviour of that group to the long id of the object containing your handlers, then any copy of the original group should have the same behaviour. All references to "me" and variables etc are virtualised within the behaviours so for each instance of the behaviour the resolution of "me" is the correct object, and variables do not interfere with one anothers' instances.
Last edited by SparkOut on Sun Jul 17, 2011 9:59 pm, edited 1 time in total.

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

Re: Working with Groups in script

Post by Klaus » Sun Jul 17, 2011 9:52 pm

Hi Dalton,

I am still not sure what exactly you want to achieve, but maybe you could use just on "mouseup" handler
in the card script and use "the target" and "the owner".

Card script:

Code: Select all

on mouseup
  put the owner of the target into tGroup
end mouseup
Now when you click a button or field or whatever that is in a group this will put somethign like
-> group "name of group here"
into the variable tGroup

Or script:
...
put the short name of the owner of the target
put the ID of the owner of the target...
put the long ID of the owner of the target...
...
To get only the name, or ID or LONG ID.

Is that what you are looking for?


Best

Klaus

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

Re: Working with Groups in script

Post by SparkOut » Sun Jul 17, 2011 10:00 pm

Hey Klausimausi, sorry, I should refresh before reposting my edits!

DaltonCalford
Posts: 22
Joined: Fri May 06, 2011 7:33 pm

Re: Working with Groups in script

Post by DaltonCalford » Mon Jul 18, 2011 2:24 am

Hey Guys,

Thanks for the hint to "me".

Here is a simple example of what I am trying to do.

I have a group. It has the name "my group".
The group contains a polygon named "polygon" and a label called "count"

There is a script on the group that responds to the mouseUP event.

Everytime the group is clicked (mouseUp), it increments the text field by 1 unless the text field has a value of 3 where it is reduced down to 1 again.

pseudo code
.....
put '2' into field "count"
.....

Now, I am going to have a great deal of these groups - hundreds on a single card in fact. They all do the same job, so they should share the same script instead of having to give each group their own script.

so the pseudo code would be something like this - I just need the correct syntax.

.....
put '2' into field "count" of group "me"
.....

In this way, I do not care about the groupid, or unique names etc - the script is changing the value of a member of the group which is responding to the mouseup event.

Right now, when I run it, the script changes the label on the original group and not on the group that is being fired. I am getting a compile error with the above pseudo code so I am just trying to find a working example or link to documentation that shows me how to properly tell the parser what I want to do.

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

Re: Working with Groups in script

Post by SparkOut » Mon Jul 18, 2011 8:50 am

Here is a little sampler that you should be able to use to figure out more.
It uses a behavior script so that you can apply the same "behavior" to many controls and each set will resolve itself independently of the others, yet you will only have to maintain one piece of code. If you click on the rectangles or labels in each group you will see that the "target" resolves itself in each case in the context of "me", and "me" resolves itself to the current group.

[ignore this British rant](grrrr, can we have a synonym spelt "behaviour" please?) [/ignore this British rant]
Attachments
Test Group references.zip
(1.63 KiB) Downloaded 112 times

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”