I don't think I understand Custom savers
Posted: Thu Mar 26, 2020 12:12 pm
Hi Tony,
Sorry to bother you again so soon but I'd been working through two issues concurrently and I've hit a dead end with this one too.
I've been setting up savers in my scene for monsters being killed/items picked up/etc. The pre-written scripts all work fine, having no issues there. But I am intending to use custom savers for information like the contents of the Inventory and what state Monsters are in (if the player killed them or saved them etc).
I have a feeling I'm not writing these custom scripts quite right as the ApplyData keeps returning blank Lists for both Inventory and MonsterTracker data. I'll post both scripts below, could you let me know what dumb mistake I'm making. Pretty sure there's something simple I'm just missing when it comes to writing these.
Here's the Monster Tracking custom saver:
public class MonsterDataSaver : Saver // Rename this class.
{
public override string RecordData()
{
string dataToSave = SaveSystem.Serialize(gameObject.GetComponent<MonsterTracker>().monsterDatabase);
return dataToSave;
}
public override void ApplyData(string s)
{
if (string.IsNullOrEmpty(s)) return;
var data = SaveSystem.Deserialize<List<InitialMonsterData.MonsterData>>(s);
if (data != null)
{
GetComponent<MonsterTracker>().monsterDatabase = data;
}
}
The MonsterDatabase is a List of a custom class made up of a string, an int and two bools. Both of those scripts are marked as serializable.
And here's the Inventory one.
[System.Serializable]
public class InventoryDataSaver : Saver // Rename this class.
{
[SerializeField]
List<int> itemIds = new List<int>(24);
public override string RecordData()
{
for (int i = 0; i < 24; i++)
{
itemIds.Add(gameObject.GetComponent<Inventory>().items.itemID);
}
string dataToSave = SaveSystem.Serialize(itemIds);
itemIds.Clear();
return dataToSave;
}
public override void ApplyData(string s)
{
if (string.IsNullOrEmpty(s)) return;
var data = SaveSystem.Deserialize<List<int>>(s);
if (data != null)
{
Debug.Log(data.Count);
List <Item> itemDatabase = gameObject.GetComponent<ItemDatabase>().itemDatabase;
for (int i = 0; i < 24; i++)
{
Debug.Log(data);
Inventory.instance.items = itemDatabase.Find(x => x.itemID == data);
}
}
}
The inventory saver I'm trying to restore the information via a list made up of the Item ID's as they are scriptable objects. The itemDatabase contains a list of every item that can appear in game. Is it okay to serialize the data on the saver itself? It's just a temporary store for the itemId's before being recorded so I didn't see much point separating it. Data.Count returns 0 btw.
Sorry to post all this in one go but I reckon I've run into some rudimentary error with both of these custom savers. Maybe I need to make reference to the key somewhere in the script etc.
Cheers,
Rob
Sorry to bother you again so soon but I'd been working through two issues concurrently and I've hit a dead end with this one too.
I've been setting up savers in my scene for monsters being killed/items picked up/etc. The pre-written scripts all work fine, having no issues there. But I am intending to use custom savers for information like the contents of the Inventory and what state Monsters are in (if the player killed them or saved them etc).
I have a feeling I'm not writing these custom scripts quite right as the ApplyData keeps returning blank Lists for both Inventory and MonsterTracker data. I'll post both scripts below, could you let me know what dumb mistake I'm making. Pretty sure there's something simple I'm just missing when it comes to writing these.
Here's the Monster Tracking custom saver:
public class MonsterDataSaver : Saver // Rename this class.
{
public override string RecordData()
{
string dataToSave = SaveSystem.Serialize(gameObject.GetComponent<MonsterTracker>().monsterDatabase);
return dataToSave;
}
public override void ApplyData(string s)
{
if (string.IsNullOrEmpty(s)) return;
var data = SaveSystem.Deserialize<List<InitialMonsterData.MonsterData>>(s);
if (data != null)
{
GetComponent<MonsterTracker>().monsterDatabase = data;
}
}
The MonsterDatabase is a List of a custom class made up of a string, an int and two bools. Both of those scripts are marked as serializable.
And here's the Inventory one.
[System.Serializable]
public class InventoryDataSaver : Saver // Rename this class.
{
[SerializeField]
List<int> itemIds = new List<int>(24);
public override string RecordData()
{
for (int i = 0; i < 24; i++)
{
itemIds.Add(gameObject.GetComponent<Inventory>().items.itemID);
}
string dataToSave = SaveSystem.Serialize(itemIds);
itemIds.Clear();
return dataToSave;
}
public override void ApplyData(string s)
{
if (string.IsNullOrEmpty(s)) return;
var data = SaveSystem.Deserialize<List<int>>(s);
if (data != null)
{
Debug.Log(data.Count);
List <Item> itemDatabase = gameObject.GetComponent<ItemDatabase>().itemDatabase;
for (int i = 0; i < 24; i++)
{
Debug.Log(data);
Inventory.instance.items = itemDatabase.Find(x => x.itemID == data);
}
}
}
The inventory saver I'm trying to restore the information via a list made up of the Item ID's as they are scriptable objects. The itemDatabase contains a list of every item that can appear in game. Is it okay to serialize the data on the saver itself? It's just a temporary store for the itemId's before being recorded so I didn't see much point separating it. Data.Count returns 0 btw.
Sorry to post all this in one go but I reckon I've run into some rudimentary error with both of these custom savers. Maybe I need to make reference to the key somewhere in the script etc.
Cheers,
Rob