How to refer to a table inside another table?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
foo0
Prole
Posts: 35
Joined: Sun Apr 27, 2014 10:25 am

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

Post 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.
User avatar
DaedalusYoung
Party member
Posts: 413
Joined: Sun Jul 14, 2013 8:04 pm

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

Post 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
User avatar
Exasperation
Prole
Posts: 11
Joined: Sat Oct 02, 2010 7:11 pm

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

Post 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.
User avatar
szmol96
Citizen
Posts: 51
Joined: Mon Oct 07, 2013 4:24 pm
Location: Horvátkút, Hungary

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

Post by szmol96 »

I've tried "currMapSegment().data" too, to no avail.
All your code are belong to us.
User avatar
moikmellah
Prole
Posts: 12
Joined: Fri Jan 31, 2014 8:31 pm
Location: USA
Contact:

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

Post 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.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 5 guests