Time for action – implementing the heartbeat event
The heart of AI is the heartbeat function that is executed at equal intervals of time to allow the AI to decide about the actions of the object. The script that is executed will have access to the creature that it operates on as well as its environment. It can also use anything that it defines in the this
object. Now, add a heartbeat function to AIScript
:
void AIScript::heartbeat(QScriptEngine *engine, QObject *personObject, QObject *otherObject) { QScriptValueList params; params << engine->newQObject(personObject); m_thisObject.setProperty("enemy", engine->newQObject(otherObject)); heartbeatFunction.call(m_thisObject, params); m_thisObject.setProperty("enemy", QScriptValue::UndefinedValue); } ...
Get Game Programming Using Qt now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.