Tutorial - Basic Language Commands

This tutorial is aiming to teach you the commands of the Basic Language used by Sprite Basic Compiler. As you will see, it is the simplest language to learn and as such a very good entry point in the wonderful world of coding
If you know already coding, you'll find everything here familiar

foreach ... in ... next

The foreach statement is used to iterate through a list of objects.
At any point within the foreach block, you can break out of the loop by using the break keyword, or step to the next iteration in the loop by using the continue keyword.

//apart for global declaration that are outside meethods, all statements have to be put within a method, like OnInit() (called once at start), OnUpdate() (called every frame) or a user defined method foreach object sprite in sprites // iterate through sprites list, at each iteration sprite object holds value of a different element of list x=sprite.getPositionX() // get position of current sprite y=sprite.getPositionY() // get position of current sprite sprite.setPosition(x+10,y) // each sprite of list will successively be moved 10 pixels to right next // if any sprite left, loop for next sprite

for...to...[step]...next

The for statement is used to iterate through a list of numerical value, optionally by a defined step (that can be negative, if no step defined, 1 will be the step)
It iterates from bound to bound inclusively
At any point within the for block, you can break out of the loop by using the break keyword, or step to the next iteration in the loop by using the continue keyword.

for i=5 to 1 step -1 log("i:"+i) // will print on logging window 5,4,3,2,1 next

while ... wend

The while statement executes a statement or a block of statements until a specified expression evaluates to false.
At any point within the while block, you can break out of the loop by using the break keyword, or step to the next iteration in the loop by using the continue keyword.

i=1 while i<=5 log("i:"+i) // will print on logging window 1,2,3,4,5 i++ wend

repeat ... until

The repeat statement executes a statement or a block of statements as long as specified expression DOESN"T evaluates to true.
The other difference with the while loop is that the condition is evaluated at the end, so there is always at least one iteration of the loop
At any point within the repeat block, you can break out of the loop by using the break keyword, or step to the next iteration in the loop by using the continue keyword.

i=1 repeat log("i:"+i) // will print on logging window 1,2,3,4,5 i++ until i>5

break

break out of the loop
see for, foreach, while, repeat

for i=1 to 8 if i=5 break endif log("i:"+i) // print 1,2,3,4 then exit, 5,6,7,8 will not be printed next

continue

step to the next iteration in the loop
see for, foreach, while, repeat

for i=1 to 8 if i=5 continue endif log("i:"+i) // print 1,2,3,4, 5 will not be printed, then print 6,7,8 next

if ... else ... endif

An if statement identifies which statement to run based on the value of a Boolean expression.In the following example, the Boolean variable result is set to true and then checked in the if statement.The output is The condition is true.

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

and

The conditional and operator performs a logical-AND of its bool operands. Both operands must be true so result evaluates to true

boolean a= true boolean b= true boolean c= false if a and b log("result is true") // will be printed endif if a and c log("result is false") // will not be printed endif

or

The conditional or operator performs a logical-OR of its bool operands. At least one operand must be true so result evaluates to true

boolean a= true boolean b= false boolean c= false if a or b log("result is true") // will be printed endif if b or c log("result is false") // will not be printed endif

not

The conditional not operator performs a logical-NOT of its bool operand. If operand is true, result will be false, if operand is false, result will be true

boolean a= true boolean b= false if not a log("result is false") // will not be printed endif if not b log("result is true") // will be printed endif

read

called from a method, read a string data or a numeric data that are declared in the global section

//called only once at start string str method OnInit() { for i=1 to 5 read str log(str) // log once value1,value2,value3,value4,value5 next } data "value1","value2","value3","value4","value5" // values to be read

label

in global section, defines a point at which datas can be restored to be read again.
see restore

// see restore

restore

in global section, restore datas from a label statement so datas can be read again.
see read, label

//called every frame, ie loops until program ends method void OnUpdate() { restore dataStart // start reading data form this label point for i=1 to 5 read x log("x="+x) //will log 1,2,3,4,5 each frame next } label dataStart data 1,2,3,4,5 // numerical values, only strings and numerical values are allowed as datas

method

used to defined a user method
unlike OnInit() and OnUpdate() that are called by the program, user defined methods are not called automatically, you have to call them explicitily (from OnInit() or OnUpdate() or another user-defined method) for their code to be executed
defines an optional list of parameters of any type passed to the method
parameters list has to be put within brackets "(" and ")", those are mandatory even if parameters list is empty
statements of method shall be put within "{" and "}". Return type must be specified after the "method" label. If nothing is returned, set return type to "void"

method void addSprites(number nsprites) { for i=0 to nsprites-1 renderer.createSprite("monster","monster.png",rnd(width),rnd(height),25,25,0xffffff,0.5) next }

return

user to return a value from user defined method
allowed return types are string, number, list, map, object, boolean and must be specified in method declaration
return statement is not mandatory in a method if you don"t want to return a value. But you shall in that case set retrun type to "void" in method declaration

method number Mult(number a, number b) { return a*b }

dim

used in conjonction with global to declare an array of any dimension to store any type of value
dimensions are separated as a number enclosed into square brackets. type of the variables to be contained is the array is mandatory
each dimension index start at zero and goes up to the bound -1 inclusively, so global dim number a[10] can hold 10 values from 0 to 9 (but no more!)
arrays can also be local to a method

method void fillValues() { for i=0 to 9 for j=0 to 9 for k=0 to 9 values[i][j][k]=i*j*k // fill array with values next next next } dim number values[10][10][10] //3 dimensions array of number defined as global (because outside any method)

switch...case...break...default...endswitch

The switch statement is used to perform different actions based on different conditions.
Use the switch statement to select one of many blocks of code to be executed.
Each block is followed by a break statement. It break is not present block execution fall off next block

number i=2 switch i case 1 log("i=1") // not printed break case 2 log("i=2") // printed break case 3 log("i=3") // not printed break default log("undefined") endswitch