unitdefs_post.lua -> ??? -> gadgets

unitdefs_post.lua -> ??? -> gadgets

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

Moderator: Moderators

Post Reply
User avatar
Rafal99
Posts: 162
Joined: 14 Jan 2006, 04:09

unitdefs_post.lua -> ??? -> gadgets

Post by Rafal99 »

So I want some custom variables in my lua unitdefs files, that will have a form of a nested lua tables. Then I will read them in unitdefs_post.lua to check if they are correct etc. Then I want to somewhat make them available in a gadget which will trigger appropriate game logic. How to do it?

The problem is that unitDef.customParams only takes strings and ignores tables. While Spring.SetGameRulesParam() only takes number values so it is even worse.

So the question is:
Is there any part of lua environment which stays untouched between unitdefs_post.lua and gadget initialization? I know Spring loads all defs and then exports them back during that time, but does it overwrite whole lua state or is there some part where i could safely store my data in a variables?
Perhaps any other easy way?

If all of the above is not possible then I will probably have to butcher (serialize) the nested tables into string(s) stored in unitDef.customParams and then later read them in the gadget which will be another source of problems in itself...
User avatar
jK
Spring Developer
Posts: 2299
Joined: 28 Jun 2007, 07:30

Re: unitdefs_post.lua -> ??? -> gadgets

Post by jK »

Lua objects like tables aren't c++-like structures that can be constructor and handled separately from a Lua State/Context. So when the LuaState gets destructed the table is also. So the engine can't internally `save` Lua objects (except creating its own uber-complexe containers for them).
So it is quite unlikely that there will be a functionality as this in the engine in the near future.

Some options to workaround this:
* serialization of customParams (unserialization doesn't have to happen in the gadget itself, it can also be moved into the gadgetHandler etc.)
* running defs.lua in LuaRules again and using its output instead
User avatar
Rafal99
Posts: 162
Joined: 14 Jan 2006, 04:09

Re: unitdefs_post.lua -> ??? -> gadgets

Post by Rafal99 »

Ok, so the whole loading-time LuaState gets destructed, that explains a lot. Thought that maybe a part of it is preserved.

jK wrote:Some options to workaround this:
* serialization of customParams (unserialization doesn't have to happen in the gadget itself, it can also be moved into the gadgetHandler etc.)
* running defs.lua in LuaRules again and using its output instead
I am not sure what to choose. Running same code and parsing same stuff two times seems bad to me, but would be easier and probably more flexible solution.

I could probably use Gadgets\Configs\definemystuffhere.lua instead but I prefer the possibility of having all unit related data in one place. I am even going to move script contants like turret turning speeds to unitdef file.

I will think more about the possibilities. Thanks for help!
User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6242
Joined: 29 Apr 2005, 01:14

Re: unitdefs_post.lua -> ??? -> gadgets

Post by FLOZi »

Would it be totally insane to write a parser to parse string custom params into tables? :shock:
Regret
Posts: 2086
Joined: 18 Aug 2007, 19:04

Re: unitdefs_post.lua -> ??? -> gadgets

Post by Regret »

FLOZi wrote:Would it be totally insane to write a parser to parse string custom params into tables? :shock:
http://lua-users.org/wiki/TableSerialization
User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6242
Joined: 29 Apr 2005, 01:14

Re: unitdefs_post.lua -> ??? -> gadgets

Post by FLOZi »

Regret wrote:
FLOZi wrote:Would it be totally insane to write a parser to parse string custom params into tables? :shock:
http://lua-users.org/wiki/TableSerialization
Ah, +nowisee.
User avatar
Rafal99
Posts: 162
Joined: 14 Jan 2006, 04:09

Re: unitdefs_post.lua -> ??? -> gadgets

Post by Rafal99 »

I have looked there for examples earlier, but eventually I wrote my own simple serializer and deserializer.
It handles just the simple cases, won't work with duplicated / circular / recursive tables but these will not happen in my unitDefs anyway.
The deserialization happens in a file included in gadgets.lua.

I have run into a problem that in Spring-exported UnitDefs original variables can't be modified in any way, but luckily you can add new variales. This means you can't modify UnitDefs.customParams and can't even add variables to it, but you can add new variables to the UnitDef itself, so I made myCustomParams table containing deserialized customParams and then added it to the UnitDefs.
Last edited by Rafal99 on 02 Mar 2011, 23:05, edited 2 times in total.
User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6242
Joined: 29 Apr 2005, 01:14

Re: unitdefs_post.lua -> ??? -> gadgets

Post by FLOZi »

Rafal99 wrote:
I have looked there for examples earlier, but eventually I wrote my own simple serializer and deserializer.
It handles just the simple cases, won't work with duplicated / circular / recursive tables but these will not happen in my unitDefs anyway.
The deserialization happens in a file included in gadgets.lua.

I have run into a problem that in Spring-exported UnitDefs original variables can't be modified in any way, but luckily you can add new variales. This means you can't modify UnitDefs.customParams and can't even add variables to it, but you can add new variables to the UnitDef itself, so I made myCustomParams table containing deserialized customParams and then added it to the UnitDefs.
Could I get a look at your simple serializer/deserializer?

By the sounds of it, I'm doing some of the same stuff you are ("I am even going to move script contants like turret turning speeds to unitdef file."), I guess the end aim is to have just one or two general unit scripts which are controlled via custom params of unitdefs?

I want (eventually) for all S44 tanks to share a script for example, and, perhaps more ambitiously, all mechs in Spiked's new BattleTech mod.
Post Reply

Return to “Lua Scripts”