creating new units in unitdefs_post.lua

creating new units in unitdefs_post.lua

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

Moderator: Moderators

Post Reply
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

creating new units in unitdefs_post.lua

Post by knorke »

I want to create new unitDefs in unitdefs_post.lua

Just doing this:

Code: Select all

newunit.name  = "testunit"
newunit.objectname  = "cube.s3o"
newunit.script = "empty.lua"
UnitDefs["testunit"] = newunit
does not work:
/give testunit
testunit is not a valid unitname


If I add something on top to copy every tag from an existing unitdef like this:

Code: Select all

newunit = {}
ud = UnitDefs ["tpthulsa"]
for key,value in pairs(ud) do
	Spring.Echo(key,value)
	newunit[key]=value
end
newunit.name               = "testunit"
newunit.objectname         = "cube.s3o"
newunit.script = "empty.lua"
UnitDefs["testunit"] = newunit
Then it works.

So I thought that maybe name, objectname & script alone are not enough to define a valid unitDef.

But this unitdef works, when saved as units\cube.lua:

Code: Select all

local unitName = "cube"
local unitDef = {
	name = "Cube Unit",
	objectName = "cube.s3o",
	script = "empty.lua",
}
return lowerkeys({ [unitName] = unitDef })
Notice it also only uses name, objectname & script.

So what is going on there?
Are the rules for unitdefs_post just more strict?
Or something else?
User avatar
quantum
Posts: 590
Joined: 19 Sep 2006, 22:48

Re: creating new units in unitdefs_post.lua

Post by quantum »

It looks like it adds the "filename" field. You can check here: https://github.com/spring/spring/blob/m ... itdefs.lua
User avatar
smoth
Posts: 22309
Joined: 13 Jan 2005, 00:46

Re: creating new units in unitdefs_post.lua

Post by smoth »

Look at gundam's feature def pots there is a bare minimal unit defined when I create units from features
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: creating new units in unitdefs_post.lua

Post by knorke »

thanks, adding
newunit.filename = "xxx"
makes it work. seems like a bug that spring requires the filename to be present even if the unitdef is created in unitdefs_post?
User avatar
smoth
Posts: 22309
Joined: 13 Jan 2005, 00:46

Re: creating new units in unitdefs_post.lua

Post by smoth »

Filename?
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: creating new units in unitdefs_post.lua

Post by knorke »

yes.

Code: Select all

newunit.name = "testunit"
newunit.objectname  = "cube.s3o"
newunit.script = "empty.lua"
newunit.filename = "blabla nonsense blablabla" <- is required due to how unitdefs are read
UnitDefs["testunit"] = newunit
User avatar
smoth
Posts: 22309
Joined: 13 Jan 2005, 00:46

Re: creating new units in unitdefs_post.lua

Post by smoth »

honestly dunno.
Post Reply

Return to “Lua Scripts”