Events and the lifetime of a record
LESSON 12
Count scene arrivals during one game session and watch what changes when you turn on Global Script.
local Script = {}
local arrivals = 0function Script.on_scene_loaded(event)
arrivals = arrivals + 1
Log("Arrival " .. arrivals .. " in " .. tostring(event.sceneId))
endfunction Script.run(context)
return arrivals
endreturn ScriptEnable the record, check OnSceneLoaded in the Lua Designer, check Global Script, apply, save and
start a fresh play session. Move between two rooms. Expect the next arrival number each time.
With Global Script off, each dispatch gets a fresh module and starts its own count.
The checkbox subscribes the record. The on_scene_loaded function receives that event. An ordinary
explicit run uses run. An event without its matching handler can fall back to run. A global record can
also implement on_global_ready for initialization. Changing source or rebuilding the runtime can
recreate the module.
Use PCA variables for saveable progress. Local state in a global module lasts while that instance
exists. It does not automatically become part of a saved game.
