Skip to content

Commit 79dbdb7

Browse files
author
Bronson Zgeb
committed
Added ItemStack Serialization on Save
1 parent 4cc43ac commit 79dbdb7

File tree

6 files changed

+37
-7
lines changed

6 files changed

+37
-7
lines changed

UOP1_Project/Assets/ScriptableObjects/SaveSystem/SaveSystem.asset

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ MonoBehaviour:
1212
m_Script: {fileID: 11500000, guid: 428b35af66b1ab44c9cca9bb53864745, type: 3}
1313
m_Name: SaveSystem
1414
m_EditorClassIdentifier:
15-
locationDatabase: {fileID: 11400000, guid: 9abcccc3f16b8c54e984de463d147899, type: 2}
1615
_loadLocation: {fileID: 11400000, guid: 00e3063edc5902e40832ea618644c597, type: 2}
17-
_onSceneReady: {fileID: 11400000, guid: b729e40fc41dd8b4ea7aaf5c857f7186, type: 2}
16+
_playerInventory: {fileID: 11400000, guid: 59c84467f7726dc4587a8373b0936f03, type: 2}
1817
saveFilename: save.chop
1918
saveData:
20-
_locationId:
19+
_locationId: d102ba8fe3b291249aeb9de4b95c1904
20+
_itemStacks:
21+
- Guid: 0a7c779a15b2c4143ae2068725b8ece9
22+
Amount: 1

UOP1_Project/Assets/Scripts/Inventory/ScriptableObjects/Item.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
using System.Collections.Generic;
1+
using System.Collections.Generic;
22
using UnityEngine;
33
using UnityEngine.Localization;
44
// Created with collaboration from:
55
// https://forum.unity.com/threads/inventory-system.980646/
66
[CreateAssetMenu(fileName = "Item", menuName = "Inventory/Item", order = 51)]
7-
public class Item : ScriptableObject
7+
public class Item : SerializableScriptableObject
88
{
99
[Tooltip("The name of the item")]
1010
[SerializeField] private LocalizedString _name = default;

UOP1_Project/Assets/Scripts/SaveSystem/Save.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
1-
using UnityEngine;
1+
using System;
2+
using System.Collections.Generic;
3+
using UnityEngine;
24

35
/// <summary>
46
/// This class contains all the variables that will be serialized and saved to a file.<br/>
57
/// Can be considered as a save file structure or format.
68
/// </summary>
7-
[System.Serializable]
9+
[Serializable]
810
public class Save
911
{
1012
// This is test data, written according to TestScript.cs class
1113
// This will change according to whatever data that needs to be stored
1214

1315
// The variables need to be public, else we would have to write trivial getter/setter functions.
1416
public string _locationId;
17+
public List<SerializedItemStack> _itemStacks = new List<SerializedItemStack>();
1518

1619
public string ToJson()
1720
{

UOP1_Project/Assets/Scripts/SaveSystem/SaveSystem.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
public class SaveSystem : ScriptableObject
44
{
55
[SerializeField] private LoadEventChannelSO _loadLocation = default;
6+
[SerializeField] private Inventory _playerInventory;
67

78
public string saveFilename = "save.chop";
89
public Save saveData = new Save();
@@ -24,6 +25,8 @@ private void CacheLoadLocations(GameSceneSO[] locationsToLoad, bool showLoadingS
2425
{
2526
saveData._locationId = locationSo.SerializableGuid;
2627
}
28+
29+
SaveGame();
2730
}
2831

2932
public void LoadGame()
@@ -34,8 +37,15 @@ public void LoadGame()
3437
}
3538
}
3639

40+
3741
public void SaveGame()
3842
{
43+
saveData._itemStacks.Clear();
44+
foreach (var itemStack in _playerInventory.Items)
45+
{
46+
saveData._itemStacks.Add(new SerializedItemStack(itemStack.Item.SerializableGuid, itemStack.Amount));
47+
}
48+
3949
if (FileManager.WriteToFile(saveFilename, saveData.ToJson()))
4050
{
4151
Debug.Log("Save successful");
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[System.Serializable]
2+
public class SerializedItemStack
3+
{
4+
public string itemGuid;
5+
public int amount;
6+
7+
public SerializedItemStack(string itemGuid, int amount)
8+
{
9+
this.itemGuid = itemGuid;
10+
this.amount = amount;
11+
}
12+
}

UOP1_Project/Assets/Scripts/SaveSystem/SerializedItemStack.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)