Hi! I’m working on a game for a Game Jam and don’t have as much of a question as a curiosity. I’m making a vampire survivors style game and it has up to 60 character bodies on screen at a time with all the enemies. Some of them even have particle effects too. What I’m confused about is how when the game was getting pretty laggy it stopped processing physics. The walls to keep the player in the level stopped working sometimes and later it crashed. I was just curious if Godot four starts to ignore physics to keep the processing low or if It’s just some other thing.
P.S.
this happened with 6 summons on screen which each have rotating raycasts and a GPU particle and 7 enemies on screen which use a animation player and some even have other character bodies being their arrows.
P.P.S
If anybody knows how to make the game not crash so often/remove some of the characterbodies on screen. that would be cool too
Physics is usually handled in steps, it aims to hit 60 steps or ticks per second (configurable in project settings). If framerate is low enough a physics engine may step twice or more in one frame, if the physics calculations are severe they may lag the game, meaning now it has to take 3 steps per frame; but oh no that’s more physics calculations! Now 4 steps per frame quickly adding up until the game isn’t rendering and only doing physics calculations. This is called the “spiral of death”, Godot does avoid this by only doing 8 steps/ticks per frame (again configurable in project settings).
Reduce Physics Calculations
If your CharacterBodies are using move_and_slide be weary that it is a very expensive function, especially for +60 enemies every step; see if you can get away with move_and_collide or avoiding physics all together by changing the root node to use Node2D or an Area2D with fine-tuned layers and masks.
As always check your profiler, though I’m sure physics will be the one thing eating your framerate.
Thank you for helping me understand that. At first I did want to try and use move_and_collide but I was confused by what it said to call and what I saw online wasn’t too helpful. I’m also going to try and change the character body to an Area2D because I already have one embedded in there to deal with damage. If you know how to get move_and_collide working that would be great but if not I only added it to let enemies collide with other enemies which isn’t necessary to the core of my game. I’m also going to look a bit at the profiler tab to make sure there isn’t anywhere else my game needs to be optimized.
Edit:
I switched the Character bodies to Area2Ds and that really helped with performance. I’m still yet to put it in a very taxing situation but I’m going to test that soon.
To use move_and_collide the least you could do is replace move_and_slide() with move_and_collide(velocity * delta), but using an Area2D will be even better for performance than move_and_collide.
You may still get even more performance out of your Area2D if you reduce overlapping layers, so the enemies do not mask the same layer as other enemies. For example your player is on layer 1, enemies on layer 2, while the enemies only mask 1, so they only detect and report collisions on with the player.
i was mainly asking about move and collide for the player. The movement code is identical to that of https://www.youtube.com/watch?v=KceMokK2qFA. I have it set up so that four handles player damage and 2 handles enemy damage. so the attack from the player emits on 2 and the enemies emit of four. this was because I originally had more layers but simplified it so that is why it’s on 2 and 4. On the player if I replaces move_and_slide() with move_and_collide(velocity * delta) it would work normally?
remove all instances which are not needed in an specific moment.
set_proces(false) or set_physics_process(false) in any node which doesn’t need to run processes at a specific time. In my game I’m switching between these two states within each node, so that they are only used when necessary.
reduce loops as much as you can.
Small marginal gains sum up.
Another suggestion: take a look into your remote node tree when running the game and look for performance waste, so that you can implement logics to remove it.