Page 2 of 2

Re: How to refer to a table inside another table?

Posted: Tue May 13, 2014 6:57 pm
by foo0
In such a case, which is common, just add "stop" before the error message:
"stop attempt to index local 'image' (a nil value)"
Now you know what to do. Apply this schema to every error message you will get. Most programmers do that trick, it's faster than posting every error message and waiting for answer.

Don't be mad I'm a dush, karma will pay me back when I'll be in a need of help.

Re: How to refer to a table inside another table?

Posted: Tue May 13, 2014 10:06 pm
by DaedalusYoung
Exasperation wrote:Wait a second... this:

Code: Select all

not nil
returns true while this:

Code: Select all

nil == false
returns false.

So, since the two options behave differently for the same input, shouldn't we use the one that provides the desired behavior in whatever particular situation we're using it?
True and false are the only possible values of the boolean type. Nil is of type nil. Therefore, of course, nil ~= false. In fact, nil just means 'different from any other value'[1].

Generally, when writing code like 'if x == false', you don't actually care if x is false (or nil or any other value), you only want to know if x is not true, so it's better to just use 'if not x'. It's easier to debug too, not having to reverse the statement in your head (causing paradoxical "the statement 'x == false' is not true" situations).

So yes, there is a difference between false and nil, but the only time you need to know this in your code is if you really need to know if x is declared or not, and for that you would check if it's nil. And how often does that really happen? There's never* any need to explicitly check if x is true or false.

*Nothing I can think of anyway

Re: How to refer to a table inside another table?

Posted: Wed May 14, 2014 3:15 am
by Exasperation
I pretty regularly use functions with optional parameters in Lua; the following is a perfectly reasonable thing to do:

Code: Select all

foo = function (bar, optional_flag)
    apply_bar(bar)
    if optional_flag == false then
        do_something()
    end
end

foo(bat)
and I don't think it's less readable than this equivalent:

Code: Select all

foo = function (bar, optional_flag)
    apply_bar(bar)
    if type(optional_flag) == "boolean" and not optional_flag then
        do_something()
    end
end

foo(bat)
re: the original topic, it looks like the if statement in currMapSegment() is failing to match any of the values from ipairs(map), but why isn't obvious from what you've posted.

Re: How to refer to a table inside another table?

Posted: Wed May 14, 2014 1:48 pm
by szmol96
I've tried "currMapSegment().data" too, to no avail.

Re: How to refer to a table inside another table?

Posted: Wed May 14, 2014 4:38 pm
by moikmellah
The segments in your map table are indexed as 'a1', 'b1', etc., so using ipairs() to iterate over them doesn't work - the logic in your for loop is never actually executed, so currentMapSegment() always returns nil. Try using pairs() instead of ipairs() - pairs() will iterate over all key/value pairs in a table regardless of key type, whereas ipairs() will only iterate over integer indexes from 1 to #table.

Also, not really an error, but for readability's sake I'd avoid variable names that are the same as the function they're declared in (or any function, really) - maybe change the function name to getCurrentMapSegment(). Makes it a bit more descriptive, and helps to avoid confusion with error messages.