derkork / godot-statecharts

A state charts extension for Godot 4
MIT License
679 stars 33 forks source link

Make constructor of StateChart.cs protected #119

Closed BenocxX closed 1 month ago

BenocxX commented 2 months ago

Hello!

Would it be possible to make the constructor of StateChart (C# class) protected? I would like to extend the class to add custom methods for the different state charts in my game (similar to the extensions methods in the C# example, but I don't want them to be accessible on every state chart).

Example of how I want to use it with the protected constructor :

// PlayerStateChart.cs
public class PlayerStateChart : StateChart // Inherits from StateChart that inherits from NodeWrapper
{
    protected PlayerStateChart(Node wrapped) : base(wrapped)
    {
    }

    public new static PlayerStateChart Of(Node stateChart)
    {
        StateChart.Of(stateChart); // Will throw an exception if the node is not a state chart.
        return new PlayerStateChart(stateChart);
    }

    public void SetIsMovementKeyPressed(bool value) => SetExpressionProperty("is_movement_key_pressed", value);
}

// Player.cs
public override void _Ready()
{
    var stateChart = PlayerStateChart.Of(GetNode("%StateChart"));
    stateChart.SetIsMovementKeyPressed(true);
}

// Enemy.cs
public override void _Ready()
{
    var stateChart = EnemyStateChart.Of(GetNode("%StateChart"));
    stateChart.SetIsMovementKeyPressed(true); // I want an error here, this method should only be accessible via the PlayerStateChart class
}

My current solution without the protected constructor is to either manually modify the StateChart file to make the constructor protected or to make a wrapper class called PlayerStateChart that exposes the needed methods. The downside of the custom wrapper is that I loose all the other methods of the StateChart & NodeWrapper classes since the are now inside PlayerStateChart. I could make the raw state chart property public, but it does not feel like a good enough solution to me.

Thanks for your time, this Godot extension is awesome!

derkork commented 2 months ago

Makes sense, I'll add this to a future version. Thanks for reporting it!