Page 1 of 1

Inserting variable into table name

Posted: Wed Jul 17, 2013 12:14 pm
by Ultra7
Hi, I'm really new to Löve and programming and i have a really small problem (i think). My code:

Code: Select all

grid = {}
blocks = {}
function grid.spawn( x,y,id )
	blocks = {xy = {id = id}}
end
My prblem is that i want to place a variable inside a table name i.e. when function grid.spawn(1,2,3) gets called in love.load function, it should create a table inside "blocks" named "x1y2" and it would have a variable id = 3. I spent an hour trying to make it work or find on the internet how to do it, I failed and now i'm here posting this willing that someone will help. Thanks in advice.
P.S. Sorry if bad English

Re: Inserting variable into table name

Posted: Wed Jul 17, 2013 12:57 pm
by Robin
That would be:

Code: Select all

grid = {}
blocks = {}
function grid.spawn( x,y,id )
	blocks[x] = blocks[x] or {}
	blocks[x][y] = {id = id}
end
Now, I changed it a bit, because just concatenating x and y has problems, for example: x=11, y=3 would be the same as x=1, y=13.

The way I did it makes things easier to draw and update as well.

Re: Inserting variable into table name

Posted: Wed Jul 17, 2013 1:38 pm
by Ultra7
Thanks for a fast reply. I need to make a code that makes a table called "x1y2" (with letters x and y in the name). Why I need this? I want to make a sideway scrolling 2d platformer sort of that uses tiled maps. Map files would be made of a lot of grid.spawn(x,y,id) functions, when you load a map, it makes a lot of tables inside "blocks" table that have a "id" value in them. Draw function checks id's inside tables that are inside "blocks" and draws a corresponding image to the id in the coordinates of that table. Character movement is not gridlocked, player moves freely (movement allready done). Collisions would work like that: the collisions code gets characters coordinates, divides them by 32, floors them (by doing this the code gets the block that the character is standing in), checks if there's a block bellow that has a id > 0. If yes it doesn't let the character y coordinates get bellow the blocks that the character is standing on y * 32. THis will work on all directions. This is the way I understand of doing tiled game. Please, if you post a code, explain how it works.

Re: Inserting variable into table name

Posted: Wed Jul 17, 2013 1:49 pm
by micha
Ultra7 wrote:I need to make a code that makes a table called "x1y2" (with letters x and y in the name).
This is actually not a good idea. You would get a call like this:

Code: Select all

blocks.x2y23.id
  -- or
blocks['x2y23'].id
Robin's code would make a call like this

Code: Select all

blocks[2][23].id
This is much better, because the x and y-values are numbers and Robin's solution makes you treat them as numbers. Your solution means that you have to convert them into a string first.
Another aspect is that tables are Lua's implementation of arrays. A level is a matrix of tiles and thus an array of arrays. By using the index ['x2y23'] instead of [2][23] you use this in a rather unnatural way. You impose your own ordering instead of a natural number-based one.
Ultra7 wrote:This is the way I understand of doing tiled game.
As far as I see, this is all correct.

Re: Inserting variable into table name

Posted: Wed Jul 17, 2013 4:37 pm
by Ultra7
Really thank you for all of your help. Helped me a lot. :awesome: