I have been researching saving systems and decided to start making mine using a binary formatter. For my purposes everything works great, however i’ve run into an issue with my inventory system. My Item classes that hold my items data, uses a sprite variable. Because of this, when i go to save the inventory system list of items, I’m of course getting an error because you can’t serialize sprites in the binary formatter. What are some options to get around this and or advice?
Edit:
I sort of came up with an idea and have done a little testing, what i decided to do, rather than saving the inventory itself, i saved a list of ints aka the item ids. I use a method to collect these ids and throw them into the formatter with my player data class. Then when the load button is pressed, it loads the list of ids. Then i just use a foreach loop to add each of those item ids back to the players inventory. One issue, is if the player spams the load button it will keep adding the same items over and over, but in normal situations the player will only press load once before loading into the next scene. The other issue is if they keep clicking the save button it just keeps adding the same ids over and over to the list of ints. (because the get ids method i was talking about is plugged into it)
Example of my code:
public void SavePlayerData()
{
SaveSystem.SavePlayer(this);
Debug.Log("Saved Game");
}
public void LoadPlayerData()
{
PlayerData data = SaveSystem.LoadPlayer();
currentHp = data.currentHp;
currentMp = data.currentMp;
Vector3 position;
position.x = data.playerTransform[0];
position.y = data.playerTransform[1];
position.z = data.playerTransform[2];
transform.position = position;
inventoryItemIds = data.inventoryitemID;
foreach (int itemid in inventoryItemIds)
{
ItemDB.instance.AddItem(itemid, this);
}
Debug.Log("Load Game");
}
public void GetItemIds()
{
for (int i = 0; i < inventorySlots.Count; i++)
{
inventoryItemIds.Add(inventorySlots[i].id);
Debug.Log(inventoryItemIds[i]);
}
SavePlayerData();
inventoryItemIds.Clear();
}
after all this, the new issue is, after saving then loading, i’m getting item duplicates
Clear the list of items(inventory) before loading.
I save sprites as sprite names,
for example
Item.cs → int Value…string ItemName…string Sprite…
when loading the item in the game (in general,not just Load function) i have a class,instance with the name Sprites.cs
which holds reference to all sprites (divided into groups, like weapons,armor,misc,etc…)
and i find that sprite by name in the list in the Sprites.cs.