copy paste to your dungeon and name it as loader. (or load it from external file)
loader
Code: Select all
-- spawn floor trigger at the party starting location
spawn('floor_trigger',party.level,party.x,party.y,1,party.elevation,'loader_init')
loader_init.floortrigger:addConnector('onActivate',self.go.id,'init')
loader_init.floortrigger:setDisableSelf(true)
function allEntsByComponent(componentName,callback)
for i=1, Dungeon.getMaxLevels() do
mapEntsByComponent(i,componentName,callback)
end
end
function mapEntsByComponent(mapIndex,componentName,callback)
for ent in Dungeon.getMap(mapIndex):allEntities() do
if ent[componentName] then
callback(ent)
end
end
end
-- find all script entities and execute autoexec-method if it exists
function init()
local callback = function(ent)
if ent.script.autoexec and type(ent.script.autoexec) == 'function' then
ent.script:autoexec()
end
end
self.allEntsByComponent('script',callback)
end
function loadScript(entityName,scriptPath)
if findEntity(entityName) then
Console.warn('Entity named '..entityName..' already exists')
return false
end
local scr = spawn('script_entity',party.level,1,1,1,1,entityName)
scr.script:loadFile(scriptPath)
end
some_script_entity
Code: Select all
local ld = loader.script
--will load mod_assets/scripts/my_settings.lua as a my_lib script entity
ld.loadScript('my_setting','mod_assets/scripts/my_settings.lua')
ld.loadScript('my_lib','mod_assets/scripts/my_lib.lua')
ld.loadScript('other_lib','mod_assets/scripts/some_other_lib.lua')
-- you can load all your external scripts here
Code: Select all
-- autoexec is called automatically once all entities are loaded
-- so no more load order issues.
-- You can add autoexec function to any script and it will get called.
function autoexec()
my_lib.callMe()
end
-- so this wont work
-- my_lib.callMe()
-- because my_lib enti
And as a bonus this script has 2 useful methods:
Code: Select all
-- finds all entities which do have component "componentName" and executes the callback function
-- for each of them, callback receives the found entity as an argument
loader.script.allEntsByComponent(componentName,callback)
-- same for specific map entities
loader.script.mapEntsByComponent(componentName,callback)