Dynamic variable creation

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
Jordy
Posts: 32
Joined: Sat Feb 27, 2016 3:59 am

Dynamic variable creation

Post by Jordy » Mon Apr 11, 2016 11:52 am

Just a general question is LiveCode capable of creating global variables on the fly.

Simple example
A game you have turns, as players take turns you add 1 to a global gTurnNumber

Could you get gTurnNumber and get LC to create a new global while program running that gTurnWinner1, gTurnWinner2,
gTurnWinner3 that are created as game progresses?

Klaus
Posts: 14208
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Dynamic variable creation

Post by Klaus » Mon Apr 11, 2016 12:00 pm

Hi Jordy,

this is possible, but not really straightforward and probably not worth the effort!
Use a (global) ARRAY instead:
...
## Then you can do something like this:
add 1 to myGlobalGameArray["player1"]["turn"]
...
Get the picture?


Best

Klaus

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

Re: Dynamic variable creation

Post by dunbarx » Mon Apr 11, 2016 3:47 pm

I have so much fun.

What Klaus means is that you would have to take a few minutes and examine the following handler in a button:

Code: Select all

on mouseUp
   repeat with y = 1 to 3
      do "put" && y && "into newGlobal" & y
      put newGlobal & y & comma after header
   end repeat
 
   put "global" && header into globalHeader
   
   set the newGlobalScript of  me to globalHeader & return & return & the script of me
   answer the newGlobalScript of  me 
end mouseUp
LC can, er, "do" anything, but what I just did is old-fashioned.

Craig Newman

Post Reply