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
Before you make a thread asking for help, read this.
Post Reply
blackout_alex
Prole
Posts: 9
Joined: Thu Apr 26, 2012 9:07 pm

Not sure what is wrong with my table

Post by blackout_alex » Sun Apr 29, 2012 3:20 pm

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 55 times

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

Re: Not sure what is wrong with my table

Post by zeitlos » Sun Apr 29, 2012 3:43 pm

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

User avatar
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

Post by Jasoco » Sun Apr 29, 2012 4:48 pm

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.

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

Re: Not sure what is wrong with my table

Post by sanjiv » Sun Apr 29, 2012 7:39 pm

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)

User avatar
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

Post by Jasoco » Mon Apr 30, 2012 4:33 am

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?

User avatar
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

Post by Nixola » Mon Apr 30, 2012 9:11 am

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

User avatar
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

Post by Robin » Mon Apr 30, 2012 9:57 am

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.
Help us help you: attach a .love.

Post Reply

Who is online

Users browsing this forum: No registered users and 16 guests