January 22, 2017

How Do I Make a Numerical Array?

Etoys does not have a list or array type. However several objects contain collections that behave similiarly to arrays. Holder objects accept a collection of any type of object, arranges them in a row and displays a rectangular cursor surrounding the currently selected object on screen. The cursor can be moved using script tiles.


Holder with Text Objects Image
To create a number array, you need to create a text object to hold each number. This can be done using a script but for this example just get a text object and a holder out of the supplies flap and drop the text object inside the holder using the black embed halo icon. Next right click the text object, select the options menu, siblings..., make multiple siblings then type in the number of objects you need. The holder is now full of text objects and the holder's cursor should be on its first element. Click on the individual text objects and type in the desired numbers.


Holder's Viewer Category Collections Image
Open the viewer for the Holder, select the collections category and you'll see some tiles relating to the object collection. One tile displays the numeric value of the object at the cursor. You can move the cursor by clicking on the up/down arrows in the Holder's "cursor" tile and view the values for every element. The cursor will wrap to the beginning element when you go past the last element. This is very important to remember when moving the cursor inside scripts. There are tiles for adding elements or removing all of them. If you need to remove one element through a script all objects know how to erase themselves and the holder will adjust automatically if one leaves.


Now you can write scripts to manipulate the numbers in the collection. The Holder "count" property is useful for setting up loops to move the cursor through the collection and access each value. You can write scripts to sort them, assign them to variables or other objects, add themselves together etc. There is a tile that will call a script inside every object. This is where siblings come in handy because they share scripts and variables so you could create for example a mapping function where they all increase themselves by a value.


Holders can contain other Holders so technically you can create multidimensional or associative arrays but that involves manipulating multiple cursors and could be more trouble than its worth depending on how badly you want to do it. For more on collections see Objects that Contain Collections.


Holders can have objects added to them using script tiles in the collections category.  The process of  creating sibling objects and assigning values to them is described in the article Dynamically Creating Sibling Objects. To create a numerical array using scripts add  a Text object and a Holder object, create sibling copies of the Text object, assign a number to each sibling's numeric value and add a scripting tile from  the Holder's viewer collections category to add or insert the sibling Text objects. This can be done inside a loop or on the fly as needed. An example project numberArrayMorph.040.pr can be downloaded from my GitHub repository, saved to your Etoys directory and loaded into Etoys. This project demonstrates user input forms and dynamic number array creation.



Last Updated.