Variable in an identifier??

About advanced scenes, and the Thyme scripting language used in Algodoo.

Variable in an identifier??

Postby Antotabo » Tue Apr 20, 2010 6:12 am

How can I declare a new variable that its identifier contains a variable??

I want something like this to create n number of array like scene.my.brain(n).

Code: Select all
scene.my.count1 >= 20 ? {} :
{
scene.my.brain(scene.my.count):=
[
scene.my.random,
scene.my.random,
scene.my.random,
.....
.....
scene.my.random,
scene.my.random,
scene.my.random,
0
]
;scene.my.count1 = scene.my.count1 + 1
}
Antotabo
 
Posts: 54
Joined: Tue Sep 01, 2009 6:27 am

Re: Variable in an identifier??

Postby Mr_Stabby » Tue Apr 20, 2010 10:20 am

in thyme you have to give arrays the values of all array elements at the same time (its stupid like that) but there are ways around it - eval command parses text into code so you could either do something like


scene.my.codebuffer="scene.my.brain:=[
for (20, (n)=>{
scene.my.codebuffer=scene.my.codebuffer+scene.my.random+", "
<swap random here>
})
scene.my.codebuffer=scene.my.codebuffer+"]"
eval(scene.my.codebuffer)


or a much easyer solution would be just to make a buttload of dynamically numerated variables, that way you can also easily add or change them later
for (20, (n)=>{
eval("scene.my.brain"+n+":="+scene.my.random")
<swap random here>
})

PS! untested code, might detonate your computer
Mr_Stabby
 
Posts: 155
Joined: Wed Dec 16, 2009 12:16 am

Re: Variable in an identifier??

Postby Antotabo » Wed Apr 21, 2010 3:58 am

Yeah, I know that you CAN'T write only one data at a time like this :
Code: Select all
scene.my.brain1(0)=scene.my.random

But what I mean is that I want to create several arrays like that:
Code: Select all
scene.my.brain1, scene.my.brain2, scene.my.brain3...

The elements in the arrays are created in a single command like my original post and I know this works:
Code: Select all
(event):...
{scene.my.brain1:=
[
scene.my.random,
scene.my.random,
.....
.....
scene.my.random,
scene.my.random,
0
]}


Then elements of those arrays are loaded on normal variables to be read and edited, then stored back all at one time into the proper array. Like this:
Load code:
Code: Select all
scene.my.a1=scene.my.brain3(0); scene.my.a2=scene.my.brain3(1)...scene.my.d4=scene.my.brain3(7)....

Write code:
Code: Select all
scene.my.brain3=[scene.my.a1,scene.my.a2,...scene.my.d4...]


But what I mean is how can I create/read/write on those arrays using a variable to define which array I want to read/write/create. In other words, what can replace the number contain in this : scene.my.brain1
by this: scene.my.count?


Thanks!
Antotabo
 
Posts: 54
Joined: Tue Sep 01, 2009 6:27 am

Re: Variable in an identifier??

Postby Mr_Stabby » Wed Apr 21, 2010 7:33 am

i still dont get what ur trying to do but i think whatever it is can be solved by jagged arrays

array = [[a, b], [c, d]]
as result
array(0) = [a, b]
array(1) = [c, d]
array(0)(0) = a
array(0)(1) = b
array(1)(0) = c
array(1)(1) = d

you can make it as big and/or wide as you like

or you could make fake pointer array to arrays like this
pointerarray=["arrayname1", "arrayname2"]
value=eval(pointerarray(n)+"("+n2+")")
or make it a pointer array to pointer arrays that point to regular variables which is even more confusing but actually much more efficent and flexible then jagged arrays while performing the same task
Mr_Stabby
 
Posts: 155
Joined: Wed Dec 16, 2009 12:16 am

Re: Variable in an identifier??

Postby Antotabo » Tue Jun 29, 2010 3:13 am

Sorry for the bump, but I just want to know a simple thing because I don't understand MR_Stabby first response.

How can I create an array in which the name of it contains a number of an existing variable.

Let me explain:

I have a variable that increment by one each time the code is run:
scene.my.count

I want to create a new array each time the code run:
scene.my.array**the variable contained inscene.my.count should be here**:= [0,0,0...]

How can I do that? Is it just a matter of using the right () [] " " at the right place?? tell me please!

Thanks to Mr_graddy for your help.
Antotabo
 
Posts: 54
Joined: Tue Sep 01, 2009 6:27 am

Re: Variable in an identifier??

Postby Antotabo » Tue Jun 29, 2010 6:15 am

OK sorry, I found It!
Code: Select all
scene.my.count1 <= 20 ? {         eval("scene.my.brain" + scene.my.count1 + ":=[scene.my.random, scene.my.random,...... scene.my.random,0]");         scene.my.count1 = scene.my.count1 + 1     }
Antotabo
 
Posts: 54
Joined: Tue Sep 01, 2009 6:27 am


Return to Thyme scripting

Who is online

Users browsing this forum: No registered users and 5 guests