Creating FSMOwner and Blackboard scripts in code

NodeCanvas Forums General Discussion Creating FSMOwner and Blackboard scripts in code

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #11853
    aer0ace
    Participant

    I’m looking to programmatically AddComponent<FSMOwner> and a Blackboard when I instantiate one of my unit prefabs. The idea is, at this time, I wanted to load and bind a separate <graph>.asset to the FSMOwner. I already noticed that I can bind the graph asset to the FSMowner, and also “promote the parameters to blackboard variables”, in code.

    It looks like I can do AddComponent<FSMOwner> but I’m not sure how to add a blackboard. Is this a bad idea or how should I approach this?

    #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

    #11856
    aer0ace
    Participant

    This helps a lot. Thanks, Gavalakis! It’s such a thorough response indicating the advantages and disadvantages to the different methods for doing what I’d like to do.

    The main reason I didn’t want to attach an FSMOwner and Blackboard to the prefab, is that I get extremely frustrated when I have missing scripts on my objects, and I can’t find out where they are coming from. This would be the case if I say, opened my project on my laptop, and haven’t installed NodeCanvas yet. There’s no way for me to know that those missing scripts are NodeCanvas scripts. I wish Unity was a little better with this, and at least store a string type somewhere so that users can easily know what scripts are missing…

    Your answers to Blackboard population are good too. It would make sense that CreateDefinedParameterVariables() would not be performant, as it would need to traverse the entire FSM. Though, I could see this being less of a problem the smaller the graph, no?

    Ultimately, it would be ideal to keep the BB on the owning game object, because if I also needed to keep default BB values, I could. But the “missing component” problem is such a sticking point for me, I wanted to weigh out all my options.

    Thanks again!

    #11859
    Gavalakis
    Keymaster

    Hey,

    You are very welcome! I am glad I could be of help in determining what is the best approach for you.
    Indeed if some script is missing, Unity does not really tell us any useful info, but I think that in the end you can’t simply avoid adding scripts to gameobjects completely 🙂
    Regarding CreateDefinedParameterVariables(), yes it should be relatively fine to be used in runtime for smaller graphs.
    If you have any other questions I can help you with, just let me know.
    Thanks!

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

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.