I'm having a weird problem when having lot's of stuff on screen.
I am at the point where i want to add some wicked bullet patterns to my game but i start to see weird issues with object removal.
I added an example. It's really basic and not an actual bullet pattern but it illustrates the problem:
When the screen is full of bullet spam you'll start to see the objects being removed premature in the corners of the screen.
Can anyone explain to me why this happens?
It's totally not what i was expecting and i can't think of a way why it would behave like this.
weird behavior witth lot's of objects on screen
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
weird behavior witth lot's of objects on screen
- Attachments
-
- bullet_test.love
- (35.91 KiB) Downloaded 115 times
- nevon
- Commander of the Circuloids
- Posts: 938
- Joined: Thu Feb 14, 2008 8:25 pm
- Location: Stockholm, Sweden
- Contact:
Re: weird behavior witth lot's of objects on screen
I think it has to do with how you remove your bullets. Once you remove a bullet from the bullets table, the indexes saved in rem_bullets aren't valid anymore. I'm slightly inebriated, but I think this is how you would do it.
Code: Select all
for i, v in ipairs(rem_bullets) do
table.remove(bullets, v-(i-1))
end
- bartbes
- Sex machine
- Posts: 4946
- Joined: Fri Aug 29, 2008 10:35 am
- Location: The Netherlands
- Contact:
Re: weird behavior witth lot's of objects on screen
Or, a commonly used trick is a reverse traversal:
(this only works if rem_bullets is sorted from low to high, this trick has several variants).
Another thing, which would basically be the same as the reverse traversal, is sorting the removal table highest-first.
Code: Select all
for i = #rem_bullets, 1, -1 do
table.remove(bullets, rem_bullets[i])
end
Another thing, which would basically be the same as the reverse traversal, is sorting the removal table highest-first.
Re: weird behavior witth lot's of objects on screen
Yet another way would be to store the bullets in a set instead of an array. To get a set, simply use the object itself as index:
Code: Select all
function love.update(dt)
local rem_bullets = {}
if timer >= 0.01 then
-- as before ...
bullets[b] = b -- instead of table.insert(bullets, b)
end
for _, b in pairs(bullets) do -- notice the pairs instead of ipairs
b.update(dt)
if b.is_off_screen() then
rem_bullets[#rem_bullets+1] = b
end
end
for _, b in ipairs(rem_bullets) do bullets[b] = nil end -- instead of table.remove
end
Re: weird behavior witth lot's of objects on screen
Thanks for all the help!
@nevon: Such a simple fix, adding only -(i-1)
I have no idea why the order at which i remove the bullets is important. Can someone explain?
@nevon: Such a simple fix, adding only -(i-1)
I have no idea why the order at which i remove the bullets is important. Can someone explain?
- bartbes
- Sex machine
- Posts: 4946
- Joined: Fri Aug 29, 2008 10:35 am
- Location: The Netherlands
- Contact:
Re: weird behavior witth lot's of objects on screen
Because if you remove an entry with table.remove all others (after that) shift down, so their indices are now pointing to the wrong value.
- nevon
- Commander of the Circuloids
- Posts: 938
- Joined: Thu Feb 14, 2008 8:25 pm
- Location: Stockholm, Sweden
- Contact:
Re: weird behavior witth lot's of objects on screen
To expand on what Bartbes said, you can visualize it kind of like this:sentry wrote:Thanks for all the help!
@nevon: Such a simple fix, adding only -(i-1)
I have no idea why the order at which i remove the bullets is important. Can someone explain?
Code: Select all
ourFirstTable = {
1 : 'Value 1',
2 : 'Value 2',
3 : 'Value 3',
4 : 'Value 4',
5 : 'Value 5'
}
removeTable = {
1 : 3, --We want to remove 'Value 3' from ourFirstTable ('Value 3' has the index 3)
2 : 4 -- We also want to remove 'Value 4'
}
Code: Select all
ourFirstTable = {
1 : 'Value 1',
2 : 'Value 2',
3 : 'Value 4',
4 : 'Value 5'
}
Code: Select all
ourFirstTable = {
1 : 'Value 1',
2 : 'Value 2',
3 : 'Value 4'
}
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: weird behavior witth lot's of objects on screen
FYI, that's Python. Except for the comments.nevon wrote:<blah>
Help us help you: attach a .love.
- nevon
- Commander of the Circuloids
- Posts: 938
- Joined: Thu Feb 14, 2008 8:25 pm
- Location: Stockholm, Sweden
- Contact:
Re: weird behavior witth lot's of objects on screen
Or Javascript.Robin wrote:FYI, that's Python. Except for the comments.nevon wrote:<blah>
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: weird behavior witth lot's of objects on screen
Pah. Speak not of that language!
Help us help you: attach a .love.
Who is online
Users browsing this forum: Google [Bot] and 12 guests