Page 1 of 1

[Script] Script loader

Posted: Tue Nov 04, 2014 10:34 pm
by JKos
Here is a helper script which loads script entities dynamically on run time. It also implements autoexec-function similar to Xanathar's GrimQ-library from LoG1. With autoexec you will get rid of any load-order issues, because everything is loaded before autoexec is called.

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
Usage example:
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
some_other_script_entity

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
Feel free to use it.

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)
Edit: fixed a "local bug"

Re: [Script] Script loader

Posted: Wed Nov 05, 2014 7:20 pm
by NutJob
Using! This is a highly valuable scripting technique to stay organized and optimized, so use looking at this thread to see it.

Re: [Script] Script loader

Posted: Thu Feb 26, 2015 4:32 pm
by Granamir
If i understood well this loader load every script and execute it every time u load a save position? Is it usefull to avoid load/save issue with objects with minimalsavestate, right?

Re: [Script] Script loader

Posted: Thu Feb 26, 2015 11:53 pm
by JKos
No, it's for load order issues. Example:

add a script entity to your dungeon
script_entity_1

Code: Select all

script_entity_2.script.test()
and then add script_entity_2

Code: Select all

function test()
   print('works')
end
now try to run your dungeon, you will get an error, with script loader you can fix this by enclosing that mehod call inside autoexec function:
script_entity_1

Code: Select all

function autoexec()
script_entity_2.script.test()
end
now it works.

And of course loader part of this script allows you to load all external scripts within one script entity. Useful if you have many external scripts.

Re: [Script] Script loader

Posted: Fri Feb 27, 2015 1:34 pm
by Granamir
understood
ty