Saving gameobject state in the flow

Kingblade 3 years ago updated 3 years ago 4

Hey there!

I am currently using bolt as a way to manage my characters state and behavior by a given situation (for example, what dialogue they should say when you talk to them right now.

The thing is, as I am doing it right now, the flow of the character is represented in a tree like fashion with a ton of nodes, something like this:

Once you enter the scene, depending on a few application variables, your state will be changed to any of the 3 top options, and it goes down from there depending on all kinds of player actions.

So far so good, but right now I want to be able to do something a bit more advanced.
I want the player to be able to go and do other things while in the middle of the flow.

For example, let's say we are here:

And then, the player needs to do something in the current scene to advance to the next node. Instead of doing it, he goes to exit the scene, and move to another one. Once he returns, the game object will no longer be in the right node of the state graph.

As much as I know, I have to create some kind of flow in the starting node that is able to make a transition to the last node it was in based on application variables I need to save along the way. That method will work, but it also means that, given that there are A LOT of nodes (and the picture above represents a relatively simple flow for me), I have to create a lot of application variables, and a lot more transitions from the starting node to any node in my graph.

That seems like something that I should not do, so I thought to myself, maybe I can save the state of a graph like I'm saving application variables...?
Is that a feature? If not, could it become a feature somewhere down the line?

Thank you very much for your time (:

Bolt Version:
Unity Version:
Scripting Backend:
.NET Version (API Compatibility Level):

Hi Kingblade!

So if I understand correctly, your player switches scenes during the course of your state graph, and you want it to preserve its active state across scenes?

The application variables approach could work... but it's very complicated.

My suggestion would be to call Dont Destroy On Load in the start state of your player object. Then, instead of having one player object per scene, you would be reusing the same player object across all scenes. This way, it's active state (and all its other properties) would be preserved.

Does that make sense in your situation?

There is a problem with that, as the gameobject containing this graph (and there can be multiple game objects in one scene with this problem) holds references to other objects in the scene.

That basically means that in order for the game object to not break, I would have to not destroy the entire scene, and mind that when the player re-enters the scene, as to not reload the entire inactive scene again.

Other than that being quite a big workaround (that might consume a lot of memory, depending on how many scenes were loaded), there is the issue with saving the game.

The current thing I am doing to save my game is taking all of bolt's application level variables, and saving their values to a save file. If I'll go with this method, I will lose that state once a player saves the game, quits and reloads.
Having a way to save the graph current state would be the ideal way for my workflow, as it allows me to save a single variable value to the save file, and reloading it later so that everything will work as planned.

I do get from your answer that it is not something that is currently available, so for now I guess I will have to create a ton of application level variables to determine these states, but I would love to hear more from you if this feature is something that you consider adding.


Hi Kingblade!

I'll try to give you the best advice I can without knowing your exact design. I think there are multiple ways you can go about this, but they may require rethinking how you approach your bigger structure.

  • You can move part of your logic to a "manager" graph that lives across the scenes (DontDestroyOnLoad). This manager would therefore keep track of the character's current state. This way, the only application variable you would need is one that points to the character object in the current scene. 
  • You can get rid of direct scene references (assuming this is an embed graph) by using Scene Variables or Application Variables, meaning the same graph will stay functional regardless of which scene it's in.
  • You can use Saved Variables to simplify your save/load mechanism. They are automatically handled by Bolt for any type and saved to the cross-platform compatible PlayerPrefs automatically. But if you have something more advanced, they might not be ideal.
  • As a general tip, remember you can use Super States to nest state graphs inside state graphs. This can help if you have "higher-level" states (e.g. "Dialogue", "Free Roam", "Combat"), and within each of those multiple "lower-level" states (e.g. "Patrol", "Attack", "Chase").

To answer your last question, no, we don't plan to implement any "state-save" feature, for the same reason we do not plan to add a "get current state" unit. That is, states are not data, they are language constructs. Referring to them and saving them would basically be the equivalent of reflection in C#, and we don't want to open this pandora's box with Bolt anytime in the near future.

we don't want to open this pandora's box with Bolt anytime in the near future.

That's fair :)

Thank you very much anyway!