[Script] Script loader

Ask for help about creating mods and scripts for Grimrock 2 or share your tips, scripts, tools and assets with other modders here. Warning: forum contains spoilers!
Post Reply
User avatar
JKos
Posts: 464
Joined: Wed Sep 12, 2012 10:03 pm
Location: Finland
Contact:

[Script] Script loader

Post 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"
- LoG Framework 2http://sites.google.com/site/jkoslog2 Define hooks in runtime by entity.name or entity.id + multiple hooks support.
- cloneObject viewtopic.php?f=22&t=8450
NutJob
Posts: 426
Joined: Sun Oct 19, 2014 6:35 pm

Re: [Script] Script loader

Post by NutJob »

Using! This is a highly valuable scripting technique to stay organized and optimized, so use looking at this thread to see it.
Granamir
Posts: 202
Joined: Wed Jan 07, 2015 7:19 pm

Re: [Script] Script loader

Post 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?
User avatar
JKos
Posts: 464
Joined: Wed Sep 12, 2012 10:03 pm
Location: Finland
Contact:

Re: [Script] Script loader

Post 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.
- LoG Framework 2http://sites.google.com/site/jkoslog2 Define hooks in runtime by entity.name or entity.id + multiple hooks support.
- cloneObject viewtopic.php?f=22&t=8450
Granamir
Posts: 202
Joined: Wed Jan 07, 2015 7:19 pm

Re: [Script] Script loader

Post by Granamir »

understood
ty
Post Reply