-
Notifications
You must be signed in to change notification settings - Fork 10
Component Manager
There is a very noticeable gap in performance when we have 10,000 nodes all with their own _Process
functions. If we use a centralized component manager that handles all processes we notice a 3.5x increase in performance.
Normally without ComponentManager
you would do something like this.
public partial class Player : Node
{
public EntityMovementComponent MovementComponent { get; private set; }
public override void _Ready()
{
MovementComponent = new EntityMovementComponent(this);
}
public override void _Process(double delta)
{
MovementComponent.Update(delta);
}
public override void _ExitTree()
{
MovementComponent.Dispose();
}
}
But this setup isn't great because consider you implement EntityMovementComponent
in Enemy
and you forget to call Update
or Dispose
or maybe even something else.
This is where components come in. Instead lets make EntityMovementComponent
extend from Component
.
public class EntityMovementComponent(Player player) : Component(player)
{
// Notice these methods do not start with an underscore
public override void Ready()
{
// Process is disabled by default and we must enable it ourselves
SetProcess(true);
}
public override void Process(double delta)
{
// Handle process...
}
public override void Dispose()
{
// Handle dispose...
}
}
Now in Player
we can just do the following. We no longer have to worry about calling Update
or Dispose
as it is handled for us because EntityMovementComponent
now extends from Component
.
public partial class Player : Node
{
public EntityMovementComponent MovementComponent { get; private set; }
public override void _Ready()
{
MovementComponent = new EntityMovementComponent(this);
}
}
Now our ComponentManager
which you never need to look at is centralizing all the _Process
, _PhysicsProcess
, _UnhandledInput
, _Input
methods increasing the performance of your game!