Page 1 of 1

nil is nil, right?

PostPosted: 21 Mar 2012, 01:08
by Eglin
Hi guys,

I'm having a little trouble understanding something.

Consider the following:
Code: Select all
store = Storage(Object)
store.set("bar")
if store.get("foo") then print "bar" end
This will print "bar" all day long.

However, if I try to use the same mechanism in a trigger, I see different behavior:
Code: Select all
    <TRIGGER value="DOESN'T MATTER WHICH">
    return (Storage(Object()).get("target") ~= nil)
    </TRIGGER>
will fire, but:
Code: Select all
    <TRIGGER value="DOESN'T MATTER WHICH">
    return (Storage(Object()).get("target"))
    </TRIGGER>
will not. Why? It seems like the .get should evaluate to non-false/non-nil and therefore the expression should be evaluated as true, right? What gives? It's bad enough that the game-code is geared toward returning 0 as a result to many logical functions (like TEST/TESTANY/etc) when 0 == true in Lua. But I'm having a hard time wrapping my head around this one (and lost an hour to trying to debug my failing trigger, to boot!)

My only guess is that the game's logic in this case considers anything != true to be false, which is a fallacy. If anyone can shed a little light on this, I'd be most grateful.

Thanks in advance,
Eglin

Re: nil is nil, right?

PostPosted: 21 Mar 2012, 09:10
by thefiremind
I often forget to add "~= 0" after the tests when I write code quickly, and this always leads to code that doesn't work. I'm not an expert in LUA and I learned the most by observing other code, so I don't know the reason. I can only imagine that LUA always needs explicit evaluations.

Re: nil is nil, right?

PostPosted: 21 Mar 2012, 17:59
by Eglin
thefiremind wrote:I often forget to add "~= 0" after the tests when I write code quickly, and this always leads to code that doesn't work. I'm not an expert in LUA and I learned the most by observing other code, so I don't know the reason. I can only imagine that LUA always needs explicit evaluations.
Everything I've read about the language says that false is defined as nil and true is defined as not false. It's why "bar" prints above. What we're seeing seems to be an inconsistency, which is why it's so confusing.

Re: nil is nil, right?

PostPosted: 23 Mar 2012, 17:21
by nabeshin
not so confusing, c-part of game wait from your lua-script SOME important...
and you disappointed her.