Placing a lua script to have access to UnitDefs and to lua libraries

Placing a lua script to have access to UnitDefs and to lua libraries

Discuss Lua based Spring scripts (LuaUI widgets, mission scripts, gaia scripts, mod-rules scripts, scripted keybindings, etc...)

Moderator: Moderators

Post Reply
filu005
Posts: 2
Joined: 22 Sep 2016, 22:57

Placing a lua script to have access to UnitDefs and to lua libraries

Post by filu005 »

Hi there!
I'm working on a AI optimization project for Spring:1944. I plan to run a genetic algorithm to optimize squad (i.e. unit in s44) production.
I have a problem with placing/loading my lua script. In this script I need simultaneously: io lua library (for savetable io.open) and UnitDefs table AND I need to run it before s44 squad files are loaded (that is before gadgets from 'luarules/gadgets' and before config.lua file from 'luarules\configs\craig').
I tried to place the script in LuaIntro where lua libraries work as expected but UnitDefs table is not yet created. Then I tried to place the script somewhere in 'luarules/gadgets' and load it as first gadget but lua libraries won't be found there (lua error for 'io.open': "attempt to index field 'io' (a nil value)"; 'require' doesn't work either).

So I have two questions:
Why lua libraries don't work with gadgets?
and
Where can I place my script?

Thanks,
Filip
User avatar
Silentwings
Posts: 3720
Joined: 25 Oct 2008, 00:23

Re: Placing a lua script to have access to UnitDefs and to lua libraries

Post by Silentwings »

Why lua libraries don't work with gadgets?
Some do, some don't. I guess the reason gadgets can't use io is because they run synced code that should depend only on the gamestate - for example if you had two AIs that learned differently and created different files and then tried to run them as configs for the same synced gadget in an online game -> desync.

It is a bit of a pain that (unsynced) gadgets can't write to disk. Here's one example of a way to work around it: http://imolarpg.dyndns.org/trac/balates ... _stats.lua and http://imolarpg.dyndns.org/trac/balates ... damage.lua
Where can I place my script?
Afaik the only lua state that has access to both the UnitDefs table and the io library is LuaUI (widgets). However, this lua state is loaded *after* the two LuaRules states (gadgets) are loaded.

Your best bet here is probably to rely on widget<->gadget communication.
filu005
Posts: 2
Joined: 22 Sep 2016, 22:57

Re: Placing a lua script to have access to UnitDefs and to lua libraries

Post by filu005 »

Hey, much thanks for the response!
Unfortunately I need to save to file before LuaRules gadgets are loaded (to modify squads and their production order) so "widget<->gadget communication" solution won't work.
Now I see only one solution for my problem: I put the script in LuaIntro (where lua io library work) and construct UnitDefs-like table on my own. In which place is this table created? I could only found a setup of 'UnitDefNames' and others in 'LuaGadgets/setupdefs.lua'.
Or maybe there is an another solution, to somehow hack the script loadings?

Edit:
Ok, I managed to make over my algorithm so it works with "widget<->gadget communication" solution (game launches only to modify squads scripts and exits right after that. then it launches for the second time, without initial squad setup process but this time with full GA algorithm running).
I have one last following question: how can I turn off a gadget on game exit (essentially how to change a gadget:GetInfo() 'enabled' parameter)?
User avatar
Silentwings
Posts: 3720
Joined: 25 Oct 2008, 00:23

Re: Placing a lua script to have access to UnitDefs and to lua libraries

Post by Silentwings »

When the game exits all gadgets automatically have their gadget:Shutdown() called, and they'll be unloaded automatically after that call is complete.

To forcibly unload a gadget from within itself you can use gadgetHandler:RemoveGadget() wrapper e.g. http://imolarpg.dyndns.org/trac/balates ... I.lua#L233. To have one gadget unload another, you need your gadget to have handler=true in its description, which gives it raw access to the gadgetHandler object and you can then call the RemoveGadget function (defined in gadgets.lua, which may be hiding inside basecontent for your game) directly.

Fwiw, reconstructing the UnitDefs table in pure lua is not really possible - in practice one lua state loads the raw tables & possibly modifies them (see defs.lua in basecontent), then those are fed to the engine, the engine processes them, and then (later, to a different lua state) sends back the UnitDefs table. A small but significant set of key names and values change in this process, so its not really practical to hackily replicate it.
Post Reply

Return to “Lua Scripts”