Switching Scene and GameObjects

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
dnblank12
Posts: 14
Joined: Wed Sep 21, 2022 5:46 am

Switching Scene and GameObjects

Post 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? :?
Attachments
tests.PNG
tests.PNG (59.31 KiB) Viewed 378 times
User avatar
Tony Li
Posts: 22143
Joined: Thu Jul 18, 2013 1:27 pm

Re: Switching Scene and GameObjects

Post 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.
dnblank12
Posts: 14
Joined: Wed Sep 21, 2022 5:46 am

Re: Switching Scene and GameObjects

Post 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);
            
        }
    }

}
User avatar
Tony Li
Posts: 22143
Joined: Thu Jul 18, 2013 1:27 pm

Re: Switching Scene and GameObjects

Post by Tony Li »

Hi,

What do you mean by "other sword"? Are there two swords?
dnblank12
Posts: 14
Joined: Wed Sep 21, 2022 5:46 am

Re: Switching Scene and GameObjects

Post by dnblank12 »

Hi, sorry for the confusion, its an Axe but currently still using a prefab sword so I have two swords :D
User avatar
Tony Li
Posts: 22143
Joined: Thu Jul 18, 2013 1:27 pm

Re: Switching Scene and GameObjects

Post 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;
    ...
dnblank12
Posts: 14
Joined: Wed Sep 21, 2022 5:46 am

Re: Switching Scene and GameObjects

Post by dnblank12 »

here is the console result

Code: Select all

{"key":"ItemSaverSaverItem","sceneIndex":-1,"data":"{\"Sword\":true,\"Axe\":true,\"Shield\":true}"}]}
User avatar
Tony Li
Posts: 22143
Joined: Thu Jul 18, 2013 1:27 pm

Re: Switching Scene and GameObjects

Post 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.
dnblank12
Posts: 14
Joined: Wed Sep 21, 2022 5:46 am

Re: Switching Scene and GameObjects

Post by dnblank12 »

Many thanks Tony, it works perfectly :)
User avatar
Tony Li
Posts: 22143
Joined: Thu Jul 18, 2013 1:27 pm

Re: Switching Scene and GameObjects

Post by Tony Li »

Glad to help!
Post Reply