AI Scripting Overview
AI Scripting
The Ryzom Core AI system has a sophisticated custom scripting language that is used for scripting the various group states.
You can also get thorough documentation by going to AI Scripting Native Functions Reference.
Here's an example of a simple code block:
(aggroSize)getAggorListSize(0); // to much player are attacking the boss if (aggoroSize > 10) { ($player1)getAggroListElement(0); ($player2)getAggroListElement(1); ($player3)getAggroListElement(2); // so the boss teleport 3 person from its aggro list at the end of the world teleportPlayer($player1, 14233, 123123, 0, 0); teleportPlayer($player2, 14233, 123123, 0, 0); teleportPlayer($player2, 14233, 123123, 0, 0); clearAggroList(); }
In addition to code segments within AI actions you can also create AI script templates in World Editor using the script_rep and script nodes. It looks a little like this:
AI Script Rep Container in World Editor
AI Script Window for Script Rep
General information
Its a C like code.
Comments are defined after two slashes like this:
// my comment
Code is used to specify decisions for state machine on group. Each group can have different variables and functions, they can only be affected via code only.
Variables don't need to be defined before they are used. They are automatically created with value equals to 0 or "".
Variables are either floating point numbers (no prefix), character strings ('$' prefix) or script contexts ('@' prefix).
There are two special object that are hard-coded and that can be used to call function:
- parent: represent the parent group of the current group
- caller: represent the group that called a function on the current group
Additionnaly one can reference any static group defined in the same AI instance by its name. The usage is similar to that of 'parent' and 'caller'. In fact 'parent', 'caller' and group names are a kind of pseudo-contexts and can be used as such (without the '@' prefix).
Debug and log
There are two special function for debug and logging:
- print: used for debug purpose, removed in release mode
- log: used to display any information that should be read in release mode
Syntax:
print("foo"); log("bar");
You can put any number of const string (e.g. "toto") and var (float
or $string
) separated by coma. All the string representation are concatenated to form the final string.
Example:
print("Ma position est ", x, ":", y);
About Functions
You can dynamically define function on the group.
This function are called script function or user function (as opposed to the hard coded ‘native function’).
User functions don't have parameters.
Functions can be defined this way:
my_function() { print("message"); // print a message mavar = 5*mavar; // the variable mavar is now equals to mavar*5 caller.mavar = mavar; // the variable mavar of the caller's is now equal to mavar (see info box for more explanation) }
In the above code we could replace caller by parent which is another keyword that indicate the parent of a machine state. The parent/children relation is defined for dynamic spawned groups (see AI Scripting Native Functions Reference).
You can call a function like this (note that if a function is not defined for a group, nothing is called):
my_function(); // call my_function from this group group1_tryker.my_function(); // call my_function for group1_tryker (inside this function the current group is accessible via caller keyword)
Variable manipulation
Some simple mathematical operators can be used: +, -, /, * with or without parenthesis.
Example:
mavar =(5*mavar+1)/3;
Note that the negative operator should be used with caution, YOU MUST add a space after the ‘-‘ symbol.
You can do comparison with variables and or numbers (can be used for if and while statements).
mavar = 10; if(mavar < 10) { // ... }
You cannot do mathematical computation inside of an if statement. For example you cannot write: if (x+10 < 20))
Code flow structure
Classical code flow constructs
while, if, else can be used as in C.
onchildren
onchildren special construct allow you to execute a script block on all the child of the current group.
onchildren() { // ... }
onchildren
random special construct allow you to execute randomly a block of code among several. One instruction in the block following rand keyword is chosen and executed. This instruction can be a block, in which case the whole block is executed.
random() { block1(); { // Block2 begin print("foo"); } // Block2 end (bar)block3(baf); }
Native function calls
Native calls are available too. Native calls offer hard coded services to the scripter.
Native calls have input and output parameters (AKA parameters and return values).
(destVar1,destVar2)nativeMethod(ParamVar1);
This line calls the function nativeMethod for the current group. The left parenthesis are used to define output arguments, the right for input arguments.