I have a problem that i don’t know that to do about. I tried reading documentation and looking at the examples but I have not found a solution.
I’m trying to figure out how to keep my bullet count on the screen to a certain ammount.
If there is too many bullets on the screen (let’s say more than 100) i would like the game to delete the first bullet that was shoot (and then the second bullet ect) so that the total ammount is never more than 100 but so that the more recently shot bullets are still on the screen.
Is there a way to do this?
If you want to delete bullets outside the screen, GDevelop got a behavior that is going to delete every object outside the screen automatically.
If you apply this behavior on the bullet objects, all the bullet object outside the screen going to be deleted automatically.
In case you expect to have high number of bullets on the screen and you always want to delete the last ones above a certain number, the easiest way to go is to check the distance of the bullet form the player along with the number of bullets on the screen.
Let say. the number of bullets > 100 and Distance between bullet and player > 500 : Delete bullet.
And the same for the enemy in case the enemy also shooting bullet, just check distance between the enemy and enemy bullets.
You also need to consider to use For Each Object event to make sure the condition is checked for every single bullet and enemy and only the ones deleted that are fulfill the condition.
I have solved my problem using For each (object).
I still don’t exactly understand why it works and how the code is being processed. When I was tried to combine two of the for each expressions using sub-events in for each code block it stoped working. I have to run more experiments to understand why.
Also using the timers in for each code block created strange behavior - that only one of the bullets was executing the code for bullet behavior (turning towards the mouse cursor).
Anyway - problem is solved. I will now just look for more info on for each object code block and how it handles sub-events and timers. Because now my current code (below) is processing code for rotation and adding force every frame and not every few frames like i had it before using timers. I have to figure that out.
Here is my working solution for deleting object with object count, i hope it’s gonna help someone learning Gdevelop in the future.
The events/conditions/actions are triggered every frame (60 times per second) from top to bottom. Always the first on the top triggered first and then the second and so on down to the last one on the bottom.
The only exception are events with Trigger Once condition and At the beginning of the scene condition.
Events with Trigger Once condition are triggered only once every time the condition are met.
Events with At the beginning of the scene condition are triggered only once and only at the beginning of the scene.
Sub events are triggered always and only after the main event.
Timers are global timers, if you wan to run a timer multiple places you always need to reset the timer first. You can also pause and start timers as you need. If you don’t pause or reset a timer it is going to run forever.
When you are using For Each Object event, the sub events are gong to be triggered only if any object fulfills the condition in the main (for each object) event and only on the object that is fulfill the condition in the main event so the sub-event can be an standard event unless you want to go through every single object a second time or also on different objects.