derkork / godot-statecharts

A state charts extension for Godot 4
MIT License
761 stars 39 forks source link

How to get the name of the active state? #133

Closed eobet closed 1 month ago

eobet commented 1 month ago

Sorry, I tried to read the documentation but I must have missed the obvious.

I'm using this plugin in conjunction with another plugin, and before I got away with just looking at the @active but apparently I'm not supposed to do that? (It stopped working in the latest bleeding edge nightly that I'm using, but not for reasons related to this plugin.)

So what's the official way of asking StateChart which state is active? (And by extension, getting the name of that state?)

derkork commented 1 month ago

The nature of state charts is that more than one state can be active at the same time, even for the simplest cases (e.g. when you have two parallel states, both are active and when you have compound states both the parent and one child are active at the same time). As such there is no function built-in to retrieve the one currently active state. The idea is to use events and signals to drive state dependent logic. If you have logic that is specific to a certain state you can connect this state's state_processing signal to a function to have this code running every frame while the state is active. Another option would be to connect to the state's event_received signal to react when a certain event is received in a certain state. Finally , if you need to know whether a specific state is currently active you can also get this state with get_node and check its active property, e.g.

if get_node("StateChart/Root/Invulnerable").active:
    # ignore damage 

If none of these work for you, I would very much like to know more about your use-case.

eobet commented 1 month ago

Thank you for taking your time to explain!

Since I'm a beginner with both Godot and this plugin (and the other plugin I'm using), I got confused when @Active disappeared from my editor which now with the latest version did properly change to .active so yes, all is good.

Thank you for making this plugin. 🙏