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:
Not sure what is wrong with my table
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
-
- Prole
- Posts: 9
- Joined: Thu Apr 26, 2012 9:07 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
to
Also, to make the bullets really move you need to alter love.draw from
to
In your love.load you can also just create the bullet table as empty.
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})
Code: Select all
table.insert(bullets, { bx = playerX + 150, by = playerY})
Code: Select all
love.graphics.draw(lazar, bullets.bx, bullets.by)
Code: Select all
love.graphics.draw(lazar, v.bx, v.by)
Code: Select all
bullets = {}
- Jasoco
- Inner party member
- Posts: 3726
- 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.
Re: Not sure what is wrong with my table
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 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.
- Jasoco
- Inner party member
- Posts: 3726
- Joined: Mon Jun 22, 2009 9:35 am
- Location: Pennsylvania, USA
- Contact:
Re: Not sure what is wrong with my table
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?sanjiv wrote: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 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.
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
lt = love.thread
li = love.image
lg = love.graphics
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
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:
will take roughly 1000000x as much time as:
But for small tables, other things will affect performance much more than how you fill the table.
Code: Select all
local t = {}
for i = 1, 1000000 do
table.insert(t, somefunc())
end
Code: Select all
local t = {}
for i = 1, 1000000 do
t[i] = somefunc()
end
Help us help you: attach a .love.
Who is online
Users browsing this forum: Ahrefs [Bot], Google [Bot] and 3 guests