NodeCanvas Forums › Support › Saving and loading Behavior Trees
Tagged: save load saving loading
Hey,
I was wondering if NodeCanvas supports any kind of saving / loading functionality. I know there is an ES3 extension for saving and loading what I assume are blackboard variables, but I’m more interested in saving the actual state of the behavior tree.
For an example, in a really large BT, if I am in a deeply nested task with lots of conditionals and logic that were required to get there, I’m wondering if it’s possible to save the current executing node and internal BT stuff like that, so that I can save the state of the BT and tell it “hey – resume from that node right there with this state right here”.
How much of this is built in and how much of the saving/loading logic would I have to code myself?
Hello,
Saving the whole state of a graph is not supported. There are technical reasons not to, but I also believe that allowing to save the game at any given time is not ideal, hence why most games save only at some sort of check points (but that is of course not a defacto). Saving Blackboard variables is supported however, which depending on how the tree is designed (in conjunction with other non-variable conditions), it may become a way of saving a tree state and making the tree flow to the state it was, based on the evaluation of the conditions.
There is a way to make a node the “Start” node of the graph through code as well (by setting the ‘graph. primeNode’ property), which might work for your use case of resuming from a specific node. So you could for example save the ID of a node and when you want to load the graph state: 1) Make the node of the previously saved ID the Start node (setting graph.primeNode property), 2) Start the graph, 3) Restore the original Start node immediately so that the graph restarts from it when it resets. Do note however that this will just make the tree start from a specific node. I haven’t really tested this for the purposes of saving/resuming as described in your use case, but it could work. I can make some tests and provide some code if you want to though.
( The ES3 integration by the way, is an integration to call the existing functionality of ES3 from within the NodeCanvas using the tasks included in the integration package. )
To summarize, saving/loading the whole state of a graph is not really supported. Saving/loading Blackboard variables are.
Let me know.
Join us on Discord: https://discord.gg/97q2Rjh
Hey Gavalakis, thanks for the reply.
I’d really appreciate if you could do the said tests and provide some sample code. Anything is useful because I don’t currently have a lot of experience with NodeCanvas. The idea does sound promising though.
Please let me know if you figure anything out. I’ll also start testing on my own.
Thank you!
Hello again,
Unfortunately changing the root node did not work as expected in my tests for the purposes of saving/loading as described above 🙁 Even if it did work however, that would still be problematic since there could be more than one node running in a behaviour tree, in case for example a Parallel node is used; so this is unfortunately not a viable option to explore further 🙁
As described before, I would suggest to try and design your trees in such a way that saving/loading only the blackboard variables (along with other conditional tasks of course), makes it so that the tree flow to the desired branch. Alternatively, you could disallow the possibility for the player to save the game at any given time (if that is a possibility for your project as well of course).
Please let me know.
Thank you.
Join us on Discord: https://discord.gg/97q2Rjh
Hi,
Thank you for trying this. It’d still be great if I could just resume from one exact node, but I do agree problems arise when the likes of Parallel nodes are used. I’m not sure what the best way to deal with this is.
If I went with the approach of making it so the tree flows to the desired branch, do you have any advice on how to achieve that? I’ve never done anything like this before and this seems like it’d take much more extra logic and it’d be highly specific for every single behavior tree.
Thank you
Hello again and sorry for the very late reply.
It is mostly a matter of using conditions that evaluate the state of the game (or variables that are bound to property getters that reflect the state of the game), which game state should be correct at any given moment. Thus a behaviour tree will only “reference” the game state via conditions and thus it flows to the correct branch (since branching is all about the conditions used essentially, apart from a few nodes like the sequencer of course). Utilizing the “Dynamic” option that is found in some nodes can also be helpful to make the graph react instantly to those conditions.
I hope that helps. Thanks!
Join us on Discord: https://discord.gg/97q2Rjh