Page 1 of 1

UIS save system and position saver

Posted: Sat Feb 04, 2023 6:08 am
by qest43
Untitled.png
Untitled.png (14.13 KiB) Viewed 854 times
So I have levels on different scenes. In every scene I had player prefab placed. When scene was loaded, position saver were loading player last position, and move it there, so on the start of every level, there were camera movement visible, from starting point to the loaded point, after fade out. I tried to change it a little and I deleted all player prefabs from scene, and added this code:

Code: Select all

        

            var savedGameData = SaveSystem.storer.RetrieveSavedGameData(GameManager.Instance.currentSaveSlot);
            var s = savedGameData.GetData("playerPositionKey");
            if (!string.IsNullOrEmpty(s))
            {
                var positionData = SaveSystem.Deserialize<PositionSaver.PositionData>(s);
                Instantiate(m_playerPref, positionData.position, Quaternion.identity);
            }
            else
            {
                var position = GameObject.FindGameObjectWithTag("SpawnPoint").transform.position;
                Instantiate(m_playerPref, position, Quaternion.identity);
            }
        
So if there is no any save on current save slot, player will be spawned in gameObject position with SpawnPoint tag. If there is a save on saveslot, player will be spawned on last saved position. This way I avoided this camera movement.

The problem still existed when I was using Pixel Crusher portal. So I added this code to this above:

Code: Select all

if (SaveSystem.playerSpawnpoint != null)
        {
            Instantiate(m_playerPref, SaveSystem.playerSpawnpoint.transform.position, Quaternion.identity);
        }
But I noticed player is instantiated before SaveSystem assign spawnPoint to variable. I have no idea how to solve that.

Re: UIS save system and position saver

Posted: Sat Feb 04, 2023 11:39 am
by Tony Li
Hi,

You can use the scene transition manager to fade from black when entering a new scene, which prevents the player from seeing the camera move into place.

Re: UIS save system and position saver

Posted: Sat Feb 04, 2023 11:54 am
by qest43
it doesn't help, I use it already, and it moves player after fade out. Even when I set a long fade out duration.

Re: UIS save system and position saver

Posted: Sat Feb 04, 2023 1:57 pm
by Tony Li
Do you have a fade in? It's configured on the Standard Scene Transition Manager's Enter Scene section. Also set the Save System component's Frames To Wait Before Apply Data to 0 or 1.

Re: UIS save system and position saver

Posted: Sat Feb 04, 2023 2:15 pm
by qest43
Untitled.png
Untitled.png (40.96 KiB) Viewed 828 times
Yes, and also it happens when I set frames to wait before in save system component to 0 or 1.

Re: UIS save system and position saver

Posted: Sat Feb 04, 2023 5:52 pm
by Tony Li
Does this happen in the example scene?

If not, can you send a reproduction project to tony (at) pixelcrushers.com?

Re: UIS save system and position saver

Posted: Sun Feb 05, 2023 3:24 pm
by Tony Li
Thanks for sending the repro project.

Add this code to the end of the PositionSaver's ApplyData() method:

Code: Select all

if (usePlayerSpawnpoint)
{
    var cameraPos = new Vector3(transform.position.x, transform.position.y, Camera.main.transform.position.z);
    Camera.main.transform.position = cameraPos;
    var vcam = FindObjectOfType<Cinemachine.CinemachineVirtualCamera>();
    vcam.PreviousStateIsValid = false;
}

Or, better yet, make a subclass of PositionSaver and use it for the player:

Code: Select all

public override void ApplyData(string s)
{
    base.ApplyData(s);
    var cameraPos = new Vector3(transform.position.x, transform.position.y, Camera.main.transform.position.z);
    Camera.main.transform.position = cameraPos;
    var vcam = FindObjectOfType<Cinemachine.CinemachineVirtualCamera>();
    vcam.PreviousStateIsValid = false;
}