Page 1 of 1

Not sure what is wrong with my table

Posted: Sun Apr 29, 2012 3:20 pm
by blackout_alex
Hi all,

So I have been working on some code for bullets, but something is wrong with the table that contains the bullets. I initialize it in love.load(), but it will not allow me to modify any of the variables from love.update(). Any ideas?

Here is the code:
pigeon.love
(32.61 KiB) Downloaded 266 times

Re: Not sure what is wrong with my table

Posted: Sun Apr 29, 2012 3:43 pm
by zeitlos
Hi,

In the love.keypressed part you don´t initialize the table that contains the position right.
Change

Code: Select all

table.insert(bullets, {playerX + 150,playerY})
to

Code: Select all

table.insert(bullets, { bx = playerX + 150, by = playerY})
Also, to make the bullets really move you need to alter love.draw from

Code: Select all

love.graphics.draw(lazar, bullets.bx, bullets.by)
to

Code: Select all

love.graphics.draw(lazar, v.bx, v.by)
In your love.load you can also just create the bullet table as empty.

Code: Select all

bullets = {}

Re: Not sure what is wrong with my table

Posted: Sun Apr 29, 2012 4:48 pm
by Jasoco
I like to avoid using table.insert and table.remove whenever I can since they actually slow things down a bit since insert has to check the entire table for an opening and remove has to rearrange the table when they are used which gets pretty slow when used a lot at once. I guess it all depends on what you need to use it for.

Re: Not sure what is wrong with my table

Posted: Sun Apr 29, 2012 7:39 pm
by sanjiv
Jasoco wrote:I like to avoid using table.insert and table.remove whenever I can since they actually slow things down a bit since insert has to check the entire table for an opening and remove has to rearrange the table when they are used which gets pretty slow when used a lot at once. I guess it all depends on what you need to use it for.
For table.insert, is this still a problem if you're not specifying the position? (In that case I thought the inserted item simply be tacked on at the end)

Re: Not sure what is wrong with my table

Posted: Mon Apr 30, 2012 4:33 am
by Jasoco
sanjiv wrote:
Jasoco wrote:I like to avoid using table.insert and table.remove whenever I can since they actually slow things down a bit since insert has to check the entire table for an opening and remove has to rearrange the table when they are used which gets pretty slow when used a lot at once. I guess it all depends on what you need to use it for.
For table.insert, is this still a problem if you're not specifying the position? (In that case I thought the inserted item simply be tacked on at the end)
When you don't supply a position yes, it will try to find the first available numerical position. I believe it will still try to find an open space before putting it at the end. But maybe someone can clarify this for me. Like if an array has 1, 2, 3, 5, 6, 9 taken, will insert put it at 4 or at 10?

Re: Not sure what is wrong with my table

Posted: Mon Apr 30, 2012 9:11 am
by Nixola
It puts it at 4

Re: Not sure what is wrong with my table

Posted: Mon Apr 30, 2012 9:57 am
by Robin
If your tables are small, you don't need to worry about it. Only if you have very large tables, for example:

Code: Select all

local t = {}
for i = 1, 1000000 do
    table.insert(t, somefunc())
end
will take roughly 1000000x as much time as:

Code: Select all

local t = {}
for i = 1, 1000000 do
    t[i] = somefunc()
end
But for small tables, other things will affect performance much more than how you fill the table.