It is currently 10 May 2025, 22:42
   
Text Size

nil is nil, right?

Moderator: CCGHQ Admins

nil is nil, right?

Postby Eglin » 21 Mar 2012, 01:08

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
User avatar
Eglin
Programmer
 
Posts: 195
Joined: 01 Mar 2012, 14:44
Has thanked: 39 times
Been thanked: 22 times

Re: nil is nil, right?

Postby thefiremind » 21 Mar 2012, 09:10

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.
< Former DotP 2012/2013/2014 modder >
Currently busy with life...
User avatar
thefiremind
Programmer
 
Posts: 3515
Joined: 07 Nov 2011, 10:55
Has thanked: 118 times
Been thanked: 722 times

Re: nil is nil, right?

Postby Eglin » 21 Mar 2012, 17:59

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.
User avatar
Eglin
Programmer
 
Posts: 195
Joined: 01 Mar 2012, 14:44
Has thanked: 39 times
Been thanked: 22 times

Re: nil is nil, right?

Postby nabeshin » 23 Mar 2012, 17:21

not so confusing, c-part of game wait from your lua-script SOME important...
and you disappointed her.
User avatar
nabeshin
 
Posts: 207
Joined: 27 Jun 2011, 20:07
Has thanked: 5 times
Been thanked: 31 times


Return to Programming Talk

Who is online

Users browsing this forum: No registered users and 1 guest


Who is online

In total there is 1 user online :: 0 registered, 0 hidden and 1 guest (based on users active over the past 10 minutes)
Most users ever online was 4143 on 23 Jan 2024, 08:21

Users browsing this forum: No registered users and 1 guest

Login Form