Callbacks
Any function from system, world or editor scripts can be called in a C# code. UnigineScript functions that are called from an external code are known as callbacks. Via callbacks scripts can communicate with each other, as well as with the external application.
The callback functions can receive optional arguments of the int or IntPtr type that are used to store user data. IntPtr values can be wrapped in classes, for example:
IntPtr ptr;
// create a node and then create a Unigine object
Unigine.Object.create(new Node(ptr));
See also
An example can be found in <UnigineSDK>/source/csharp/samples/Api/Scripts/Callbacks/ directory.
Callbacks Usage Example
C# Side
To demonstrate how callbacks can be used, let's code the C# part first:
using System;
using Unigine;
class UnigineApp {
// world function
private static Variable runWorldFunction(Variable name,Variable v) {
Log.warning("runWorldFunction({0},{1}): called\n",name.getTypeName(),v.getTypeName());
Engine engine = Engine.get();
return engine.runWorldFunction(name,v);
}
// entry point
[STAThread]
static void Main(string[] args) {
Wrapper.init();
// export the runWorldFunction() function defined above
Interpreter.addExternFunction("runWorldFunction",new Interpreter.Function2v(runWorldFunction));
// initialize the engine
Engine engine = Engine.init(Engine.VERSION,args);
// main loop
while(!engine.isDone()) {
engine.update();
engine.render();
engine.swap();
// call the counter() function of the script (defined below)
Variable ret = engine.runWorldFunction(new Variable("counter"));
// print a message depending on the value returned by the counter() script function:
// print the current value of the counter
if(ret.getInt() != -1) Log.message("counter is: {0}\n",ret.getInt());
// print the world name
if(ret.getInt() == 3) Log.message("\nworld name is: \"{0}\"\n",engine.runWorldFunction(new Variable("engine.world.getName")).getString());
}
// shutdown engine and call the shutdown() function in the loaded world script
Engine.shutdown();
}
}
Unigine Script Side
And now the UnigineScript side where callbacks are defined:
// callbacks.cpp
/*
*/
int callback(int value) {
log.warning("callback(%s): called\n",typeinfo(value));
return value;
}
/*
*/
void counter() {
for(int i = 0; i < 4; i++) {
log.warning("counter(): called\n");
yield i;
}
return -1;
}
/*
*/
int init() {
log.message("\n");
// run the callback() script function via the API runWorldFunction() function
log.message("result is: %s\n\n",typeinfo(runWorldFunction("callback",10)));
log.message("result is: %s\n\n",typeinfo(runWorldFunction("callback",vec3(1.0f,2.0f,3.0f))));
log.message("result is: %s\n\n",typeinfo(runWorldFunction("callback","a string")));
/////////////////////////////////
// show console
engine.console.setActivity(1);
return 1;
}
Calling Sequence
The sequence of function call will be as follows:
- The interpreter exports the runWorldFunction() function to make it available from the script.
- The engine is initialized, and the init() function of the script is called. This function calls the exported runWorldFunction() function.
- The exported runWorldFunction() function calls the callback() function from the script.
- The engine enters the main loop, where it calls the counter() function from the script by using the Unigine.Engine.runWorldFunction() function which has the same behavior as the Unigine::Engine::runWorldFunction() C++ API function.
Output
The following result will be printed into the console:
runWorldFunction(string,int): called
callback(int: 10): called
result is: int: 10
runWorldFunction(string,vec3): called
callback(vec3: 1 2 3): called
result is: vec3: 1 2 3
runWorldFunction(string,string): called
callback(string: "a string"): called
result is: string: "a string"
counter(): called
counter is: 0
counter(): called
counter is: 1
counter(): called
counter is: 2
counter(): called
counter is: 3
world name is: "data/callbacks"