derkork / godot-statecharts

A state charts extension for Godot 4
MIT License
734 stars 35 forks source link

Port to C# #50

Closed malino-dev closed 10 months ago

malino-dev commented 10 months ago

Are there any plans to port this to C# ever?

derkork commented 10 months ago

You can actually use this with C#:

// get the state chart node
var stateChart = GetNode<Node>("StateChart");

// Send an event to the state chart
stateChart.Call("send_event", "some_event");

// Set expression guard properties
stateChart.Call("set_expression_property", "health", 27);
stateChart.Call("set_expression_property", "shields", 48); 

The rest is just using signals which work with C# out of the box. I should probably add this to the manual instead of the FAQ. Maybe I could also add some extension methods to Node which make this work with less strings.

yuiidev commented 10 months ago

I was actually thinking about doing the same thing with extension methods. It would be much appreciated.

derkork commented 10 months ago

So I have decided against extension methods and in favour of wrapper classes. This way the extension methods will not pollute the autocomplete of normal nodes with state chart specific stuff which may be confusing and/or irritating. The new way will be:

var stateChart = StateChart.Of(GetNode("StateChart"));

// send an event to the state chart
stateChart.SendEvent("some_event");

// set expression guard properties
stateChart.SetExpressionProperty("health", 27);
stateChart.SetExpressionProperty("shields", 48);

I have also added some additional support for connecting signals in code, so this relies less on strings:

var state = State.Of(GetNode("%ActiveState"));
state.Connect(State.SignalNames.StateEntered, Callable.From(OnStateEntered));

Currently finalizing the manual and the example and then you can give this a spin.