how to write into an array?

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

how to write into an array?

Postby pixartist » Mon Feb 01, 2010 2:43 pm

I can't seem to modify array values(via thyme).. why? and how can i do that?
Image
pixartist
 
Posts: 65
Joined: Wed Sep 02, 2009 5:30 pm

Re: how to write into an array?

Postby KarateBrot » Mon Feb 01, 2010 3:00 pm

I wrote something about it a few days ago.

viewtopic.php?f=13&t=1503

If you want to modify specific cells you can do it this way:

for example you've got the array "scene.my.array = [3, 5, 8, 7]" and you want to change the third cell value to 24
Code: Select all
scene.my.array = [scene.my.array(0), scene.my.array(1), 24, scene.my.array(3)]


I also made a function so that you don't have to type so much for very long arrays. (scene.my.ArraySize is Grady's work. It's very cool because it counts the number of cells in an array. Without it I wouldn't have been able to do scene.my.ArrayChange).
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, 2, 24) for example.
Image
User avatar
KarateBrot
 
Posts: 825
Joined: Mon Aug 31, 2009 7:32 pm
Location: Germany

Re: how to write into an array?

Postby Kilinich » Mon Feb 01, 2010 5:00 pm

pixartist wrote:I can't seem to modify array values(via thyme).. why? and how can i do that?

In some cases I use this way:
arr = old_array * [1,1,1,0] + [0,0,0,new_value]
Dream of Algodoo as game development engine...
User avatar
Kilinich
[Best bug reporter 2010]
 
Posts: 2098
Joined: Mon Aug 31, 2009 8:27 pm
Location: South Russia

Re: how to write into an array?

Postby savask » Thu Feb 04, 2010 10:46 am

2KarateBrot
Can you post the unlimited array-change function, it will be very useful :thumbup:
And will it work in Phun?
User avatar
savask
 
Posts: 162
Joined: Mon Oct 12, 2009 2:53 pm
Location: Russia, Siberia

Re: how to write into an array?

Postby KarateBrot » Thu Feb 04, 2010 8:52 pm

Yeah of course. I don't know if it's working in Phun. Just try it :D I never took a deeper look into the extended for-function because I didn't make it. Immibis did.

Code: Select all
Scene.my.for2_internal = (n, min, max, what) => { (min == max) ? { (min < n) ? { what(min) } : {} } : { Scene.my.for2_internal(n, min, min + (max - min + 1) / 2 - 1, what); Scene.my.for2_internal(n, min + (max - min + 1) / 2, max, what) } }; Scene.my.for2 = (n, what) => { num := Math.toFloat(Math.toInt(n)); max := 2^(Math.toInt(((num^0.001 - 1) / (2^0.001 - 1)) + 1)); (num <= 0) ? {} : { (num == 1) ? { what(0) } : { Scene.my.for2_internal(num, 0, (max/2)-1, what); Scene.my.for2_internal(num, (max/2), max-1, what) } }; };

Scene.my.ArraySize = (array) => { a := array ++ ["end"]; n := 0; SizeUnknown := true; scene.my.for2(200, (i)=>{ SizeUnknown ? { ("" + math.toString(a(i))) == "end" ? { n = i; SizeUnknown = false } : { }} : { }}); n };

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

Hmm but something isn't working. I tested it in algodoo. I don't have enough time at the moment so I just posted the not working code. I'll correct it later (or someone locates the error faster than me)
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 2 guests

cron