Tutorial - Data Types

Sprite Basic has 6 main data types : number, string, boolean, object, list, map, object.
In addition, you can make arrays of any dimensions of any data type, you just have to specify the size of each dimension inside '[]'. list and map are collections, that is they hold a collection of variables of any types.
In the documentation, function will specify its returned value data type, as well as the data type of each parameter, if any, passed to the function upon call
Object is data type that has sub-type you must conform to. For example, you call addRenderer function on an object that has sub-type world. Object is the data type, world is the object sub-type. You couldn't call addRenderer on an object of sub-type body or sprite, that would raise an error.
Sometimes, function can be called on object of sub-type body or sprite. It will then be specified in the documentation under the form object:body|sprite which means that body and sprite are accepted sub-type of the caller object
In fact, when we say the function must be called by an object of sub-type sprite, it includes also other forms of sprite such as tilingSprite or animatedSprite. Same apply for animatedBody and shapedBody that can be use as a body. (we say this sub-types 'extends' the body sub-type)
Sometimes functions accepts or return any data type, in that case in the documentation the data type will be specified as 'any'. This is for example the case of list.push(any) where you can push to a list a number, string, object etc...

number

number is the numerical data type. it is a floating point value. You can get the decimal value with math library functions math.round(), math.ceil(), math.floor()

number n=Math.round(1.7) // 2

string

The string data type hold multiple characters.
You must enclose a string literal within quotation marks (" ").
You have at disposition a set of functions to manipulates strings

string brand="Coca Cola" boolean b=brand.contains("Co") // true as brand string contains substring 'Co'

boolean

boolean is data-type that can hold values true or false
It is used to test a condition in a if/else/endif command or while or repeat loop

boolean condition = true; if condition log("The variable is set to true.") // will be printed else log("The variable is set to false.") // not printed endif

list

list is a collection of variables of same data-types. It is in fact a single dimension array, with useful methods such as list.push(variable) and variable=list.pop()

list sprites sprites.push(sprite1) // add sprite1 to list sprites.push(sprite2) // add sprite2 to list sprite2=sprites.pop() // retrieve last element inserted and remove it from list

map

map is another type of collection that holds key/value pairs. keys must share the same type and values must share the same type, but keys and values can be of different types (any type allowed)

map levelScores map.put("level1",150) map.put("level2",200) map.put("level3",250) number scoreLevel3=levelScores.get("level3") // = 250

arrays

Array can be made of any dimensions, each dimension being of any size, but that must be specified at initialization.
you declare an array with the 'dim' keyword followed by the data-type of the values it holds and a list of dimensions wose sizes are specified between '[ ]'
Beware Arrays dimensions index starts at position 0 and go up dimensions length-1.

dim number positions[100][50] number position1=positions[99][49] // correct number position2=positions[100][49] // illegal as it references the 101th value of an array of dimension 100 (starts at zero)!