Time for action – storing the script
The first task is to read the script, extract the needed functions from it, and store them in a safe place. Then, load the project for the game and add a new class with the following code:
class AIScript {
public:
QScriptProgram read(const QString &fileName);
bool evaluate(const QScriptProgram &program, QScriptEngine *engine);
QScriptValue initFunction;
QScriptValue heartbeatFunction;
QScriptValue defendFunction;
};The reading method can have the same content as the original readScriptFromFile method. The evaluate method looks as follows:
bool AIScript::evaluate(const QScriptProgram &program, QScriptEngine *engine) {
QScriptContext *context = engine->pushContext();
QScriptValue activationObject;
QScriptValue result = engine->evaluate(program);
activationObject = context->activationObject();
if(!result.isError()) {
initFunction = activationObject.property("init");
heartbeatFunction = activationObject...