Problem when deleting go in loop

Need help

Number of rounds of bullets are firing after all bullets deleted I am calling to update wave

bullet.script

function final(self)
    -- data.shots = table with all instance of fired bullets
	if table.maxn(data.shots) == 0 then
		-- update wave
		msg.post('level1:/common/controller', "update_waves")
	end
end

Now, their is a button clicking on it it will delete all bullets and and update wave

funtion to remove bullets in controller.script

function removeBullets()
	-- TASK 1
	for i, v in pairs(data.shots) do
		go.delete(v)
	end
	data.shots = {}
end

Here, I am making data.shots empty after loop but still condition inside final funtion in bullet.script executing several times

I wanted to update wave only once when all instance of fired bullets deleted by clicking button or by bullets destroyed by walls

Instead of handling the logic in the bullet, why don’t you check in the controller? For example instead of sending a message “update_waves” you could send a message like “check_if_update_waves”. Then in the controller you count the number of bullets and act accordingly.

1 Like

I will try it Thanks

I might be misunderstanding something, but couldn’t you also just send the update_waves message at the end of the removeBullets function?

I’m pretty sure the issue you’re running into here is that when an object is deleted, its final function will only be executed at the end of the current frame of execution. This means that if you delete a bunch of objects in a loop in the same frame, their final functions will basically all be added to a list of final functions to run after your update, messages, etc. have been processed.

I think the key is that bullets could be destroyed by walls as well. removeBullets was called on pressing a button.

Right, makes sense. I would go with your way then.

I solved it, I seperated update_wave for wall and when clicking button to remove bullets and and calling update_wave in collision responce message rather than final funtion.

1 Like