Can I Change macro at StateMachine in RunTime with out use Preb?

Junsion Loh 4 years ago updated by Lazlo Bonin (Lead Developer) 4 years ago 12

i have a Enemy Database (ScriptableObject) with a AI setting. It Setting use StateMacro to Save difference Enemy AI Setting. But I cannot find any option or API can use it. (I found how to change macro but StateMachine still cannot working.. Please Help

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

Hi Junsion, welcome to the community!

Try this:

var stateMachine = enemy.AddComponent<StateMachine();
stateMachine.nest.source = GraphSource.Macro;
stateMachine.nest.macro = yourMacro;

Let me know if it works!

Still Cannot Work!

InvalidOperationException: Cannot change nested graph source after instantiation.
Ludiq.GraphNest`2[TGraph,TMacro].set_source (Ludiq.GraphSource value) (at <aedb7912210a43bbbfa33ce46dd7e79b>:0)

I try 


can change the Macro, but statemachine is not working (Not Running)

i Found Solution to fix it, i think it got some function at Awake or Start , so must be disable before change Macro.

var stateMachine = transform.GetComponent<statemachine>();
stateMachine.nest.source = GraphSource.Macro;
stateMachine.nest.macro = GetCharacter.GetSubCharacter.SubAIBase;
stateMachine.enabled = true;

Hi Junsion,

Yes, the macro gets instantiated in Awake.

As the error mentions, you cannot and should not change the macro after the machine has been instantiated. This is why I created a new state machine in my example code.

If you want to keep an existing machine, make sure it starts with its source as macro and its macro to null.

Your workaround works too.

I try to spawn Monster every One Second but found a performance issue...

Every Time I Spawn a new Enemy, my game will Lag... 

Have you tried creating the equivalent script and testing performance in a clean project with no Bolt?

Are u mean disable Bolt in my Project ? (After Disable Bolt is no laggy anymore...) 

Yep. Just to confirm. And seems Lazlo just did. 

Yikes! That's definitely not normal. Can you turn on deep profiling and show me the inside of StateMachine.Awake? And/or post the macro file for the state machine so I can have a look myself. 

i already disable any connection at StarAIBase


Hi Yan Jun Loh,

I created a separate topic to keep track of the issue, please continue the discussion there: