Normally a script function is executed in a few steps:
The code for this might look something like this:
If your application allows the execution to be suspended, either by using the callback function or registering a function that allow the script to manually suspend the execution, then the execution function may return before finishing with the return code asEXECUTION_SUSPENDED. In that case you can later resume the execution by simply calling the execution function again.
Note that the return value retrieved with GetReturnValue() is only valid if the script function returned successfully, i.e. if Execute() returned asEXECUTION_FINISHED.
When calling script functions that take arguments, the values of these arguments must be set after the call to Prepare() and before Execute(). The arguments are set using a group of SetArg methods:
arg
is the argument number, where the first argument is on 0, the second on 1, and so on. value
is the value of the argument. What method to use is determined by the type of the parameter. For primitive types you can use any of these. If the parameter type is a reference to a primitive type it is recommended to use the SetArgAddress() method and pass the pointer as the value. For non-primitive types the method SetArgObject() should be used, which will be described in the next section.
Once the script function has been executed the return value is retrieved in a similar way using the group of GetReturn methods:
Note that you must make sure the returned value is in fact valid, for example if the script function was interrupted by a script exception the value would not be valid. You do this by verifying the return code from Execute() or GetState(), where the return code should be asEXECUTION_FINISHED.
Passing registered object types to a script function is done in a similar way to how primitive types are passed. The function to use is SetArgObject():
arg
is the argument number, just like the other SetArg methods. object
is a pointer to the object you wish to pass.
This same method is used both for parameters passed by value and for those passed by reference. The library will automatically make a copy of the object if the parameter is defined to be passed by value.
Getting an object returned by a script function is done in a similar way, using GetReturnObject():
This method will return a pointer to the object returned by the script function. The library will still hold a reference to the object, which will only be freed as the context is released.
It is important to make a copy of the returned object, or if it is managed by reference counting add a reference to it. If this is not done the pointer obtained with GetReturnObject() will be invalidated as the context is released, or reused for another script function call.
If the script performs an illegal action, e.g. calling a method on a null handle, then the script engine will throw a script exception. The virtual machine will then abort the execution and the Execute method will return with the value asEXECUTION_EXCEPTION.
At this time it is possible to obtain information about the exception through the asIScriptContext's methods. Example:
If desired, it is also possible to register a callback function that will be called at the moment the exception occurred, before the Execute method returns. The exception callback can then use WillExceptionBeCaught to determine if the exception will be caught within the script or if it will abort the execution.