Username:
Password:
Login Register Password Lost

Scripting Language

The Dot World Maker includes a scripting language which takes it roots in the Javascript language. If you know Javascript then using the scripting language of the engine should be pretty straight forward.

Warning: Some features of the Javascript language are currently not available:
  • Objects (new operator, this operator or JSON objects).
  • Switch / case.
  • try / catch blocks.

While working with the scripting language, keep an eye on the line above the editor as it will contains possible errors. At the same time the editor will highlight in red the faulty line (or the line where the language is not able to parse your code anymore).

The editor will also show the help of the function the cursor is over as well as propose function names while you type. To insert the function proposed, press the return key, or use the up / down keys to select the previous or next function.

Line of code

Lines of code are composed of a sequence of instructions ended with a semi-column (;). Unlike Javascript where the semi-column is optional in our scripting language the semi-column is mandatory.
Sound.Play('My Music');

Code blocks

Blocks of code are surrounded with curly brackets, used by function definitions, if / else blocks and loops.
if(win == true)
{
    Sound.Play('My Music');
    Display.AddMapMessage(10, 10, 'You win!');
}

Function call

Most of the code will contain function calls, which are either part of the engine offered API or self defined functions.

To call a function the function name must be followed by round brackets containing all the parameters (separated by a coma) required by the function itself.
Sound.Play('My Music', 0.7);

Function definition

To define your own function which you may call yourself at any time within the same script or used as "engine callback", the line must be outside of any other function definition, and must start with the keyword function.
You may then between two round brackets define any number of parameters which will be then translated as variable inside your function allowing to receive information from outside your function.
function IsRich(limit)
{
    if(Player.GetStat('Money') >= limit)
        return true;
    return false;
}
Self defined function may or not return values. To return a value use the keyword "return" followed either by the value or the variable which you want to pass back to the caller.

Variables

Variables are like "boxes" where you can store and retrieve values. Without variables you cannot "remember" anything, and will simply have fixed code. Using variables you can do calculations, checks and pass values which can change to the different functions.

To create a new variable and / or to change its value we must use the equal (=) operator. On the left side we find the variable name and on the right side the value or statement which will be evaluated to put inside the variable.
x = 5 + 2;
In this example we put the value of the calculation 5 + 2 into the variable x. From this point on, the language will know what X is and will replace it with it's value (which may change at any later time too).

Strings

Strings of text must be surrounded by single or double quotes:
x = 'this is my text'; // Valid
x = "this is my text too" // Valid
x = "this is wrong!' // Invalid
Strings must end with the same type of quotes you used to start the string.

Comments

Code comments are texts which helps you read the code but doesn't influence the logic.

The scripting language supports two different kind of comments:
  • Single line comments: starting with a double slash // till the end of the row
  • Multi lines comments: starting with a slash star and ending with a star slash
x = 5; // This is a single line comment
/* Now I shall have a multi
lines comment just for
the purpose of this example */

Conditions

In many case you will need to decide what to do depending on the game play, for example if the player wears a given armor then he will be able to beat a given monster. This "if" translate as an if statement inside the scripting language:
if(Player.Stat('Life') <= 0) // If we reach 0 we die
{
    Player.Respawn();
}
The condition of the "if" is defined with round brackets, and only if the condition is met then the following statement (or block of statements) is executed.

The language allows any number of "else if" and a final "else" statement:
if(x == 1)
{
    // Do something
}
else if(x == 2)
{
    // Do another thing
}
else
{
    // Or do this thing
}
If statements don't end with a semi-column as you see as you define what to do if the condition is met. You can have single line condition statement by not adding the curly brackets.

If statements can be done one within another for more complex logic, however be careful as your code could start to be hard to be understood and therefore hard to maintain and debug.

Operators

The scripting language understand the following operators in increasing order of precedence:
[]Array operator. Allows to access a value inside the array.
=Assign the value to a variable
==Checks if the first value is the same as the second
!=Checks if the first value is not the same as the second
>Checks if the first value is bigger than the second
>=Checks if the first value is bigger or the same as the second
<Checks if the first value is smaller than the second
<=Checks if the first value is smaller or the same as the second
&&Logical "and" (if this and that)
||Logical "or" (if this or that)
+Adds 2 values
-Subtracts the second value from the first
*Multiplies 2 values
/Divides the first value by the second
++Increases the variable by 1
--Decreases the variable by 1
The order of precedence can be changed by surrounding part of an expression with round brackets:
x = 5 + 3 * 2; // X == 11
x = (5 + 3) * 2; // X == 16
x = 5 + (3 * 2); // X == 11

While loops

A while loop allows to repeat a block as long as a condition is true:
while(Player.GetStat('Life') > 0)
{
    Player.SetLife('Life', Player.GetLife('Life') - 1);
}
In this case as long as the player life is bigger than 0 we will run the block. Be careful with infinite loops as they will freeze your browser while running them.

Do While loops

Do while loops are nearly the same as while loop however you are guarantee to run at least once the loop and then repeat it if the condition is met:
do
{
    Player.SetLife('Life', Player.GetLife('Life') - 1);
} while(Player.GetStat('Life') > 0);
Note that in this case we have a semi-column at the end of the line after the while statement.

© 2016 - 2017 - A. Bertrand - Terms & conditions