Managing bullet object lists
Posted: Thu Nov 28, 2019 5:55 am
I'm building a bullet hell shmup system and I'm trying to determine the best way to implement a list of shot objects, since there are going to be a lot of them. I had decided that generating brand new objects for each bullet at the time of firing would be inefficient. So instead I decided to initialize 5000 dead bullet objects in advance.
For bullet creation, I use a function to find the first available dead bullet object for its reactivation.
The reactivation would, however, still consist of re-running the function that initializes its default values. I can include that code too, but it gets more complicated from there.
I realize now that when there are a lot of bullets already in existence, the findDeadBullet function has to iterate through more of the shot list to find a dead one, which may be significant.
I was wondering if there was a different, more efficient way of handling this. One thing I thought of that was an extension on this was to start the bullet list small and increase its size as needed. This might not have a tangible benefit, however, and may in fact introduce hitches when generating the new dead shots for the size increases.
Code: Select all
for i = 1, 5000 do
local obj = ObjShot(0,0) --generates bullet with default coordinates (0,0)
obj.isDelete = true --ensures that its inactive
shot_all[i] = obj --adds to the shot list
end
Code: Select all
function findDeadBullet()
local x = 0
while true do
x = x + 1
if shot_all[x].isDelete == true then --if bullet object is inactive then it is returned for use
break
end
end
return x
end
I realize now that when there are a lot of bullets already in existence, the findDeadBullet function has to iterate through more of the shot list to find a dead one, which may be significant.
I was wondering if there was a different, more efficient way of handling this. One thing I thought of that was an extension on this was to start the bullet list small and increase its size as needed. This might not have a tangible benefit, however, and may in fact introduce hitches when generating the new dead shots for the size increases.