ensure `enable` doesn't miss newly-added scripts

and add some more documentation
develop
Myk Taylor 2022-12-10 22:13:38 -08:00
parent e88b1fdfe5
commit ffd646462a
No known key found for this signature in database
2 changed files with 15 additions and 3 deletions

@ -5574,9 +5574,9 @@ Example usage::
end end
end end
If the state of your script is tied to the active savegame, then your script If the state of your script can be tied to an active savegame, then your script
should hook the appropriate events to load persisted state when the savegame is should hook the appropriate events to load persisted state when a savegame is
reloaded. For example:: loaded. For example::
local json = require('json') local json = require('json')
local persist = require('persist-table') local persist = require('persist-table')
@ -5597,6 +5597,15 @@ outside of any function. DFHack will load your script as a module just before
the ``SC_DFHACK_INITIALIZED`` state change event is sent, giving your code an the ``SC_DFHACK_INITIALIZED`` state change event is sent, giving your code an
opportunity to run and attach hooks before the game is loaded. opportunity to run and attach hooks before the game is loaded.
If an enableable script is added to a DFHack `script path <script-paths>` while
DF is running, then it will miss the initial sweep that loads all the module
scripts and any ``onStateChange`` handlers the script may want to register will
not be registered until the script is loaded via some means, either by running
it or loading it as a module. If you just added new scripts that you want to
load so they can attach their ``onStateChange`` handlers, run ``enable`` without
parameters or call ``:lua require('script-manager').reload()`` to scan and load
all script modules.
Save init script Save init script
================ ================

@ -45,6 +45,9 @@ function reload()
end end
function list() function list()
-- call reload every time we list to make sure we get scripts that have
-- just been added
reload()
for name,fn in pairs(enabled_map) do for name,fn in pairs(enabled_map) do
print(('%20s\t%-3s'):format(name..':', fn() and 'on' or 'off')) print(('%20s\t%-3s'):format(name..':', fn() and 'on' or 'off'))
end end