Jump to content

trigger.misc.getUserFlag() not evaluating Boolean values


Pizzicato

Recommended Posts

I discovered a weird oddity while debugging a Lua script tonight. Specifically, with a flag set to true (in this case Flag 1) the following condition doesn't fire:

trigger.action.setUserFlag( 1, true )

if trigger.misc.getUserFlag(1) == true then 
	env.info( "This code never runs" )
end

If I change the true to 1, however, it works fine, e.g.

trigger.action.setUserFlag( 1, true )

if trigger.misc.getUserFlag(1) == 1 then 
	env.info( "This code runs just fine" )
end

 

i7-7700K @ 4.9Ghz | 16Gb DDR4 @ 3200Mhz | MSI Z270 Gaming M7 | MSI GeForce GTX 1080ti Gaming X | Win 10 Home | Thrustmaster Warthog | MFG Crosswind pedals | Oculus Rift S

Link to comment
Share on other sites

On 11/12/2021 at 5:14 AM, Pizzicato said:

I discovered a weird oddity while debugging a Lua script tonight. Specifically, with a flag set to true (in this case Flag 1) the following condition doesn't fire:...

Hi, it is normal, as getUserFlag() gets the numerical value of a flag (0, which is false flag, or 1, 2, 3 etc ... which is true flag). setUserFlag() is different as you can input a value, or a boolean state (which in fact converts to value, 0 for false and 1 for true).

"if trigger.misc.getUserFlag(1) == 0 then" will check if flag 1 is false,

"if trigger.misc.getUserFlag(1) > 0 then" will check if flag 1 is true, whatever the positive value.

Link to comment
Share on other sites

Thanks Toutenglisse. That makes sense.

Given that logic, though, shouldn't trigger.misc.getUserFlag(1) == true evaluate to true for any positive, non-zero value? Lua isn't a type-safe language, so true and positive, non-zero values should evaluate as exactly the same, right?

(I'm totally open to possibility that I'm missing something here, but I'm not sure what).

i7-7700K @ 4.9Ghz | 16Gb DDR4 @ 3200Mhz | MSI Z270 Gaming M7 | MSI GeForce GTX 1080ti Gaming X | Win 10 Home | Thrustmaster Warthog | MFG Crosswind pedals | Oculus Rift S

Link to comment
Share on other sites

36 minutes ago, Pizzicato said:

...shouldn't trigger.misc.getUserFlag(1) == true evaluate to true for any positive, non-zero value?...

As it returns a number ("Return Value:number" in wiki) it can't work with other then number.

Edit : you can ask if a Unit is true, means does it exist, but you can't ask if a number is true, it's not a question that scripting engine will "understand". So in this case you can only ask if a number (what function returns) is equal to a number, or greater than a number etc...


Edited by toutenglisse
  • Like 1
  • Thanks 1
Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...