0
Answered

How to Add FlowMachine to a GameObject at runtime, and let it run a designated macro?

yyabb 4 years ago updated by nkcd 3 years ago 6

Thanks!

Bolt Version:
Unity Version:
Platform(s):
Scripting Backend:
.NET Version (API Compatibility Level):

This was copied from a very old post and I changed it to reflect this question. Haven't tested but should work.

var flowMachine = transform.AddComponent<FlowMachine>();
flowMachine.nest.source = GraphSource.Macro;
flowMachine.nest.macro = FlowMacro("flow macro name");
flowMachine.nest.Instantiate();
flowMachine.enabled = true


It doesn't work on Bolt 1.4.0f7, there's no "Instantiate" method on GraphNest.

Pending Review

Unless I'm mistaken, you shouldn't need Instantiate in 1.4, the macro change should be detected automatically.

Yes, no need doing flowMachine.nest.Instantiate(). I ended up using `flowMachine.nest.SwitchToMacro(flowMacro);`.

But I encountered another problem: changed flowMachine didn't respond to events. And I found a solution on a related question 

  1. I set the prefab with FlowMachine component inactive.
  2. At runtime, Instantiate the prefab, and `var machine = gameObject.GetComponent<FlowMachine>(); machine.nest.SwitchToMacro(flowMacro);`.
  3. SetActive the instantiated gameObject.