Multidimensional array returns nil.

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.
Post Reply
KayleMaster
Party member
Posts: 234
Joined: Mon Aug 29, 2016 8:51 am

Multidimensional array returns nil.

Post by KayleMaster »

Code: Select all

terrain_chunk = {}          
            for i=chunk_width,1,-1 do
              terrain_chunk[i] = {}     
              for j=chunk_height,1,-1 do
                terrain_chunk[i][j] = 1
              end
            end

Code is located in love.load
I followed a tutorial on the love website which I can't seem to find anymore for some reason :|
It was called matrices and multidimensional arrays. I come from game maker and arrays there are handled better.
I have no idea how to do this, any ideas?
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Multidimensional array returns nil.

Post by raidho36 »

What do you mean "returns nil"? Throws an error of nil reference? The code itself works fine. You can get a nil if you reference a key that doesn't exist in the table, but then if the "first bracket" reference resolved to nil, trying to resolve the second one will crash the game, it's the same as trying to do "nil[j]" and obviously it won't work.

You should allocate arrays in normal order, not reverse. GameMaker suffers performance-wise when you do it in normal order because it reallocates the array constantly, and in reverse it does it once. In LUA reallocation goes progressively less often as array grows so it's not of any concern. Plus, because of the way it works, you'll actually get better performance if you allocate it in normal order, one by one.

Just so you know, tables in LUA are hash-tables. It means they can have both integer keys and string keys. For what it's worth, any variable can pass for a key in LUA, even other tables. You can access literal keys with dot notation.

Code: Select all

foo[1] = "bar"
foo["bar"] = 10 
foo.bar = 100 --last two are equivalent
User avatar
zorg
Party member
Posts: 3441
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Multidimensional array returns nil.

Post by zorg »

raidho36 wrote:Just so you know, tables in LUA are hash-tables. It means they can have both integer keys and string keys. For what it's worth, any variable can pass for a key in LUA, even other tables. You can access literal keys with dot notation.
Just tiny nitpicking:

The one value you cannot have as a table index is nil.

Literals, according to at least wikipedia, include numbers and booleans (in lua), not just strings, and in lua, you can't use the dot notation on neither numbers, nor boolean true and false.
You probably meant to say string keys instead.

Finally, i personally don't care about this one, but it's really called lua, not LUA.
LUA => L.U.A. => Lua est pas Un Acronyme. Lua is not an acronym.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Multidimensional array returns nil.

Post by raidho36 »

Well, to be perfectly nitpicky, nil is not a variable.

By "literal" I meant of course the ones that are valid stings for variable names.

Also, pretty sure it's all capitals. It means "moon" in portugese or some shit. Or I think it used to be all caps at some point? Now it says the proper spelling is "Lua".
User avatar
zorg
Party member
Posts: 3441
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Multidimensional array returns nil.

Post by zorg »

Apparently, Roberto always disliked LUA since it's not an acronym, as i've said; it's a simple word, as you said, meaning moon in portuguese, so it's just plain "lua".

Also, a number, a bool, a string literal ain't a variable either; you did say "For what it's worth, any variable can pass for a key in LUA, even other tables."; I meant that your variable can be anything, but nil, even though you can set a variable to it. :3
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
Positive07
Party member
Posts: 1014
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

Re: Multidimensional array returns nil.

Post by Positive07 »

Nitpicking game! I'm in!

It's a name so Lua (no LUA because that is an acronym, not lua because it's used as a proper name so it should be capitalized)

And I'm with zorg here, tables can use number, strings, tables, userdata, functions and booleans as indexes, nil is not a valid index.
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
KayleMaster
Party member
Posts: 234
Joined: Mon Aug 29, 2016 8:51 am

Re: Multidimensional array returns nil.

Post by KayleMaster »

Oops, forgot about the topic!
I actually fixed it, I don't know how, but here's the current code:

Code: Select all

	        
	   terrain_chunk = {}          

            for y = 0,cols do
                local row = {}
                    for x = 0,rows do
                        row[x]=love.math.random(8);
                    end
                terrain_chunk[y]=row;
            end
User avatar
pgimeno
Party member
Posts: 3548
Joined: Sun Oct 18, 2015 2:58 pm

Re: Multidimensional array returns nil.

Post by pgimeno »

Whatever the problem was, it's clear it wasn't in the snippet you copied, because that one worked just fine.
KayleMaster
Party member
Posts: 234
Joined: Mon Aug 29, 2016 8:51 am

Re: Multidimensional array returns nil.

Post by KayleMaster »

Yeah, It was somewhere else in the code.
Post Reply

Who is online

Users browsing this forum: No registered users and 62 guests