## Tables

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
LÖVER
Prole
Posts: 10
Joined: Mon Jan 05, 2009 9:09 pm

### Tables

Would someone be so kind and explain how to use Lua tables with love so I can spawn more than one of the same sprite or even post a tiny example of how to accomplish this? I would be very thankful

rude
Posts: 1051
Joined: Mon Feb 04, 2008 3:58 pm
Location: Oslo, Norway

### Re: Tables

Attachments
tables.love

LÖVER
Prole
Posts: 10
Joined: Mon Jan 05, 2009 9:09 pm

### Re: Tables

Thank you, this helps a lot but I've run into another problem. Mostly because I have found almost nothing to help with the learning of Lua other than just diving into it and looking at other projects. Most of them are uncommented and use bad variable naming conventions making it very frustrating to figure out whats going on. Anyways!

I understand how the tables are working. But from this example I see that to insert more than one of the same sprite into this table you need a simple for loop. And to show the sprites, another for loop. This confuses me on how I would create an unlimited amount of a single sprite (without looping from 1 to 1billion).

A common example would be per mouse click shoot a single bullet. If I fill the table with 100 shots, but the user shoots 101 times you have a problem How would I accomplish this? Per mouse click loop a bullet shoot according to a 1 second timer? Or is there an easier way?

With that being said, can someone post a decent link to learning lua besides the lua docs? That doesn't help with seeing how syntax is set up, functions are used, tables, etc...

Kaze
Party member
Posts: 189
Joined: Sat Jul 19, 2008 4:39 pm
Location: Dublin, Ireland

### Re: Tables

LÖVER wrote: With that being said, can someone post a decent link to learning lua besides the lua docs? That doesn't help with seeing how syntax is set up, functions are used, tables, etc...

LÖVER wrote:A common example would be per mouse click shoot a single bullet. If I fill the table with 100 shots, but the user shoots 101 times you have a problem
This didn't quite make sense to me, when you shoot - wouldn't it be better to add the "shot" to the table?

table.insert inserts a value into the table, at the end of it.

Code: Select all

tab = { "a", "b" }
table.insert(tab, "c") -- tab is now { "a", "b", "c" }
tab[4] = "d" -- tab is now { "a", "b", "c", "d" }


LÖVER
Prole
Posts: 10
Joined: Mon Jan 05, 2009 9:09 pm

### Re: Tables

Alright I think I understand tables now. Basically a "mega array", it's hard to wrap my head around after programming with such strict rules in other languages. So to be sure I am on the right track I want something like:

Code: Select all

--Mostly pseudocode
if shootBullet is true then
table.insert(bulletsTable, bulletImage)
love.graphics.draw(bulletImage, x, y, rotation)

if bullet x and y are > viewable area then
table.remove(bulletImage, 0) --0 because tables start storing at index 0
I am assuming table has a remove function, otherwise my table will be massive and end up taking up large amounts of memory.

Thanks for the help so far!

bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

### Re: Tables

LÖVER wrote: table.remove(bulletImage, 0) --0 because tables start storing at index 0
Guess what, not Lua! Tables start at 1. (The remove function does indeed exist).
Another thing you might need to think about, are you sure that the first one going off screen is always the first one added?
This pseudo-code thingy "if bullet x and y are > viewable area then" should be within a for loop, looping all variables (use pairs() or ipairs()), so you know which bullet leaves the viewable area and which to remove. (Though it does spawn the next problem, indexes change)

Kaze
Party member
Posts: 189
Joined: Sat Jul 19, 2008 4:39 pm
Location: Dublin, Ireland

### Re: Tables

LÖVER wrote:Alright I think I understand tables now. Basically a "mega array", it's hard to wrap my head around after programming with such strict rules in other languages. So to be sure I am on the right track I want something like:

Code: Select all

--Mostly pseudocode
if shootBullet is true then
table.insert(bulletsTable, bulletImage)
love.graphics.draw(bulletImage, x, y, rotation)

if bullet x and y are > viewable area then
table.remove(bulletImage, 0) --0 because tables start storing at index 0
I am assuming table has a remove function, otherwise my table will be massive and end up taking up large amounts of memory.

Thanks for the help so far!
table.remove will remove the value from the table, return the value, and also rearranges the table so that the indexes go in numerical order.

Code: Select all

tab = { "a", "b" }
tab[1] = nil
print( tab[1] ) -- "nil"

tab = { "a", "b" }
table.remove( tab, 1 ) -- returns "a"
print(tab[1]) -- "b"

table.remove can also be used like a "pop" function, to remove and return the last element in the table: table.remove( tab )

osgeld
Party member
Posts: 303
Joined: Sun Nov 23, 2008 10:13 pm

### Re: Tables

bartbes wrote: Guess what, not Lua! Tables start at 1
this still drives me bonkers from time to time

LÖVER
Prole
Posts: 10
Joined: Mon Jan 05, 2009 9:09 pm

### Re: Tables

bartbes wrote: Guess what, not Lua! Tables start at 1.
Seriously? What idiot decided to do that... Computers count in binary starting at 0 what makes Lua so special? Counting in base 10, for us, we might as well count like 9, 1, 2, 3, etc...

EDIT

Ok I am unsure why this doesn't work, would someone mind taking a look? Basically, the bullet sprite will flash for each mouse click but as I read through this code I don't see any reason at all as to why it would disappear almost instantly after the mouse click.

Code: Select all

function mousepressed()
mouseY = love.mouse.getY()
mouseX = love.mouse.getX()
shootBullets()
end

function shootBullets()
xDis = mouseX - x
yDis = mouseY - y

rot = math.atan2(yDis, xDis) + math.pi
rotation = math.atan2(yDis, xDis) * (180 / math.pi) - 90
posX = posX - (math.cos(rot) * 1.2)
posY = posY - (math.sin(rot) * 1.2)

table.insert(bullets, bullet)
love.graphics.draw(bullet, posX, posY, rotation)
end

Thanks again for all the help, I think once I wrap my head around this table concept I should be able to actually finish this thing lol

subrime
Citizen
Posts: 76
Joined: Thu Nov 13, 2008 6:18 pm
Location: Australia

### Re: Tables

The people designing tables in lua were not idiots. Your problem is in thinking of them like an indexed chunk of memory (like in c) when they are actually much more sophisticated.

Lua implements associative arrays, meaning the keys are not restricted to consecutive integers starting at zero - they can be anything. Keys can be a string, a floating point number, another table, even a function... so if you really want, there is nothing to stop you using t[0] as your first element - but if that's all you ever use you will miss one of the most awesome parts of lua.

### Who is online

Users browsing this forum: Bing [Bot] and 13 guests