Reply To: Creating FSMOwner and Blackboard scripts in code

NodeCanvas Forums General Discussion Creating FSMOwner and Blackboard scripts in code Reply To: Creating FSMOwner and Blackboard scripts in code

#11854
Gavalakis
Keymaster

Hey,

I would generally instead recommend, having the FSMOwner and Blackboard components already attached in the prefab, just without an assigned FSM asset in the property. Then when the prefab is instantiated, make a call to it’s FSMOwner.SwitchBehaviour(FSM newFSM) method.
This method will assign/bind and start the newFSM target all at once and the already attached Blackboard will be used.

Of course one thing to note here, is that the Blackboard should already contain the variables that the new FSM requires (same name and type), or of course you could alternatively create them manually just after instantiating the prefab (and before calling SwitchBehaviour) by for example using GetComponent<Blackboard>.AddVariable(string variableName, object variableValue), or even use the Graph.CreateDefinedParameterVariables(IBlackboard targetBlackboard) method to promote all parameters to variables like you said (although it’s a bit slow performance wise).

To answer your question though, if for any reason you don’t want to have a Blackboard component already attached, a blackboard can simply be added normally with AddComponent() 🙂 There is no further step required to make the FSMOwner use it as long as it is added on the same gameobject.

So in the end you could have something like this:

Please let me know if that helps.
Thanks!

Join us on Discord: https://discord.gg/97q2Rjh