Arrays and Spevial Variables

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

Arrays and Spevial Variables

Postby Versieon » Sat Jan 23, 2010 12:12 am

I'v got two questions here that i've gotten while working on a project that deals with many different variables and some large arrays.

First with the array.

I've got a fifteen part array, with each value being another four part array
Code: Select all
[ [1,0,0,1], [1,0,0,1], ...]

this all works fine.

However, I want to change one of the four part arrays, and so I put in this code

Code: Select all
scene.my.array(5) = [0,0,0,1]


This will not accept and a "bad placement of =" comes up in the console.
is there a way to edit the values, i can read them fine, but cannot edit.

---------------------------------------------------------------------------------------------------------------------

The second part is a little more complicated.

I was wondering if there is a way to switch variables depending on a value, but with hundreds of possabilitys.

for example,
scene.my.start = 1
scene.my.start1 = 3

Code: Select all
e.this.var = "scene.my." + scene.my.start + "(" + scene.my.start1 + ")"


e.this.var would then come out to scene.my.1(3) when the part are added.
this effect can be achieved in the text area, but it will only display "scene.my.1(3)", it there a way for it to show the value of scene.my.1(3)?

I read up on eval but couldn't figure out how it would work/
User avatar
Versieon
 
Posts: 375
Joined: Tue Sep 01, 2009 4:45 pm

Re: Arrays and Spevial Variables

Postby KarateBrot » Sat Jan 23, 2010 12:18 am

Instead of
Code: Select all
scene.my.array(5) = [0,0,0,1]

You have to type
Code: Select all
scene.my.array = [scene.my.array(0),scene.my.array(1),scene.my.array(2),scene.my.array(3),scene.my.array(4),[0,0,0,1],scene.my.array(6) ...]


The solution for the second problem will follow. I have to think about it because I don't exaclty know what you want to do.
Image
User avatar
KarateBrot
 
Posts: 825
Joined: Mon Aug 31, 2009 7:32 pm
Location: Germany

Re: Arrays and Spevial Variables

Postby Versieon » Sat Jan 23, 2010 12:22 am

Thanks for the quick response
is there a quicker way to solve the array problem? it would be nice if you could just access the individual set of values instead of writing out each one, as ive got fifteen to a set and fifteen seperate sets at the momment
User avatar
Versieon
 
Posts: 375
Joined: Tue Sep 01, 2009 4:45 pm

Re: Arrays and Spevial Variables

Postby KarateBrot » Sat Jan 23, 2010 12:43 am

I think there's no quicker way but I can make a function for it so it's much quicker. It's a bit tricky but no big deal.
Image
User avatar
KarateBrot
 
Posts: 825
Joined: Mon Aug 31, 2009 7:32 pm
Location: Germany

Re: Arrays and Spevial Variables

Postby Versieon » Sat Jan 23, 2010 12:45 am

That would be great, and would save a lot of time!
did you understand the second part?
User avatar
Versieon
 
Posts: 375
Joined: Tue Sep 01, 2009 4:45 pm

Re: Arrays and Spevial Variables

Postby KarateBrot » Sat Jan 23, 2010 1:07 am

no not really :oops:

Edit:
There's a problem with the function. I need to let it count the array size. That's basically no problem. Grady once showed me his counting function but the problem is: It's only for arrays like [3,5,5,7,6,9, ...] and not for arrays like [[3,76,2],[4,6,1], ...]. It's not his fault but a mistake of algodoos function math.toString(). Math.toString can't convert arrays to text. That's a problem in this case. I will post a topic for it in the bug forum.

Edit2:
Never mind. I found a solution. instead of math.toString([3,2,1]) for example I have to use "" + math.toString([3,2,1])

Edit3:
Here it is
Code: Select all
Scene.my.ArraySize = (array) => { a := array ++ ["end"]; n := 0; SizeUnknown := true; for(40, (i)=>{ SizeUnknown ? { ("" + math.toString(a(i))) == "end" ? { n = i; SizeUnknown = false } : { }} : { }}); n };

scene.my.ArrayChange := (array, n, new) => { result := []; for(scene.my.arraysize(array), (i)=>{ i == n ? { result = result ++ [new]} : {result = result ++ [array(i)]} }) }


scene.my.ArrayChange(array, n, new)
array: Your array that needs to be changed
n: the number of the cell you want to change (starting with 0, 1, 2, ...)
new: the new cell value that you want to replace with the old one

Now you can use scene.my.ArrayChange to change any cell within the array more easy. It works up to 40 cells. If your array is bigger than 40 cells I still can adjust the code so you can change arrays up to thousands of cells. Just type scene.my.array = scene.my.arraychange(scene.my.array, 3, [3,2,1]) for example.
Last edited by KarateBrot on Sat Jan 23, 2010 1:49 am, edited 2 times in total.
Image
User avatar
KarateBrot
 
Posts: 825
Joined: Mon Aug 31, 2009 7:32 pm
Location: Germany

Re: Arrays and Spevial Variables

Postby Versieon » Sat Jan 23, 2010 1:46 am

ok so lets say I had this

scene.my.array1 = [ [1,1,1,2],[1,2,1,4]]
I could then say
scene.my.array1 = scene.my.arraychange(scene.my.array1,1,[0,0,0,0])
it would then change scene.my.array1 to [ [1,1,1,2], [0,0,0,0]]
right?

what i need to do for the second part is to combine the string "scene.my.my" with a variable, to get "scene.my.my1" with 1 being the value of the variable. I then need to convert that from a string to a value so that I can use it.

I think the "eval" function will read the string "scene.my.my1" as the variable scene.my.my1 but I don't know how use it.
User avatar
Versieon
 
Posts: 375
Joined: Tue Sep 01, 2009 4:45 pm

Re: Arrays and Spevial Variables

Postby KarateBrot » Sat Jan 23, 2010 2:02 am

Versieon wrote:ok so lets say I had this

scene.my.array1 = [ [1,1,1,2],[1,2,1,4]]
I could then say
scene.my.array1 = scene.my.arraychange(scene.my.array1,1,[0,0,0,0])
it would then change scene.my.array1 to [ [1,1,1,2], [0,0,0,0]]
right?

Yes exactly.

- - - - -

(1) do you only want to know how to let algodoo define scene.my.my1, scene.my.my2, scene.my.my3, ... or
(2) do you want to do something beyond that?

for the first thing it's
Code: Select all
for(3, (i)=>{ eval("scene.my.my" + (i+1) + "= i") })


After pressing enter in the console algodoo creates scene.my.my1, scene.my.my2, scene.my.my3. Of course this can be made more dynamic by adjusting it for your needs but that's the basic idea.
Image
User avatar
KarateBrot
 
Posts: 825
Joined: Mon Aug 31, 2009 7:32 pm
Location: Germany

Re: Arrays and Spevial Variables

Postby Versieon » Sat Jan 23, 2010 2:11 am

Yes exactly!! That is great, although i don't need to define them, I can change it so they can be read just the same with eval, now that I know how it works! Thanks alot!
User avatar
Versieon
 
Posts: 375
Joined: Tue Sep 01, 2009 4:45 pm

Re: Arrays and Spevial Variables

Postby KarateBrot » Sat Jan 23, 2010 2:13 am

Great! No problem :thumbup:
Image
User avatar
KarateBrot
 
Posts: 825
Joined: Mon Aug 31, 2009 7:32 pm
Location: Germany


Return to Thyme scripting

Who is online

Users browsing this forum: No registered users and 11 guests