imjp94 / gd-YAFSM

Yet Another Finite State Machine for godot
MIT License
534 stars 25 forks source link

(feature request) any state #39

Open tomankirilov opened 2 years ago

tomankirilov commented 2 years ago

I often find myself needing to have a state that can be reached by all other states with a single condition. Making transition arrows from each state creates a big mess and everything looks quite confusing. When using unity's state machine I have noticed they have a "any state" state. Implementing soemthing like this should make state machines cleaner.

imjp94 commented 2 years ago

Can you point me to the documentation of unity's "any state" state?

tomankirilov commented 2 years ago

Thats the best I can find: **Any State** Any State is a special state which is always present. It exists for the situation where you want to go to a specific state regardless of which state you are currently in. This is a shorthand way of adding the same outward transition to all states in your machine. Note that the special meaning of Any State implies that it cannot be the end point of a transition (ie, jumping to “any state” cannot be used as a way to pick a random state to enter next). https://docs.unity3d.com/Manual/class-State.html

imjp94 commented 2 years ago

Cool, I do have some experience with unity's state machine but I never knew the existence of "any state". And I think the implementation should be as easy as adding a special state - named "Any State"(literally, just like "Entry" or "Exit")

tomankirilov commented 2 years ago

Exactly! It is not a big feature but helps you keep the spaghetti away.

cheesycoke commented 3 weeks ago

I'm not good with code by any means but this workaround seems to work for me for the time being, so figure someone else might find it handy. (where smp is the StateMachinePlayer node)

func forceState(state:String):
    smp.set_param(state,true)
    smp.restart(true,true)
    await smp.transited
    smp.set_param(state,false)

Attach the state you wanna switch to to the Entry state using a boolean. It's probably possible using a trigger, but this ensures that it catches the state machine before it tries going to the first state after Entry. Using a stealth game "caught" fail state as an example: image Won't be surprised if this is inefficient or unreliable for any reason, but it's at least kind of a band-aid fix?