Page 1 of 1

Switching Scene and GameObjects

Posted: Wed Sep 21, 2022 6:19 am
by dnblank12
Hi, I have an issue regarding switching scene, after I finish a quest to equip sword and shield (by using on trigger enter) then going to the portal, how do I make the sword and shield still equip on the next scene? :?

Re: Switching Scene and GameObjects

Posted: Wed Sep 21, 2022 11:23 am
by Tony Li
Hi,

Write a custom saver with a RecordData() method that records what the player has equipped, and an ApplyData() method that equips the recorded items back onto the player.

When the save system handles a scene change (e.g., via a Scene Portal), the player GameObject in the original scene will call the customer saver's RecordData() and save the equipment state. After the save system finishes loading the new scene, it will call that scene's saver's ApplyData() to apply that same state to the player GameObject in the new scene.

Re: Switching Scene and GameObjects

Posted: Sun Oct 16, 2022 10:03 pm
by dnblank12
Hello, where do I attach the custom saver? to the player or a separate gameobject?

Edit: I managed to make it somehow working(not sure) but when loading from save slot (dialogue system menu framework) it enables the other sword. Any tips or best way to do this?

Code: Select all

using UnityEngine;

public class ItemSaver : MonoBehaviour
{
    public GameObject Sword;
    public GameObject Axe;
    public GameObject Shield;
}

Code: Select all

using System;
using UnityEngine;

namespace PixelCrushers
{

    public class SaverItem : Saver
    {


        [Serializable]
        public class Data
        {
            public bool Sword;
            public bool Axe;
            public bool Shield;
        }
        public override string RecordData()
        {
            var data = new Data();
            bool boolSword = GetComponent<ItemSaver>().Sword ? true : false;
            bool boolAxe = GetComponent<ItemSaver>().Axe ? true : false;
            bool boolShield = GetComponent<ItemSaver>().Shield ? true : false;

            data.Sword = boolSword;
            data.Axe = boolAxe;
            data.Shield = boolShield;

            return SaveSystem.Serialize(data);
        }

        public override void ApplyData(string s)
        {
            if (string.IsNullOrEmpty(s)) return;
            var data = SaveSystem.Deserialize<Data>(s);
            if(data == null) return;
            var itemsaver = GetComponent<ItemSaver>();
            itemsaver.Sword.SetActive(data.Sword);
            itemsaver.Axe.SetActive(data.Axe);
            itemsaver.Shield.SetActive(data.Shield);
            
        }
    }

}

Re: Switching Scene and GameObjects

Posted: Mon Oct 17, 2022 8:02 am
by Tony Li
Hi,

What do you mean by "other sword"? Are there two swords?

Re: Switching Scene and GameObjects

Posted: Mon Oct 17, 2022 9:16 am
by dnblank12
Hi, sorry for the confusion, its an Axe but currently still using a prefab sword so I have two swords :D

Re: Switching Scene and GameObjects

Posted: Mon Oct 17, 2022 9:30 am
by Tony Li
Temporarily tick the Debug checkboxes on the Save System and PlayerPrefs Saved Game Data Storer components. When you save the game, it will log the save data to the Console. Find the Key value of your SaverItem. Then check the values of Sword and Axe. For example, it might look like:

Code: Select all

{ "key":"Equipped", "data":{ Sword:true, Axe:false, Shield:true } }
In your SaverItem class's ApplyData() method, add some Debug.Log() lines that will tell you if it's erroring out early:

Code: Select all

public override void ApplyData(string s)
{
    if (string.IsNullOrEmpty(s)) Debug.Log($"SaverItem {key} data is empty");
    if (string.IsNullOrEmpty(s)) return;
    var data = SaveSystem.Deserialize<Data>(s);
    if(data == null) Debug.Log($"SaverItem {key} data {s} could not be deserialized");
    if(data == null) return;
    ...

Re: Switching Scene and GameObjects

Posted: Mon Oct 17, 2022 10:01 am
by dnblank12
here is the console result

Code: Select all

{"key":"ItemSaverSaverItem","sceneIndex":-1,"data":"{\"Sword\":true,\"Axe\":true,\"Shield\":true}"}]}

Re: Switching Scene and GameObjects

Posted: Mon Oct 17, 2022 10:08 am
by Tony Li
In your RecordData() method, change these lines:

Code: Select all

bool boolSword = GetComponent<ItemSaver>().Sword ? true : false;
bool boolAxe = GetComponent<ItemSaver>().Axe ? true : false;
bool boolShield = GetComponent<ItemSaver>().Shield ? true : false;
to:

Code: Select all

var itemSaver = GetComponent<ItemSaver>();
bool boolSword = itemSaver.Sword.activeSelf;
bool boolAxe = itemSaver.Axe.activeSelf;
bool boolShield = itemSaver.Shield.activeSelf;
This will save the active/inactive state of each of the GameObjects instead of whether a GameObject is or isn't assigned to the variable.

Re: Switching Scene and GameObjects

Posted: Mon Oct 17, 2022 10:49 am
by dnblank12
Many thanks Tony, it works perfectly :)

Re: Switching Scene and GameObjects

Posted: Mon Oct 17, 2022 11:08 am
by Tony Li
Glad to help!