- Home /
Combining two systems: Adding Inventory to Save Data
So my problem is a bit of a specific one.
I'm pretty inexperienced when it comes to programming, and I followed two separate tutorials to implement the save and inventory systems for my game: these two articles for saving and loading, and this tutorial for creating an inventory.
Both systems seem pretty good to me - my main problem is that I don't know how to feed the values from the inventory system into the save system (because, again, inexperienced).
Here's the Inventory file:
public class Inventory : MonoBehaviour {
public Image[] itemImages = new Image[numItemSlots];
public Item[] items = new Item[numItemSlots];
public const int numItemSlots = 16;
public void AddItem (Item itemToAdd) {
for (int i = 0; i < items.Length; i++) {
if (items[i] == null){
items[i] = itemToAdd;
itemImages[i].sprite = itemToAdd.sprite;
itemImages[i].enabled = true;
return;
}
}
}
public void RemoveItem (Item itemToRemove)
{
for (int i = 0; i < items.Length; i++) {
if (items[i] == itemToRemove) {
items[i] = null;
itemImages[i].sprite = null;
itemImages[i].enabled = false;
return;
}
}
}
}
And here's the PlayerStats:
[System.Serializable]
public class PlayerStats {
public int SceneID;
public float PlayerPosX, PlayerPosY, PlayerPosZ;
public float coinage;
public ArrayList items; //I added this preemptively because I thought it might help - it doesn't currently serve a purpose.
}
And the PlayerControl:
public class PlayerControl : MonoBehaviour
{
[HideInInspector]
public bool facingRight = true; // For determining which way the player is currently facing.
[HideInInspector]
public float moveForce = 365f; // Amount of force added to move the player left and right.
public float maxSpeed = 2f; // The fastest the player can travel in the x axis.
public bool isSitting = false;
public GameObject inventory;
private Transform groundCheck; // A position marking where to check if the player is grounded.
private bool grounded = false; // Whether or not the player is grounded.
private Animator anim; // Reference to the player's animator component.
private void Start()
{
if (GlobalControl.Instance.IsSceneBeingLoaded)
{
PlayerState.Instance.localPlayerData = GlobalControl.Instance.LocalCopyofData;
transform.position = new Vector3(
GlobalControl.Instance.LocalCopyofData.PlayerPosX,
GlobalControl.Instance.LocalCopyofData.PlayerPosY,
GlobalControl.Instance.LocalCopyofData.PlayerPosZ
+ 0.1f);
GlobalControl.Instance.IsSceneBeingLoaded = false;
}
}
void Awake()
{
// Setting up references.
groundCheck = transform.Find("groundCheck");
anim = GetComponent<Animator>();
inventory = GameObject.Find("Canvas").GetComponent<OpenInventory>().Inventory;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.F5))
{
PlayerState.Instance.localPlayerData.SceneID = SceneManager.GetActiveScene().buildIndex;
PlayerState.Instance.localPlayerData.PlayerPosX = transform.position.x;
PlayerState.Instance.localPlayerData.PlayerPosY = transform.position.y;
GlobalControl.Instance.SaveData();
}
if (Input.GetKeyDown(KeyCode.F9))
{
GlobalControl.Instance.LoadData();
GlobalControl.Instance.IsSceneBeingLoaded = true;
int whichScene = GlobalControl.Instance.LocalCopyofData.SceneID;
SceneManager.LoadSceneAsync(whichScene);
}
}
//[...]
}
I realize that there's a tutorial for saving droppable items and such immediately after the first two, but because I already have an existing Inventory, I'm not exactly sure how much it applies/how much of the work would be necessary.
I feel like it's just a matter of taking the right values from Inventory and putting them in PlayerStats and PlayerControl with the right setup - plugging in the wires, so to speak. Problem is, I don't know where to start.
Can anyone help me?
Your answer
Follow this Question
Related Questions
Help in Gesture Implementation ... 1 Answer
Saving a class List from another script 0 Answers
I need to find inventory and item system, and to save them 0 Answers
Adventure game tutorial (can't use items) 0 Answers
Save and load an inventory 0 Answers