Switching Scene and GameObjects
Switching Scene and GameObjects
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 (59.31 KiB) Viewed 380 times
Re: Switching Scene and GameObjects
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.
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
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?
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
Hi,
What do you mean by "other sword"? Are there two swords?
What do you mean by "other sword"? Are there two swords?
Re: Switching Scene and GameObjects
Hi, sorry for the confusion, its an Axe but currently still using a prefab sword so I have two swords
Re: Switching Scene and GameObjects
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:
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
{ "key":"Equipped", "data":{ Sword:true, Axe:false, Shield:true } }
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
here is the console result
Code: Select all
{"key":"ItemSaverSaverItem","sceneIndex":-1,"data":"{\"Sword\":true,\"Axe\":true,\"Shield\":true}"}]}
Re: Switching Scene and GameObjects
In your RecordData() method, change these lines:
to:
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.
Code: Select all
bool boolSword = GetComponent<ItemSaver>().Sword ? true : false;
bool boolAxe = GetComponent<ItemSaver>().Axe ? true : false;
bool boolShield = GetComponent<ItemSaver>().Shield ? true : false;
Code: Select all
var itemSaver = GetComponent<ItemSaver>();
bool boolSword = itemSaver.Sword.activeSelf;
bool boolAxe = itemSaver.Axe.activeSelf;
bool boolShield = itemSaver.Shield.activeSelf;
Re: Switching Scene and GameObjects
Many thanks Tony, it works perfectly
Re: Switching Scene and GameObjects
Glad to help!