Page 1 of 1

Array index for object?

Posted: Fri Mar 13, 2015 8:10 am
by JumpingFrog
Hi, I used to write HyperCard stacks, and now enjoy learning LiveCode.

Is there any way to use some kind of array description in order to refer to objects?

Such as field CCard instead of

field "CCard1"
field "CCard2"
field "CCard3"
field "CCard4"
field "CCard5"
?

I like to create many objects on a card and change properties of any, not by their name, but by their index.

Re: Array index for object?

Posted: Fri Mar 13, 2015 11:50 am
by FredBeck
You just need to store the id of the fields in an array when you create it.
Usually I store this array in a custom property of the card or group that contains the objects but you can put it into a global variable if you prefer.

in the card script :

Code: Select all

global gCCard 

command createCCardField
   local tIndex
   put the number of lines in the keys of gCCard + 1 into tIndex
   create field "CCard" & tIndex
   put the abbrev id of it into gCCard [tIndex]
end createCCardField

command deleteCCardField pIndex
   if there is a gCCard [pIndex] then
      delete gCCard [pIndex] -- delete the field
      delete global gCCard [pIndex] -- delete the array key
   end if
end deleteCCardField
then a couple buttons,
create fields :

Code: Select all

global gCCard

on mouseUp
   repeat with x = 1 to 5
      createCCardField
   end repeat
   --breakpoint
   placeFields
end mouseUp

command placeFields
   set the top of gCCard [1] to the top of this card
   set the left of gCCard [1] to (the width of this card - the width of gCCard [1]) / 2
   repeat with x = 2 to the number of lines in the keys of gCCard
      set the top of gCCard [x] to the bottom of gCCard [x - 1] + 5
      set the left of gCCard [x] to the left of gCCard [x - 1]
   end repeat
end placeFields
and delete fields :

Code: Select all

global gCCard

on mouseUp
   repeat with tIndex = 1 to the number of lines in the keys of gCCard
      deleteCCardField tIndex
   end repeat
end mouseUp
Cheers,
Fred

Re: Array index for object?

Posted: Fri Mar 13, 2015 1:56 pm
by dunbarx
Hi.

It is likely that the ID's of controls, as they are created, deleted or relayered will not be in any sequence that you can rely on. I would recommend that you use any other settable property instead, like the "altID" or "name", since you have control over that.

As for storing that information in an array, that is simple, and if you use a numerical system, like "yourField1", yourField2", etc., you can create an array where the last char of each name becomes a numerical key for the array. If that is not convenient, I like to use the altID. But do consider a custom property to store the data, whether in array format or simply in the clear in the prop itself. These survive sessions.

Craig Newman

Re: Array index for object?

Posted: Sat Mar 14, 2015 2:49 am
by JumpingFrog
Thank you very much Fred, and Craig for your kind and through advices.

I will try using youe codes in my program over this weekend!

Again, Thank you!!

Re: Array index for object?

Posted: Tue Mar 17, 2015 6:49 am
by JumpingFrog
Thanks. It worked!!