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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//instantiate and add components.
varinstance=Instantiate(prefab);
varowner=instance.AddComponent<FSMOwner>();
varbb=instance.AddComponent<Blackboard>();
//optionaly create variables either...
// 1) manually
bb.AddVariable("myFloat",13f);
// 2) or by promoting fsm parameters into blackboard variables.
fsmAsset.CreateDefinedParameterVariables(bb);
//switch to the new fsm asset which will assign and start the fsm using the blackboard found on 'owner'.
owner.SwitchBehaviour(fsmAsset);
Please let me know if that helps.
Thanks!
Join us on Discord: https://discord.gg/97q2Rjh
Login
Register
By registering on this website you agree to our Privacy Policy.