how to write into an array?
5 posts • Page 1 of 1
how to write into an array?
I can't seem to modify array values(via thyme).. why? and how can i do that?
- pixartist
- Posts: 65
- Joined: Wed Sep 02, 2009 5:30 pm
Re: how to write into an array?
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
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).
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.
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.
-
KarateBrot - Posts: 825
- Joined: Mon Aug 31, 2009 7:32 pm
- Location: Germany
Re: how to write into an array?
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...
-
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?
2KarateBrot
Can you post the unlimited array-change function, it will be very useful
And will it work in Phun?
Can you post the unlimited array-change function, it will be very useful
And will it work in Phun?
-
savask - Posts: 162
- Joined: Mon Oct 12, 2009 2:53 pm
- Location: Russia, Siberia
Re: how to write into an array?
Yeah of course. I don't know if it's working in Phun. Just try it I never took a deeper look into the extended for-function because I didn't make it. Immibis did.
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)
- 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)
-
KarateBrot - Posts: 825
- Joined: Mon Aug 31, 2009 7:32 pm
- Location: Germany
5 posts • Page 1 of 1
Who is online
Users browsing this forum: No registered users and 2 guests