S2ENGINE HD
1.4.6
Official manual about S2Engine HD editor and programming
|
Scripts are used to customize the behavior of objects of the game scene but also programming the logic of the game GUI (widgets). The first script to be executed just when game start is the script that you specify into the .ini file (see also GUI Main widget. To write a script you can use any notepad/wordpad application and save the text file with sc2 extension. To associate a script to an object you must set the script param of the object inside the Editor.
S2Engine HD script language has a syntax similar to standard C language. You can declare variables, at start but also in any part of the code, assign or read them, define functions, also with parameters, call functions inside other functions, do arithmetic and logical operations, do loops and if...then...else constructs. Like C language the code statements are separated by ';' simbol.
There are only few differences regarding:
Every script is associated to an object or a widget. At certain points of the object/widget code the script code is called through some entry point functions:
If you want to code script of an object or a widget you have to implement these functions that are the script entry points (just like DllMain(), main() or WinMain() entry point functions in C language). To implement, for example, the init entry point function you must write:
see the Functions for more about function declaration.
The following are all the reserved language keywords:
Directives are some MACRO that let S2Script compiler to comunicate with S2Sandbox Editor.
To change params of an object script you have to access the class tab and select scriptParams object param.
Comments can be inserted into the script code. A comment must be preceded by simbol
and followed by simbol
. It can be more than one line:
Data type format is similar to C language except for some new data type native support. The following are the supported types:
Variables are indentifiers that can contain a value of a certain type. Their value can be read or assigned. Before using it a variable must be declared. To declare a variable you must type:
where:
Variables can be defined:
The variable described until now are valid only inside a single script. It is possible to define variable that are visible inside all the scripts of a game, in this case we say they are global. global variables can be defined in the following manner:
Like C language in S2Script variables value are assigned by using the assigment statement:
The right term of an assignment statement can be also another variable or a function. The important thing is that the type of the variable or the left is the same as the type of the variable, function, data at the right. For example:
You can define also list of variables which we call arrays. An array is a list that contains variable of the same type. To define an array you have to specify how many elements the list can contain:
In the example an it is defined an array 'a' that can contain a maximum of 100 integers, and an array 'b' that can contain a maximum of 100 floats. To access to an element of an array you have to specify the index of the element:
In the first line you assign the value 10.0 to the i-th element of the array a. In the second line you assign to variable b the i-th element of array a.
Just like C language, a code block is a piece of code included into '{' and '}' symbols. Code blocks can be innested. Every variable created in a block could be visible only into the block and its contained blocks. The function body, for example, is a code block that could contain, for example a while construct that is another block:
In this case the init block is the parent of while and if so a variable defined into while block isn't visible into if block, but a variable defined into init block is visible either into while and if blocks.
S2Script lets you to use logical and arithmetic operators. Single operators can be concatenated forming more complex expressions. Arithmetic expressions are made by arithmetic operators (+, -, *, /, -) which take in input 2 (or 1) numbers (or vectors) and give, as result, a number (or a vector). For example:
is an arithmetic expression.
Logical expressions are made by logical operators (&&, ||, !, <, >, <=, >=, == respectively and, or, not, smaller, greater, smaller equal, greater equal, equal) which take in input 2 (or 1) booleans or numbers and give as result a boolean. For example:
is an logical expression where b<3.0 returns true if b is smaller then 3.0, c>10.0 returns true if c is greater than 10.0.
When an expression is encoutered compiler executes in series operators from left to right, for example the expression 2+4+5 is solved by executing 2+4 and then summing the result to 5. As said in S2Script, unlike C language, operators haven't a priority order, so, to let compiler solve expression in a particular order you have to use parentheses. For example the expression (3+4)*(4+5) is solved by first summing 3+4 and 4+5 and then multiply the two results, otherwise 3+(4*4)+5 is solved by executing 4*4 and then sum the result to 3 and to 5.
Functions are code blocks, labelled with a name that identifies them, that execute a sequence of operations and give a result and, mainly, can be executed more than one time in any part of the script by name calling. S2Script lets you to implement your own functions and call them inside other functions or inside the main entry functions. Like in C functions must return a value (if you want function doesn't return a value you have to specify the 'void' return value). To define a function you must write:
where:
NOTES:
The following code is right:
While the following code is wrong:
After defined a function can be called inside another function or inside an entry point function. Calling a function means executing it. To call a function you must write its name followed by the params values encolsed by '(' and ')' simbols:
S2Script lets you to use the main syntax constructs of a programming language: conditions and loops. Conditions can be obtained using the if...then...else construct, the loops can be obtained using the while construct. The syntax is the same of C language.
This construct lets you to execute a code part only if a condition is satisfied. It has the following form:
where logical expression is an expression made with logical operators which result is a boolean value "true" or "false".
This construct lets you to repeatedly execute a code part until a condition is no more satisfied. It has the following form:
where logical expression is an expression made with logical operators which result is a boolean value "true" or "false".