## Not sure what is wrong with my table

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
blackout_alex
Prole
Posts: 9
Joined: Thu Apr 26, 2012 9:07 pm

### Not sure what is wrong with my table

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

zeitlos
Prole
Posts: 1
Joined: Sun Apr 29, 2012 3:35 pm

### Re: Not sure what is wrong with my table

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 = {}


Jasoco
Inner party member
Posts: 3650
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

### Re: Not sure what is wrong with my table

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.

sanjiv
Citizen
Posts: 85
Joined: Mon Feb 27, 2012 5:11 am

### Re: Not sure what is wrong with my table

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)

Jasoco
Inner party member
Posts: 3650
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

### Re: Not sure what is wrong with my table

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?

Nixola
Inner party member
Posts: 1940
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

### Re: Not sure what is wrong with my table

It puts it at 4
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
li = love.image
lg = love.graphics

Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

### Re: Not sure what is wrong with my table

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.