Hi there,
I am trying to preserve the contents of my inventory when going from scene1 to scene2 and vice versa. No such luck. I am using the scene portals, as described in the video about the save system ( ). I hooked up a few buttons on a canvas that let me save and load and the animation state saver and the position saver are working great.
I saw a DontDestroyGameObject.cs in the Common\Wrappers\Misc\ folder. Attaching this to a gameobject however does nothing. Is there a way to prevent my inventory contents and gameobjects from disappearing upon scene transition?
scene transitition and dont destroy on load
-
- Posts: 29
- Joined: Fri Jul 17, 2020 9:19 am
Re: scene transitition and dont destroy on load
Using the save system, typically you will have separate inventory GameObjects in scene1 and scene2. Write a custom Saver component, and add it to the inventory GameObjects in scene1 and scene2.
When you use a scene portal to change from scene1 to scene2, this is what happens:
1. The save system tells the Savers in scene1 to record their data. Here, your custom Saver component in scene1 will need to record its data, which the save system will store in its memory (e.g., "player has 1 sword and 3 gold").
2. The save system unloads scene1 and loads scene2.
3. The save system tells the Savers in scene2 to apply the data stored in its memory. Here, your custom Saver will receive the data and apply it (e.g., "set player's inventory to 1 sword and 3 gold").
When you use a scene portal to change from scene1 to scene2, this is what happens:
1. The save system tells the Savers in scene1 to record their data. Here, your custom Saver component in scene1 will need to record its data, which the save system will store in its memory (e.g., "player has 1 sword and 3 gold").
2. The save system unloads scene1 and loads scene2.
3. The save system tells the Savers in scene2 to apply the data stored in its memory. Here, your custom Saver will receive the data and apply it (e.g., "set player's inventory to 1 sword and 3 gold").
-
- Posts: 29
- Joined: Fri Jul 17, 2020 9:19 am
Re: scene transitition and dont destroy on load
Hi again,
I had a crack at writing my own custom saver and attached the script to my inventory in the scene. But no luck yet in getting it to work. My inventory resets the moment I go through the portal to scene2. Returning to scene1, it also did not restore my inventory.
I got the animation saver working properly across scenes (an npc with 2 animation states, idle and death, play death when a button is pressed), so I know I have scene transition and the save system set up correctly (in accordance with the video I posted in the first post).
I have added the code of the save/load functionality (which is in InventoryManager.cs) in the comments below that was already present in the inventory asset. I know the save/load functionality of the inventory is not broken, since I have tested this out by tying the methods to some buttons. I have the custom saver script on the inventory gameobject in both scenes. They both have the same unique key (123456). Maybe the screenshot helps to clarify some things. Basically, the inventory gets populated with the initial list. You can then take out and put into the slots items that have been marked as storable.
Any suggestions?
using Language.Lua;
using PixelCrushers;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Epitybe.VRInventory; //namespace containing save load functionality
public class CustomSaverVRInventory : Saver
{
public InventoryManager inventoryManager; //I dragged VRInventory gameobject that has InventoryManager.cs as a component into the slot
public override string RecordData()
{
InventoryDataStore dataStore = new InventoryDataStore();
dataStore.inventoryData = inventoryManager.GetCurrentSlotsData();
return SaveSystem.Serialize(dataStore);
/*
///////////////// contents of InventoryManager.cs //////////////
public void SaveData()
{
InventoryDataStore dataStore = new InventoryDataStore();
dataStore.inventoryData = GetCurrentSlotsData();
var json = JsonUtility.ToJson(dataStore);
FileStream fs = new FileStream(path, FileMode.Create);
BinaryFormatter formatter = new BinaryFormatter();
try
{
formatter.Serialize(fs, json);
}
catch (SerializationException e)
{
Debug.LogError("Failed to serialize. Reason: " + e.Message);
throw;
}
finally
{
fs.Close();
}
///////////////// contents of InventoryManager.cs //////////////
*/
}
public override void ApplyData(string s)
{
if (string.IsNullOrEmpty(s)) return;
var data = SaveSystem.Deserialize<InventoryDataStore>(s);
if (data != null)
{
//populate inventory
InventoryDataStore dataStore = new InventoryDataStore();
inventoryManager.PopulateDataInSlot(dataStore.inventoryData);
}
}
/*
///////////////// contents of InventoryManager.cs //////////////
public void LoadData()
{
if (File.Exists(path))
{
FileStream fs = new FileStream(path, FileMode.Open);
InventoryDataStore dataStore = new InventoryDataStore();
try
{
BinaryFormatter formatter = new BinaryFormatter();
JsonUtility.FromJsonOverwrite((string)formatter.Deserialize(fs), dataStore);
PopulateDataInSlot(dataStore.inventoryData);
}
catch (SerializationException e)
{
Debug.LogError("Failed to deserialize. Reason: " + e.Message);
throw;
}
finally
{
fs.Close();
}
}
}
public void PopulateDataInSlot(List<InventoryData> dataList)
{
for (int i = 0; i < dataList.Count; i++)
{
if (i >= slots.Length)
{
return;
}
slots.inventoryData = dataList;
slots.UpdateSlot();
}
}
///////////////// contents of InventoryManager.cs //////////////
*/
}
I had a crack at writing my own custom saver and attached the script to my inventory in the scene. But no luck yet in getting it to work. My inventory resets the moment I go through the portal to scene2. Returning to scene1, it also did not restore my inventory.
I got the animation saver working properly across scenes (an npc with 2 animation states, idle and death, play death when a button is pressed), so I know I have scene transition and the save system set up correctly (in accordance with the video I posted in the first post).
I have added the code of the save/load functionality (which is in InventoryManager.cs) in the comments below that was already present in the inventory asset. I know the save/load functionality of the inventory is not broken, since I have tested this out by tying the methods to some buttons. I have the custom saver script on the inventory gameobject in both scenes. They both have the same unique key (123456). Maybe the screenshot helps to clarify some things. Basically, the inventory gets populated with the initial list. You can then take out and put into the slots items that have been marked as storable.
Any suggestions?
using Language.Lua;
using PixelCrushers;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Epitybe.VRInventory; //namespace containing save load functionality
public class CustomSaverVRInventory : Saver
{
public InventoryManager inventoryManager; //I dragged VRInventory gameobject that has InventoryManager.cs as a component into the slot
public override string RecordData()
{
InventoryDataStore dataStore = new InventoryDataStore();
dataStore.inventoryData = inventoryManager.GetCurrentSlotsData();
return SaveSystem.Serialize(dataStore);
/*
///////////////// contents of InventoryManager.cs //////////////
public void SaveData()
{
InventoryDataStore dataStore = new InventoryDataStore();
dataStore.inventoryData = GetCurrentSlotsData();
var json = JsonUtility.ToJson(dataStore);
FileStream fs = new FileStream(path, FileMode.Create);
BinaryFormatter formatter = new BinaryFormatter();
try
{
formatter.Serialize(fs, json);
}
catch (SerializationException e)
{
Debug.LogError("Failed to serialize. Reason: " + e.Message);
throw;
}
finally
{
fs.Close();
}
///////////////// contents of InventoryManager.cs //////////////
*/
}
public override void ApplyData(string s)
{
if (string.IsNullOrEmpty(s)) return;
var data = SaveSystem.Deserialize<InventoryDataStore>(s);
if (data != null)
{
//populate inventory
InventoryDataStore dataStore = new InventoryDataStore();
inventoryManager.PopulateDataInSlot(dataStore.inventoryData);
}
}
/*
///////////////// contents of InventoryManager.cs //////////////
public void LoadData()
{
if (File.Exists(path))
{
FileStream fs = new FileStream(path, FileMode.Open);
InventoryDataStore dataStore = new InventoryDataStore();
try
{
BinaryFormatter formatter = new BinaryFormatter();
JsonUtility.FromJsonOverwrite((string)formatter.Deserialize(fs), dataStore);
PopulateDataInSlot(dataStore.inventoryData);
}
catch (SerializationException e)
{
Debug.LogError("Failed to deserialize. Reason: " + e.Message);
throw;
}
finally
{
fs.Close();
}
}
}
public void PopulateDataInSlot(List<InventoryData> dataList)
{
for (int i = 0; i < dataList.Count; i++)
{
if (i >= slots.Length)
{
return;
}
slots.inventoryData = dataList;
slots.UpdateSlot();
}
}
///////////////// contents of InventoryManager.cs //////////////
*/
}
- Attachments
-
- screenshot.png (343.68 KiB) Viewed 785 times
Re: scene transitition and dont destroy on load
I think this code in ApplyData needs to be changed:
After you get the data from the save system, you're not using it. Instead, you're creating a new, empty dataStore object and populating the inventoryManager with it.
Code: Select all
if (data != null)
{
//populate inventory
InventoryDataStore dataStore = new InventoryDataStore();
inventoryManager.PopulateDataInSlot(dataStore.inventoryData);
}
-
- Posts: 29
- Joined: Fri Jul 17, 2020 9:19 am
Re: scene transitition and dont destroy on load
Tony, I have said it before and I will say it again: You are an absolute genius. It works! Persistent inventory across scenes and save/load on SaveSystemMethods works beautifully, Thanks again!
In case anyone is wondering, I changed the line InventoryDataStore dataStore = new InventoryDataStore(); to InventoryDataStore dataStore = data;
In case anyone is wondering, I changed the line InventoryDataStore dataStore = new InventoryDataStore(); to InventoryDataStore dataStore = data;
Re: scene transitition and dont destroy on load
Happy to help!