Array index for object?

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
JumpingFrog
Posts: 3
Joined: Fri Mar 13, 2015 7:59 am

Array index for object?

Post by JumpingFrog » Fri Mar 13, 2015 8:10 am

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.

FredBeck
Posts: 77
Joined: Fri Nov 01, 2013 3:07 pm
Contact:

Re: Array index for object?

Post by FredBeck » Fri Mar 13, 2015 11:50 am

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10331
Joined: Wed May 06, 2009 2:28 pm

Re: Array index for object?

Post by dunbarx » Fri Mar 13, 2015 1:56 pm

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

JumpingFrog
Posts: 3
Joined: Fri Mar 13, 2015 7:59 am

Re: Array index for object?

Post by JumpingFrog » Sat Mar 14, 2015 2:49 am

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!!

JumpingFrog
Posts: 3
Joined: Fri Mar 13, 2015 7:59 am

Re: Array index for object?

Post by JumpingFrog » Tue Mar 17, 2015 6:49 am

Thanks. It worked!!

Post Reply