How to improve performance when you have lots of dynamic objects in your game?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By ddabrahim

Hi.

I’m using the Mono version of Godot 3.2 on Windows 10.
I have an object that consist of the following nodes

Area2D (root)
–CollisionPolygon2D
–sprite

I have 300 instances of this object in my game and I would like each instance to rotate, move around and I also would like to check collision with this object by sending and listening to a signal if anything enters the collision area which is a 2D polygon collision area.

The problem is that, even if this object do nothing but rotate, my game runs only at 5 FPS with 300 instances present in the scene. It doesn’t move and I don’t check collision but rotate it.
This is the code I’m using to rotate the instances:

this.Rotation -= rotationSpeed * delta;

The C# script is attached to the root (Area2D) node.

Only a single line of code doing nothing but rotate the object and I get only 5 FPS with 300 instances in the game. In similar scenario in other engines I get stable 60 FPS with even 1000 instances in the game and even if the instances are moving and do collision check.

I was searching the forum for optimisation and did read the docs but I can’t figure out what am I doing wrong and I have no idea how I could improve the performance in Godot. It is a very simple project also and it is surprise me it runs so badly with 300 instances.

I would appreciate any advice or links to articles regarding what to do different and what to look for when you dealing with lots of dynamic objects in Godot.

Thanks.

you don’t need “this.” at the beginning

Merlin1846 | 2020-02-02 19:48

:bust_in_silhouette: Reply From: ddabrahim

In my case the problem was that I was rotating the Area2D node directly. It seems it is not a good idea for some reason.
If I change the hierarchy to:

Sprite (root)
–Area2D
—CollisionPolygon2D

and I attach the script to the sprite node and I rotate and move the sprite node instead the Area2D and send the collision signal from the Area2D to the Sprite, I get stable 60 FPS instead of 5 with 300 instances in the game.

However, if I have 1000 instances I get only 12 FPS. In other engines in similar scenario I get stable 60 FPS even with 1000 instances.
Wondering if is there anything else I can try.
Anyhow, if anyone has a similar performance problem, make sure you don’t rotate and move the Area2D node directly.

:bust_in_silhouette: Reply From: Calinou

When you have a lot of instances, it’s better to bypass the node system by using servers directly: Optimization using Servers — Godot Engine (latest) documentation in English

:bust_in_silhouette: Reply From: LudditeGames

I have noticed this behavior on 3.4.4-mono. How I fixed it appears to be similar to you – there might be some bug with attaching a script onto an Area2D itself I found that when I have an external script that modifies area2d, I get 60 fps. A script on an Area2D causes 5 FPS or so.