Saving System from RPG and 3rd Person StateMachine

I am trying to integrate the saving system from the RPG Course to the StateMachine from the 3rd Person Traversal course.

Saving and loading “Work”. The only issue is that it doesn’t save the new location of the player. Upon loading the save, the player position is back to it’s initial starting position.

I’ve tried the capture/restore state on the PlayerStateMachine and the PlayerFreeLookState all with the same result.

Any thoughts on getting the Player location to save properly so the right location is loaded?

Saving the player position really has nothing to do with the states. If you implement the ISaveable on the PlayerStateMachine and store transform.position it should work fine. If it doesn’t you may have another issue. Show the code you used to save

1 Like

Capture/RestoreState shouldn’t be called on States, as these aren’t permanent fixtures in the game, they’re created and destroyed constantly as state changes in the game.

As @bixarrio says, PlayerStateMachine is a good place to Capture/Restore the position, although ForceReceiver may also make a good candidate (because the enemies will have a ForceReceiver as well, meaning one class covers both.

1 Like

Thank you for the response. I’ll try and post when I can.

After going back through my code I found that there was a chunk commented out that shouldn’t have been. To make a long story short. My player now loads to the correct position at which I saved. Just for my own curiosity I tried both on the ForceReceiver and on the PlayerStateMachine with successful results. Would it be better to save the object’s transform.position or the transform.position from the CharacterController?

public object CaptureState()
    return new SerializableVector3(controller.transform.position);

public void RestoreState(object state)

    SerializableVector3 position = (SerializableVector3)state;        
    controller.enabled = false;
    controller.transform.position = position.ToVector();
    controller.enabled = true;        

If they’re on the same game object, it’s the same transform. But I would save the CharacterController position since that’s the one that controls the character’s position

1 Like

Thanks for your help!

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

Privacy & Terms