Skip to reading
Lua Scripting

Events and the lifetime of a record

Edition 0.86.4.1 HF1

0.86.4.1 Hotfix 1 edition. Earlier lessons and screenshots retain their original version context; revised references and supplements describe the newer behavior.

Download PDFView PDF page

LESSON 12

Count scene arrivals during one game session and watch what changes when you turn on Global Script.

Code example
local Script = {}
local arrivals = 0
Code example
function Script.on_scene_loaded(event)
    arrivals = arrivals + 1
    Log("Arrival " .. arrivals .. " in " .. tostring(event.sceneId))
end
Code example
function Script.run(context)
    return arrivals
end
Code example
return Script

Enable 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.

Page 30 of 133Permalink
Go to page