0
Answered

Checking if a GameObject has a specific State Machine and/or Flow Machine

Zefugi 2 years ago updated 2 years ago 3

I'm performing a raycast and getting a GameObject - how do I check if the given GameObject has a specific State/Flow machine?

Bolt Version:
Unity Version:
Platform(s):
Scripting Backend:
.NET Version (API Compatibility Level):
GOOD, I'M SATISFIED
Satisfaction mark by Zefugi 2 years ago
Answered

There's no easy way to do that, but there are two easy workarounds:

  • Check for tags
  • Check for the presence of an object variable (using Is Defined units)

You can create object variables at runtime, like "isEnemy", by just setting them on the start event.

Alternatively, you inspired me to do a quick rework of an old macro that was broken. This should do what your looking for, as long as it is a Macro. Only type you can actually check. If it is embeds, you should do exactly what Lazlo said. Here is the macro link: 

https://support.ludiq.io/communities/5/topics/333-get-machine-instance

+1

Thanks for the quick responses. =D

I decided to create an empty scriptable object (ComponentTag) and mono behaviour (TagList) that has a public array of ComponentTag and two methods: void AddTag(ComponentTag tag) and bool HasTag(ComponentTag tag).

I also have a flow machine FM_EnsureTag that ensures that a given object has the TagList component and that the list has a given ComponentTag. And to top it off a FM_HasTag flow machine that returns a bool indicating wether a GameObject has a specific ComponentTag.

This way I can create as many tags as I need and I can add any combination of them to any GameObject. In the specific functionality described in the original question, any state or flow machine that I want to check for, I add a call to FM_EnsureTag, and check that the tag exists instead of checking if the machine exists.

I might use a different name for tags in the future, so as not to confuse them with the Unity build-in tags.


@ludiq : How about making some sort of place for people to share flow and state machines? It won't just serve as a functionality exchange, but also as a heap of examples to learn from.