How to array objects? - Solved

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
DR White
Posts: 470
Joined: Fri Aug 23, 2013 12:29 pm
Location: Virginia, USA

How to array objects? - Solved

Post by DR White » Wed Jun 05, 2019 1:01 pm

global Resistor_H
on mouseDown pButtonNumber
put 1 into x
set loc of grp Resistor_H[x] to 200,200
end mouseDown

The user needs to be able to add groups that are composed of unique names for the items within the group, so that script can control each new user added groups properly.

Thanks,

David
Last edited by DR White on Thu Jun 06, 2019 10:47 am, edited 2 times in total.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 6028
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: How to dimension objects?

Post by dunbarx » Wed Jun 05, 2019 4:04 pm

Hi.
The user needs to be able to add groups that are composed of unique names for the items within the group
The user needs to be able to add new groups of controls, or new controls within a group? Are you saying that the names of the child controls somehow relate to the name of the group? Let us know. Or me, at least.

In any case, why do you use an array? There is no reason not to, particularly, but you do not need to. For example, if you had two groups, "yourGroup1" and "yourGroup2", then in a button script somewhere:

Code: Select all

on mouseUp
   repeat with y = 1 to 2
      set the loc of grp ("yourGroup" & y) to "100," & random(300)
   end repeat
end mouseUp
I suspect I am missing your point, though, since I do not understand the title of the thread.

Craig

DR White
Posts: 470
Joined: Fri Aug 23, 2013 12:29 pm
Location: Virginia, USA

Re: How to dimension objects?

Post by DR White » Wed Jun 05, 2019 5:27 pm

Craig,

No, you did not miss my point.

I am using the method that you had in your sample code (which I always appreciate "sample code").

I did not know if there was an easier way using a group with "[x]" in a dimension format.

I will continue to use the same format that you showed in your code.

Thanks,

David

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 6028
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: How to dimension objects?

Post by dunbarx » Wed Jun 05, 2019 7:46 pm

OK.

Still not sure what "dimension" means, but maybe you do, and that is all that matters.

Craig

DR White
Posts: 470
Joined: Fri Aug 23, 2013 12:29 pm
Location: Virginia, USA

Re: How to dimension objects?

Post by DR White » Wed Jun 05, 2019 10:22 pm

When I use the word "dimension", a better word might be array.

example:

David [2,2}
David [1,1} = Bill
David [1,2} = John
David [2,1} = Sally
David [2,2} = Susan

So my real question was "Can I reference groups in an a array format?"
ex. group David[1,2]

As you said, its not a big deal.

Thanks

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 6028
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: How to dimension objects?

Post by dunbarx » Wed Jun 05, 2019 11:11 pm

Well, sure. You can use an array variable instead of an "ordinary" variable. You already have your named groups. You just change the syntax slightly:

Code: Select all

on mouseUp
   repeat with y = 1 to 2
      put y into yourGroup[y]
      set the loc of grp yourGroup[y] to "100," & random(300)
   end repeat
end mouseUp
In the "ordinary" method in the post above, LC builds the name reference on the fly. In the "array" method, this does not work:

Code: Select all

on mouseUp
   repeat with y = 1 to 2
      set the loc of grp yourGroup[y] to "100," & random(300)
   end repeat
end mouseUp
Because LC resolves the term "yourGroup[y]" to "2", and you are then trying to work with "group 2" instead of group "yourGroup2".

You should play around with this sort of thing, though.

Craig
Last edited by dunbarx on Thu Jun 06, 2019 3:57 pm, edited 3 times in total.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 6028
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: How to dimension objects?

Post by dunbarx » Thu Jun 06, 2019 12:14 am

Just so you know, you would have to:

Code: Select all

on mouseUp
   repeat with y = 1 to 2
      set the loc of grp ("yourGroup" & yourGroup[y]) to "100," & random(300)
   end repeat
end mouseUp
Array variable are powerful and flexible. But they are not quite as accessible and direct as ordinary variable, and, well, not as simple.

Craig

DR White
Posts: 470
Joined: Fri Aug 23, 2013 12:29 pm
Location: Virginia, USA

Re: How to dimension objects?

Post by DR White » Thu Jun 06, 2019 10:46 am

Thanks,

Craig

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”