FSMOwner.SwitchBehaviour() functionality not working synchronously?

NodeCanvas Forums Support FSMOwner.SwitchBehaviour() functionality not working synchronously?

Tagged: 

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #17756
    playboi14
    Participant

    Hi

    I’ve created a FSM asset that I’m loading at runtime. This asset has several variables and the game involves spawning an agent and then using SwitchBehaviour method on the FSMOwner script (attached to the agent). This FSM Owner attached to the gameobject is not set initially in the prefab.

    Now that the FSM has been assigned the asset, I load in a series series of ints, bools and other value type that my graph needs. For example setting an enum value here

    fsmOwner.graph.blackboard.SetVariableValue("collectionResources", savedData.collectionResources);

    Here is where the issue is, because after all of these variables have been set, the variables do not persist. What I mean is they don’t appear at all in the blackboard when I select the agent with the canvas view. I know that setting these values does work when I manually create a new FSM Owner on a gameobject and assign it values at runtime, the values get saved and are visible in the blackboard, but calling .StartBehavior() on the FSMOwner seems to erase them.

    However, if instead of doing all of that sequentially I wrap it in a coroutine, I can get the functionality to work by first switching to the behavior and THEN loading in the data after waiting till the next frame

    This is my confusion. I don’t understand why switching the behavior and then setting variables doesn’t work and why doing this in a coroutine with a slight pause fixes this. I’ve had a long look at the documentation and I’m struggling to figure out what’s happening and if this is the correct behavior.

    I’m using FSM’s with a save and load system so I need to be able to spawn things in on the fly and then set-up items by manually copying back their values into the graphs variables.

    I’d appreciate any info anyone is able to give me here, is this the right approach to ensure my loading will work?

    #17780
    playboi14
    Participant

    Is anyone able to provide any feedback on how we should go about setting FMS at runtime and setting up their blackboard variables?

    I’m looking at using NodeCanvas to create a heap of FSMs for my game and I need to swap them out dynamically, so I need to know if the approach above is how these should be set up so they work reliably.

    #17796
    Gavalakis
    Keymaster

    Hello,

    Everytime a new FSM is loaded (eg SwitchBehaviour), an instance of the original FSM is created and loaded to the FSMOwner to use. This is why setting variables manually after the graph is switched/started like you do is correct, otherwise erased since if variables are set before, they are just set on the original graph which in runtime is not serialized and therefore those variables do not persist when an instance of that original graph is created when using SwitchBehaviour.

    With that said here is what I just tried:

    1) I created a gameobject with an FSMOwner attached, but no FSM assigned.
    2) I created an FSM asset graph.
    3) I used this code (attached on the FSMOwner gameobject):

    In runtime the FSMOwner is switched to use the FSM asset and the manual variable set is there and visible in the graph editor view.
    Please note that I have used “Start” here. If I was to use “Awake” or “OnEnable”, then indeed the variable set is not there. That is because the new graph loaded is not yet initialized (which is done in Awake and OnEnable for the FSMOwner and Graph respectively).
    The above example code also works correctly in the “Update” method instead of “Start”.

    Can you please confirm when you are calling SwitchBehaviour and setting manual variables (Awake, OnEnable, Start, Update), when not using the coroutine workaround? When is your “Load” method called basically 🙂

    Thank you.

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

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