In my game, I have a test enemy that can deal contact damage. Because I had some issues with grouping, I gave the enemy a special “hurtbox,” a hitbox that can damage things that touch it. It’s designed in a way where I can re-use it for other enemies too
So when the game detects the enemy onscreen, it creates one for each enemy. I tried going for the most optimal approach to make sure these events only happen once:
Despite that, when I spawn a whole bunch of them in, it doesn’t take long before the game begins to lag:
The profiler seems to show high intensity in the specific area too, although, I’m not sure why. I thought my code was optimal? I know GDevelop can handle more stress than this, so it’s a bit baffling to me
(I have an extra hurtbox sitting in the scene for testing)
There’s barely any more logic after that, the enemy nor the Hurtbox even do much
Even the Target Dummy object which does LITERALLY nothing is more event intensive than this enemy, how strange
TestSpikeDummy is using 12.9% but it’s of its parent Enemies which is using 15% of the total.
So, it’s only using 12.9% of the 15% that the Enemies event group is using. It’s only uses .02 ms.
The same is true for target Dummy. It’s using 77% but that’s of 15%
Try moving the initialize object variable is false condition before the for each object and set the for each object as a subevent. The loop only needs to run for those objects. It’s best to reduce the pick list first. As is it’s still running through each object even if it’s initialized. It might not do anything but the simple act of looping eats resources.
Imagine having 10 water glasses. 5 glasses are full. Would you move all 10 glasses closer to the sink or would you just move the empty glasses. While you wouldn’t fill the full glasses, the act of fetching them or even looking at them, uses time.
It might not seem like a lot of time but multiple it by say 50 glasses or objects and do it 60 times a second while trying to do a bunch of other things.
Hmmm, I moved the condition above the for each like you said, and I actually did see a small boost in performance in the profiler. I’m still getting a bit of lag tho
I’ve concluded that this code is the culprit. The code used to deal damage:
It has a high stress in the profiler, and disabling it improves performance. But for the vision I have for the game, I don’t want to remove it
It works like it does in games like Terraria or Binding of Isaac. If an attack can pierce, each projectile of the same type can hit every enemy it touches, no matter what
Then, the UID of the attack is added to an array for 0.2 seconds, giving the enemy immunity to that specific instance of the attack for that time. Once that much time passes, the enemy can be hit by that projectile again, which is useful for slow moving attacks, or attacks that stick to enemies
If the HurtBox has the same UID as the Entity, then that Entity has ownership over that HurtBox, and it won’t be hurt by it’s own attacks
It also checks if the bullets should hurt enemies, or hurt the player. This way I can use less objects and have player attacks and enemy attacks used together
It works, but there’s room for improvement. Now that I know that THIS is causing the lag, are there any ways I can improve the performance of this without sacrificing the mechanics?