Tables
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Re: Tables
- Attachments
-
- tables.love
- (2.5 KiB) Downloaded 299 times
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...
Thanks from your LÖVER
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
Read Programming in Lua.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
This didn't quite make sense to me, when you shoot - wouldn't it be better to add the "shot" to the table?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
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
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:
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!
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
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
Guess what, not Lua! Tables start at 1. (The remove function does indeed exist).LÖVER wrote: table.remove(bulletImage, 0) --0 because tables start storing at index 0
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
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.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:I am assuming table has a remove function, otherwise my table will be massive and end up taking up large amounts of memory.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
Thanks for the help so far!
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"
Re: Tables
this still drives me bonkers from time to timebartbes wrote: Guess what, not Lua! Tables start at 1
Re: Tables
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...bartbes wrote: Guess what, not Lua! Tables start at 1.
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
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.
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: Ahrefs [Bot], Bing [Bot] and 2 guests