Page 1 of 2

Tables

Posted: Mon Jan 12, 2009 3:05 am
by LÖVER
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 :)

Re: Tables

Posted: Mon Jan 12, 2009 3:33 am
by rude
:neko:

Re: Tables

Posted: Mon Jan 12, 2009 9:06 pm
by LÖVER
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...

Thanks from your LÖVER

Re: Tables

Posted: Mon Jan 12, 2009 9:31 pm
by Kaze
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...

Thanks from your LÖVER
Read Programming in Lua.
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" }

Re: Tables

Posted: Tue Jan 13, 2009 5:27 am
by LÖVER
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!

Re: Tables

Posted: Tue Jan 13, 2009 7:04 am
by bartbes
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)

Re: Tables

Posted: Tue Jan 13, 2009 2:07 pm
by Kaze
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 )

Re: Tables

Posted: Wed Jan 14, 2009 12:15 am
by osgeld
bartbes wrote: Guess what, not Lua! Tables start at 1
this still drives me bonkers from time to time

Re: Tables

Posted: Wed Jan 14, 2009 5:58 pm
by LÖVER
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 :P

Re: Tables

Posted: Fri Jan 16, 2009 5:54 am
by subrime
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.