Multiple things needing saved

I’ve been confused since we started this in Core Combat. How would you save multiple things per item? I would want to save position AND rotation for the player(maybe not for this, but for an implementation in third person). It seems like anything that needs to implement CaptureState and RestoreState can only reference a single thing.

Quick Edit: The next lecture with inventory shows you can cast an array of strings for the object type. So I think in theory we could do that for transform and position. But moving beyond that, how would you do ANY mixed types of things?

You are just saving an object. You can save anything you want. Make a struct or class holding the data and save it

[Serializable]
struct MyData
{
    SerializableVector3 position;
    SerializableVector3 rotation;
}
object CaptureState()
{
    var data = new MyData()
    {
        position = new SerializableVector3(transform.position),
        rotation = new SerializableVector3(transform.forward),
    };
    return data;
}
void RestoreState(object state)
{
    var data = (MyData)state;
    transform.position = data.position.ToVector();
    transform.forward = data.rotation.ToVector();
}
2 Likes

Exactly that. Creating a struct or class to hold the data is the key. There are a few things to make sure it remains serializable…

  1. It must have the [System.Serializable] tag (or just [Serializable] if you’re using System
  2. It must only contain components that are themselves serializable
  3. It cannot have properties… this is what prevents Vector3 from being [Serializable]. The Vector3 has properties like .magnitude, and when the system tries to serialize the Vector3, it attempts to save these properties as well, but this gets into a self-referential loop and crashes the serializer.
1 Like

OMG… Yeah I don’t know why my brain skipped right over that.

I feel like that is just a good way to set it up in general so you have room to expand. Just my 2 cents.

But thanks both of you! I saw just a snippet in the email notification on my phone from Brian’s response and I just belted out “DUH!” in my living room :joy:

Okay… so what am I doing wrong?
BIG OL EDIT… I had done a bunch of Undoing and accidentally had the type saving as SV3 and not my type. However… this code still doesn’t seem to care much about rotation. Defaults to pointing east.

Clarity: I am guessing the Euler stuff is just not liking it. Any thoughts what I am being a bonehead on here?
(I’ve even saved JUST the rotation.y as a float and pulled it in that way. Debugging it certainly seems like I’ve forgotten my Quaternion rules)

    public object CaptureState()
    {
        PlayerSave playerSave = new PlayerSave();
        playerSave.position = new SerializableVector3(transform.position);
        var rot = new Vector3(transform.rotation.x, transform.rotation.y, transform.rotation.z);
        playerSave.rotation = new SerializableVector3(rot); 
        return playerSave;
    }

    public void RestoreState(object state)
    {
        PlayerSave playerSave = (PlayerSave)state;
        navMeshAgent.enabled = false;
        transform.position = playerSave.position.ToVector();
        transform.rotation = Quaternion.Euler(playerSave.rotation.ToVector());
        navMeshAgent.enabled = true;
        GetComponent<ActionScheduler>().CancelCurrentAction();
    }

    [System.Serializable]
    private class PlayerSave
    {
        public SerializableVector3 position;
        public SerializableVector3 rotation;
    }

You can’t save rotation like this. It is not what you think it is. There’s a w in there somewhere that you also need and weird stuff. You’d rather use transform.eulerAngles

// to save
playerSave.rotation = new SerializableVector3(transform.eulerAngles);

// to restore
transform.eulerAngles = playerSave.rotation.ToVector(); // I forgot the .ToVector() in my previous post...

But just saving transform.forward works fine, unless your character is leaning (rotated on x or z-axis), too

1 Like

Thanks again! I knew I was on the right track either Euler, I just did the wrong one. It’s been awhile. But this works like a charm and I am getting the hang of it better now.

Just wait until I start my full project here in a month or so. The discord is going to be on fire with my brain farts :joy:

In our game, it really really really shouldn’t be rotated on any axis but the Y axis. If it is, we did something wrong.

Apologies for not spotting the forward vs. Euler angles earlier… For completeness, if you save the transform.forward, then you can simply assign transform.forward to the value in MyData.rotation. Bear in mind that if you do that, you should probably rename rotation to forward for clarity.

Oh I know I only need forward. But for general use I won’t be using that camera/control system so I was just getting accustomed to saving it that way.

I’ve got it down now. I appreciate all the help and feedback. I even had a burst of inspiration after course work the other night and banged out like 5 pages of design doc for the project I’ll be doing :heart_eyes:

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms