Tables

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.
LÖVER
Prole
Posts: 10
Joined: Mon Jan 05, 2009 9:09 pm

Tables

Post by LÖVER » Mon Jan 12, 2009 3:05 am

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 :)

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

Re: Tables

Post by rude » Mon Jan 12, 2009 3:33 am

:neko:
Attachments
tables.love
(2.5 KiB) Downloaded 189 times

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

Re: Tables

Post by LÖVER » Mon Jan 12, 2009 9:06 pm

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

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

Re: Tables

Post by Kaze » Mon Jan 12, 2009 9:31 pm

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" }

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

Re: Tables

Post by LÖVER » Tue Jan 13, 2009 5:27 am

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!

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

Re: Tables

Post by bartbes » Tue Jan 13, 2009 7:04 am

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)

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

Re: Tables

Post by Kaze » Tue Jan 13, 2009 2:07 pm

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 )

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

Re: Tables

Post by osgeld » Wed Jan 14, 2009 12:15 am

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

Post by LÖVER » Wed Jan 14, 2009 5:58 pm

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

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

Re: Tables

Post by subrime » Fri Jan 16, 2009 5:54 am

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.

Post Reply

Who is online

Users browsing this forum: No registered users and 15 guests