Saving and Loading from a StateMachine (Visual Scripting)

Discussion and help for Easy Save 3
Post Reply
User avatar
TauCetiLabs
Posts: 8
Joined: Mon Dec 28, 2020 2:31 pm

Saving and Loading from a StateMachine (Visual Scripting)

Post by TauCetiLabs »

I have just gotten into Visual Scripting, so this is almost definitely down to me overlooking something. With that said, we are developing a game with a construction mechanic spread across multiple locations. There is a lot of data to save and load and it is done a bit more manually, which ES3 has helped us manage nicely. However, perhaps due to our more manual handling of saving, I can't seem to figure out how to save the state of a StateMachine and load it back. I haven't come across clear documentation as to how to do this on the Unity Visual Scripting side, outside of a comment I came across saying 'use Easy Save 3'. :lol:

We have implemented something similar for other components already, usually inside the component with data to serialize. The backend keeps a list of ISaveLoadComponent and performs a serialize/deserialize to the matching components. This is used in part to separate the save into separate files, keeping the data organized and the files from getting too massive. The only issue left is figuring out what data to save/serialize and how to load/deserialize it back into the StateMachine:

Code: Select all

public class SaveLoadStateMachine : MonoBehaviour, ISaveLoadComponent
{
    [ES3NonSerializable] public StateMachine stateMachine;

    [ES3Serializable]
    public SerializationData stateMachineData
    {
        get
        {
            return stateMachine.Serialize();//???  This seems like it will be trying to serialize the state machine itself, not the current state data
        }
        set
        {
            //stateMachine.CurrentState.Deserialize(value);//something like this?
        }
    }


    [ES3NonSerializable] [SerializeField] private string m_SLC_UniqueName = "StateMachine";
    public string SLC_UniqueName => m_SLC_UniqueName;

    public bool SLC_IncludesReferences => false;
}

There is no time pressure here, as we are looking into making sure we can save/load to a state machine before we commit to implementing any StateMachine.

Thank you
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: Saving and Loading from a StateMachine (Visual Scripting)

Post by Joel »

Hi there,

Unity Visual Scripting doesn't provide a way to do this automatically via serialization so you would need to manually do this.

Generally you would need to assign some sort of unique string/id to each of your states and save this. Then when loading you would get this name and then navigate your state machine to that state. I'm no expert with Unity Visual Scripting and Easy Save just exposes it's methods so that they can be used within it, so you might be better off asking on the Visual Scripting forum on how you would do this practically.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
User avatar
TauCetiLabs
Posts: 8
Joined: Mon Dec 28, 2020 2:31 pm

Re: Saving and Loading from a StateMachine (Visual Scripting)

Post by TauCetiLabs »

Thanks Joel!

I think this helps point me in the right direction. The lack of tutorials around saving/loading the 'status' of a state machine is a bit head scratching. UVS is a very powerful and flexible toolset otherwise. If we do go down this visual scripting rabbit hole, I'll respond here with our solution when it is all figured out. Might help someone down the road.
User avatar
TauCetiLabs
Posts: 8
Joined: Mon Dec 28, 2020 2:31 pm

Re: Saving and Loading from a StateMachine (Visual Scripting)

Post by TauCetiLabs »

After reading some of the replies on this thread on the UVS forum, I am rethinking the jump: https://forum.unity.com/threads/novembe ... e.1364364/

Update:

GPT-4: "Given the information you've provided and the uncertainty surrounding the future of Unity Visual Scripting, I would recommend going with a custom C# state machine implementation. This approach gives you more control and flexibility, allowing you to tailor the state machine to your specific needs, optimize it for performance, and maintain it independently of Unity's decisions regarding Visual Scripting.

Additionally, a custom C# state machine ensures that your project is not reliant on a feature that might be deprecated or significantly changed in the future, reducing potential risks and maintenance concerns.

However, I would advise keeping the ease of use and collaboration in mind when implementing the custom C# state machine. Providing clear documentation, creating custom editor tools when necessary, and organizing the code in a way that makes it accessible to the rest of the team can help mitigate some of the challenges associated with a custom implementation."
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: Saving and Loading from a StateMachine (Visual Scripting)

Post by Joel »

I'd say that GPT-4 is underestimating the work required to make a functional visual scripting asset from scratch (it feels like it's actually comparing coding to UVS), but ultimately its concerns are justified.

IMO coding is much more flexible, easier to debug and safer in the long term than visual scripting, so would always be my first choice.

There are situations where we have to use visual scripting, for example when working with client teams of non-coders who need to change logic after our contract is over. In these cases I much prefer PlayMaker to UVS. UVS seems to make things unnecessarily complex whereas PlayMaker works to it's strengths and keeps things simple. Or a better way to describe it, UVS feels like it was designed without ever consulting a non-coder. Jean at PlayMaker is extremely fast at providing support and updates, which isn't something which can currently be said of UVS.
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Post Reply