Page 1 of 1

UnitDefs[ defID ]["custom_param"] question

Posted: 23 Jan 2010, 03:41
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 :)

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

Posted: 23 Jan 2010, 03:43
by SpliFF
local Nature = UnitDefs[ defID ]["customParams"]["merciless"]

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

Posted: 23 Jan 2010, 04:05
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...

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

Posted: 23 Jan 2010, 07:15
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",
},

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

Posted: 23 Jan 2010, 09:26
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

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

Posted: 23 Jan 2010, 09:31
by SpliFF
Not sure but perhaps the FBI value is actually a string

if ( Nature == "1") ...

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

Posted: 23 Jan 2010, 09:37
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?

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

Posted: 23 Jan 2010, 09:52
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.

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

Posted: 23 Jan 2010, 13:44
by Forboding Angel
CAn you actually define custom params with FBI? I was under the impression that that was luadefs only.

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

Posted: 23 Jan 2010, 14:47
by Argh
Yes, absolutely.

[customParams]
{
myTDFparam=blah;
}

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

Posted: 23 Jan 2010, 15:03
by Tobi
Btw note that

Code: Select all

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

Code: Select all

UnitDefs[ defID ].customParams.merciless