Image:
 Author: therestofguys Group: Default Filesize: 34.66 kB Date added: 2023-03-05 Rating: 5 Downloads: 1199 Views: 269 Comments: 0 Ratings: 1 Times favored: 0 Made with: Algodoo v2.1.0 Tags: script
|
An array function example I made for myself to understand it.
The goal is to call up different values from an array.
----------
Custom global variable for an array:
By entering scene.my.array = ["A", "B", "C", "D", "E", "F", "G", "H"] into the console, scene.my.array only now exist as a global variable of this scene that is the array with 8 letters.
Note that each letter are contained within a "" since these are texts. If using only numbers then there's no need for "".
----------
To call up a value from an array:
It works like this: array(slot) = output.
So if I put:
scene.my.array(0) into the console, it will reply A
scene.my.array(1) into the console, it will reply B
...
Note that array slot positions start from 0 instead of 1.
----------
Making it interactable:
Knowing above, I can use another custom global variable for a changing (slot) value.
In this case: scene.my.number
Together with the array: scene.my.array(scene.my.number)
Like the array, entering scene.my.number = 0 into the console, so that scene.my.number exist in the scene.
In the green part, I made a little rectangle, and put the line into its text parameter:
{"" + scene.my.array(scene.my.number)}
Then I made these numbered squares, and edit each of them's onCollide parameter:
(e)=>{scene.my.number = 0} for the square with text 0
(e)=>{scene.my.number = 1} for the square with text 1
...
This way, when they collide with another object, in this case the little orange circle, the text in the green part displays an array's value corresponding to the slot position whenever scene.my.number changes.
----------
Reference I used:
https://algodoo.fandom.com/wiki/Thyme - not all accurate info |