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); }
Bring the timer back, set it to start()
, and also enable the running heartbeat functionality from within the timer event:
void timerEvent(QTimerEvent *te) { if(te->timerId() != m_timerId) return...