N3DSE Script Engine

N3DSE is a dynamic linked library made up of a compiler and a virtual machine. Programming in N3D Script (N3DS) should not be too difficult for C++ programmers as this is similar to programming in C++ using only static classes.

Here are some properties of N3DSE:

  • N3DSE can compile a set of script files interacting each other.
  • Each Script file is like a static class with its attributes and its methods.
  • A script method can be called from C++ code or from anoter script.
  • There is a base script, the System script, which methods are hard C++ coded functions. The user can add them to the system using pointer to functions.

The following diagram sums up the way N3DSE works.

Creating a script

A script is associated with a file. This means that there can only be one script per file (like in java). A script is like a static class in C++. Each script has its name and each name must be unique. The script name “System” is reserved.

Here is the syntax to write a script :

script script_name
{
    #code of the script#
}

NB: comments are inside “diesis brackets”

Declaring variables

There are 4 primitive types in N3DS. They are exactly the same as the associated C types

  • Double
  • Integer
  • Bool
  • Char

N3DS also enables the use of arrays of these primitive types.

Variable declarations and initializations are done using the keyword declare:

 declare bool  condition = false;
 declare double  variable;
 declare int  array = new [10];

NB: simple variables needn’t to be initialized while arrays must be initialized with there size.

Operations on expressions and assignments

The operations on expressions are similar to C/C++ operations (+, – , *, /)

NB: strings (arrays of chars) can be concatenated with any other expression using the

+ operator.

Assignments are made using the keyword set:

set myVar = 2*array[2];

 

Declaring functions

Functions are declared exactly like in C programming.

Ex:

int inc(int var1)
{
    return var1 + 1;
}

N3DS also enables the use of references thanks to the keyword ref

Ex:

void inc(ref int var1)
{
    set var1 = var1 + 1;
}

Calling functions

Function calls are made using the keyword call.

In any case you must specify the name of the script in which the function was declared

set myVar = 5* call script_name.Func_name(param1);

If the reference on a variable is required, you must use the keyword ref

call script_name.Func_name2(ref param1);
NB: Every function of any script can be called by the user. The dll provides methods to set the arguments of the function and then to call it.

System/Extern functions

System/extern functions are hard coded C++ functions which can be called from the script.

This enables the user to extend the language with functions coded and compiled with a C++ compiler. He can then enjoy speed and access extern data.

In order to access extern functions, you must first write their prototypes in one of the script files.

In order to write the prototype of an extern function, you must use the keyword prototype:

prototype int func_name(int param1);

When you call a system function, you must consider that they belong to the System script.

The system script is a virtual script in which all the extern functions will be stored.

Consequently, the call of an extern function will looks like something like this:

call System.func_name(5);

If you’ve declare system/extern functions in one of your script files, you must match their prototypes with your C++ functions. A dll method is provided for that task.

The principle is quiet simple. When you call an extern function from the script, the virtual machine will call the appropriate C++ function using its pointer to function.

The N3DSE dll provides a set of methods in order to get the parameters and set the return value.

If statement

Here is the syntax of the if statement:

 if (condition)
{
    #body#
}
else
{
     #else body# 
}

The body of an if statement is executed if the value of the condition is true.If it’s false, the body of the else statement is executed.

NB: the condition must be a Boolean expression

The else statement is optional.

While statement

Here is the syntax of the while statement:

 while (condition)
{
    #body#
}

The body of a while statement is executed until the value of the condition becomes false.

NB: the condition must be a Boolean expression