January 25, 2017

Variable Types and Thoughts about Scope

Variables


Wrong Type Image
Correct Type ImageVariables are added to the viewer's variable category by left clicking on the light brown icon with the down arrow on the viewer's top menu bar. The types available on the pop up list are dependent on the class of object or what main preferences have been set but there is a basic set always available. Etoys does do explicit type checking. You can replace
tiles inside scripts with variable name tiles. A green highlight appears around the receiving tile if the types match.








Variable Types Image
Some Variable Types
Variable names are alphanumeric only. I've gotten in the habit of using lowercase and capitalizing the first letter of any additional words when naming variables, scripts, objects etc. Etoys will append a number to any reserved words used in a name. Variables can be removed or modified using the options menu on the left. Be sure to remove them from any scripts before doing so. Variables can be assigned values in the viewer, in a watcher or in an assignment statement inside a script and they may be passed as parameters to other scripts.




Expressions Images
Numbers can have up to 10 decimal points. The value display will round up if assigned to a number with more decimal points than currently set. This is something to be mindful of when doing comparisons. The round function from the script editor menu will round numbers to the closest integer. Large numbers are displayed in scientific notation. Inside scripts, number variable types can be  increased, decreased or multiplied by a value or assigned a value,  the result of an expression or another variable. Tiles inside expressions can be replaced with number type tiles.


The Point type is a bit specialized. Points describe an x and y location and are assigned in the form 0@0. They can be increased, decreased and multiplied by another point value but cannot be used in expressions and can only be compared for equality.


The String type can be assigned alphanumeric characters from the keyboard or character strings inside other objects. Strings are immutable meaning that strings cannot be altered through expressions and are replaced completely when reassigned. They can only be compared for equality. However some objects have methods for manipulating strings. See Objects that Contain Collections for more information.



Player Type Image
The Player type can be assigned objects in your project. Etoys documentation often refers to objects, morphs and players. All are object classes and there are distinctions between them but for the user, player is the most relevant term to scripting. When you create a player type variable you'll see a black dot inside the value display. A little black dot does exist hidden up in the top left hand corner of the main menu bar. This dot is the default value for player variables. Any tile containing the word dot can be replaced with an object reference tile. Inside the viewer you can assign a value by clicking on the image in the value tile and selecting an object on screen with the cross hair. Or drag the variable tile out by the assignment symbol and drop. The value tile now says dot. Right click on the desired object and click the orange rectangle halo icon to get a reference tile and replace dot with it. That object's properties, methods, scripts and variables can now be accessed using the variable. This is a very powerful feature better demonstrated than explained. See Dynamically Creating Sibling Objects  for an example.


Color variables display the current color of the object and must be assigned with the color picker. They can only be compared for equality. To manipulate object colors use numeric properties in the color category instead.


The graphics type is useful for Sketch objects and can be used to change costumes for animation. Assign inside scripts by clicking on the image value and selecting an object on the screen or assign a pen trail graphic from the world or a Playfield object to a Sketch's graphic property.


Other variable types are assigned through drop down lists and can only be compared for equality. Boolean types are either true or false. ScriptName variables can be used to replace values in tiles from the scripting category.



Script with Parameter Image
Any variable type can be passed to another script as a parameter but scripts can accept only one parameter. It's name in the script is it's type so make sure you select the correct type in the script editor menu. Clicking the parameter name in the script editor menu gives you tiles that you can use in your statement tiles. This is the one serious limitation in the tile scripting system. If you are really determined to have multiple parameters you can pass a holder containing the values you want and fetch them out of it but this won't usually work well if you're using recursion to send a parameter in an expression.






Etoys does not have a list or array type. For help on creating arrays or object collections see How do I Make a Numerical Array? and Objects that Contain Collections.


Scope 



Remember when I was going on about how all the objects in your project are instances of objects inside an instance of a world object? That means that all objects are local to the world object. Variables can be modified by scripts belonging to any object including the world object. That doesn't mean however you shouldn't think about scope. Even though you can define variables anywhere you like, scripts and variables you intend to share with sibling objects must be defined inside siblings. I try to define variables all objects might use inside the world object (global scope) and define others in relevant objects (local scope). If you intend to define a constant value, it might be good to remind yourself by prefacing the name with const such as constVariableName. It does make it a little more difficult to explain the concept of scope to a novice because it takes discipline to practice. Discipline I often lack.

Last Updated.