This document explain how script works inside the engine.
1. Anatomy of a script call
In this section, we will see a scenario of a script call. At the beginning of the adventure, the hero walks in a zone that sends an IntroDialog message. Here is what happens in the code.
- 
In the mainfunction (inakagoria.cc), the world is updated withWorldProcessor::update()(inWorldProcessor.cc)
- 
The position of the hero is updated in the physics engine and the physics engine is updated with a call to b2World::update()
- 
The physics engine detects a collision between the hero and the sensor, and triggers a call to PhysicsListener::BeginContact()(inPhysicsRuntine.cc).
- 
The listener checks that the requirements for the message are fulfilled, and then call Script::onMessageDefered()(inScript.cc)
- 
Back in WorldProcessor::update(), just after the update of the physics engine,Script::handleDeferedMessages()is called (inScript.cc)
- 
The message that was defered is handled at this time and passed to Script::onMessage()(inScript.cc)
- 
A call to the Wren script is prepared with the name of the message, and then Advendture::onMessage()is called (inadventure.wren)
- 
The handler in the script retrieves the callback and, in this case, calls an anonymous function that was registered in Chapter1::initialize()(inchapter1.wren)
- 
A call to World::startDialog()is made, this Wren call is foreign (inworld.wren) and actually callScript::startDialog()(inScript.cc)
- 
Finally the startDialog()function sets the current operation toTalkand adds a dialog to the world state.
The messages are defered because the message handler could change the physical properties of the hero or characters. Changing the physical properties during b2World::update() is forbidden, the world is locked. So the message handler is called when b2World::update() is finished.