UnitDefs[ defID ]["custom_param"] question

UnitDefs[ defID ]["custom_param"] question

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

Moderator: Moderators

Post Reply
Voidranaut
Posts: 47
Joined: 20 Dec 2009, 21:16

UnitDefs[ defID ]["custom_param"] question

Post by Voidranaut »

I have made a widget with the following function inside of it

Code: Select all

function widget:CommandNotify(cmdID) 
	echo( "got here" )
	local unitID = Spring.GetSelectedUnits()[1]
	if(not unitID) then
		return
	end
	local defID = Spring.GetUnitDefID(unitID)
	local Nature = UnitDefs[ defID ]["merciless"]
	if( Nature == 1) then
		Spring.PlaySoundFile("sounds/Regis_ultamate_fight_full.wav")
	end

	echo( unitID )
	echo( defID )
	echo( Nature )

end
Probleam: as you can see the audio is called if Nature == 1 ... and I assumed that would be the case if the units fbi file had merciless=1; inside of it... but when the echo command reaches Nature it returns nil

question: is the UnitDefs[ defID ]["merciless"] not woring because it needs to be in a synced environment? or is there some other extra step I need to take to make my custom param linked to the words in my units fbi file?

any info would be great thanks :)
User avatar
SpliFF
Posts: 1224
Joined: 28 Jul 2008, 06:51

Re: UnitDefs[ defID ]["custom_param"] question

Post by SpliFF »

local Nature = UnitDefs[ defID ]["customParams"]["merciless"]
Voidranaut
Posts: 47
Joined: 20 Dec 2009, 21:16

Re: UnitDefs[ defID ]["custom_param"] question

Post by Voidranaut »

hmmm well the syntex you gave me is deffenetly right but it still is returning nil in the echo (in addition to not calling my audio)

the spelling between the widget word "merciless" and fbi "merciless" is the same (I copy pasted and double checked)

in fbi it says merciless=1; ... I assume it would return the 1 if it was working...

hmmm... I am unsure as to why its not grabbing the value I need...
User avatar
SpliFF
Posts: 1224
Joined: 28 Jul 2008, 06:51

Re: UnitDefs[ defID ]["custom_param"] question

Post by SpliFF »

Then you probably have a issue with your FBI, it should look like this if you're using Lua (and you should be):

Code: Select all

customParams = {
    ProvideTech = "tech1",
    ProvideTechRange = "500",
},
Voidranaut
Posts: 47
Joined: 20 Dec 2009, 21:16

Re: UnitDefs[ defID ]["custom_param"] question

Post by Voidranaut »

SpliFF wrote:Then you probably have a issue with your FBI, it should look like this if you're using Lua (and you should be):

Code: Select all

customParams = {
    ProvideTech = "tech1",
    ProvideTechRange = "500",
},
aaahhh ya that was something I was deffenetly missing

thanks I did not realize my fbi needed the customparams feild

unfortunetly it looks like things are still not working quite right

Code: Select all

function widget:CommandNotify(cmdID) 
	echo( "got here" )
	local unitID = Spring.GetSelectedUnits()[1]
	if(not unitID) then
		echo( "oops" )
		return
	end
	local defID = Spring.GetUnitDefID(unitID)
	local Nature = UnitDefs[ defID ]["customParams"]["merciless"]
	echo( Nature )
	if( 1 == Nature ) then
		echo( "if worked" )
		Spring.PlaySoundFile("sounds/Regis_ultamate_fight_full.wav")
	end
	echo( Nature )

end
basically everything works except

Code: Select all

if( 1 == Nature ) then
		echo( "if worked" )
		Spring.PlaySoundFile("sounds/Regis_ultamate_fight_full.wav")
	end
probleam: I get the echo( Nature )'s that stratle it to show up in the consle though ... wich means it is not seeing 1 == Nature as true...

question: does it think I mean the string "Nature" ? or am I assuming that it would just call the operation from that if statment?

also when I take Spring.PlaySoundFile("sounds/Regis_ultamate_fight_full.wav") and place it outisde of the if statment it works right as expected... so its DEFETLY how I wrote the if that is wrong...

any info on my blunder would be great :)

thanks for being patient with my ignorence
User avatar
SpliFF
Posts: 1224
Joined: 28 Jul 2008, 06:51

Re: UnitDefs[ defID ]["custom_param"] question

Post by SpliFF »

Not sure but perhaps the FBI value is actually a string

if ( Nature == "1") ...
Voidranaut
Posts: 47
Joined: 20 Dec 2009, 21:16

Re: UnitDefs[ defID ]["custom_param"] question

Post by Voidranaut »

OMG thank you

ya 1 in fbi was considered a stirng... wow I honestly did not think to try that good call :)

hay can you type cast in lua? like if I needed to set something up later were I need the number to be used in an equation would be be able to cast it into an int or float? or boolen?
User avatar
SpliFF
Posts: 1224
Joined: 28 Jul 2008, 06:51

Re: UnitDefs[ defID ]["custom_param"] question

Post by SpliFF »

Code: Select all

tonumber (e [, base]) 
tostring (e)
That's about the only typecasts that are relevant since there is no builtin way to convert tables, nils or booleans to either type. Also Lua doesn't have ints, floats or decimal just 'numbers'.

Be careful with booleans, unlike many other languages 0 is not treated as false. Only the builtins nil and false are false in a boolean test.
User avatar
Forboding Angel
Evolution RTS Developer
Posts: 14673
Joined: 17 Nov 2005, 02:43

Re: UnitDefs[ defID ]["custom_param"] question

Post by Forboding Angel »

CAn you actually define custom params with FBI? I was under the impression that that was luadefs only.
User avatar
Argh
Posts: 10920
Joined: 21 Feb 2005, 03:38

Re: UnitDefs[ defID ]["custom_param"] question

Post by Argh »

Yes, absolutely.

[customParams]
{
myTDFparam=blah;
}
Tobi
Spring Developer
Posts: 4598
Joined: 01 Jun 2005, 11:36

Re: UnitDefs[ defID ]["custom_param"] question

Post by Tobi »

Btw note that

Code: Select all

UnitDefs[ defID ]["customParams"]["merciless"]
is exactly identical to

Code: Select all

UnitDefs[ defID ].customParams.merciless
Post Reply

Return to “Lua Scripts”