Help !!! How to Save and Load Inventory items to the database?

thank you for reading! I was learned to make a Inventory system of gamegrind. now I’m looking to save my inventory , but still can not do it. Please help me !?

Tutorial in here :

This my Inventory script :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Inventory : MonoBehaviour {

    GameObject inventoryPanel;
    public GameObject slotPanel;
    ItemDatabase database;
    public GameObject inventorySlot;
    public GameObject inventoryItem;

    int slotAmount;
    public List<Item> items = new List<Item>();
    public List<GameObject> slots = new List<GameObject>();

    public GameObject toolTip;


    void Start () {
        database = GetComponent<ItemDatabase>();

        slotAmount = 20;
        inventoryPanel = GameObject.Find("Inventory Panel");
       
        for (int i = 0; i < slotAmount; i++)
        {
            items.Add(new Item());
            slots.Add(Instantiate(inventorySlot));
            slots[i].GetComponent<Slot>().id = i;
            slots[i].transform.SetParent(slotPanel.transform);
        }
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.U))
        {
            AddItem(5);
            AddItem(6);
            AddItem(7);
        }

    }

    public void AddItem(int id)
    {
        Item itemToAdd = database.FetchItemByID(id);
        if (itemToAdd.Stackable && CheckIfItemIsInInventory(itemToAdd))
        {
            for (int i = 0; i < items.Count; i++)
            {
                if (items[i].ID == id)
                {
                    ItemData data = slots[i].transform.GetChild(0).GetComponent<ItemData>();
                    data.amount++;
                    data.transform.GetChild(0).GetComponent<Text>().text = data.amount.ToString();
                    break;
                }
            }
        }
        else
        {
            for (int i = 0; i < items.Count; i++)
            {
                if (items[i].ID == -1)
                {
                    items[i] = itemToAdd;
                    GameObject itemObj = Instantiate(inventoryItem);
                    itemObj.GetComponent<ItemData>().item = itemToAdd;
                    itemObj.GetComponent<ItemData>().slot = i;
                    itemObj.transform.SetParent(slots[i].transform);
                    itemObj.transform.position = Vector2.zero;
                    itemObj.GetComponent<Image>().sprite = itemToAdd.Sprite;
                    itemObj.name = itemToAdd.Title;
                    ItemData data = slots[i].transform.GetChild(0).GetComponent<ItemData>();
                    data.amount = 1;
                    break;
                }
            }
        }
    }

    public void RemoveItem(int id)
    {
        Item itemToRemove = database.FetchItemByID(id);
        if (itemToRemove.Stackable && CheckIfItemIsInInventory(itemToRemove))
        {
            for (int j = 0; j < items.Count; j++)
            {
                if (items[j].ID == id)
                {
                    ItemData data = slots[j].transform.GetChild(0).GetComponent<ItemData>();
                    data.amount--;
                    data.transform.GetChild(0).GetComponent<Text>().text = data.amount.ToString();
                    if (data.amount == 0)
                    {
                        Destroy(slots[j].transform.GetChild(0).gameObject);
                        items[j] = new Item();
                        closealltab();
                        break;
                    }
                    if (data.amount == 1)
                    {
                        slots[j].transform.GetChild(0).transform.GetChild(0).GetComponent<Text>().text = "";
                        break;
                    }
                    break;
                }
            }
        }
        else
        {
            for (int i = 0; i < items.Count; i++)
            {
                if (items[i].ID != -1 && items[i].ID == id)
                {
                    Destroy(slots[i].transform.GetChild(0).gameObject);
                    items[i] = new Item();
                    break;
                }
            }
        }
    }

    bool CheckIfItemIsInInventory(Item item) {
        for (int i = 0; i < items.Count; i++)
        {
            if (items[i].ID == item.ID)
                return true;
        }
        return false;
    }
}

And this is ItemDatabase script :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LitJson;
using System.IO;

public class ItemDatabase : MonoBehaviour
{
    private List<Item> database = new List<Item>();
    private JsonData itemData;

    void Start()
    {
        itemData = JsonMapper.ToObject(File.ReadAllText(Application.dataPath + "/StreamingAssets/Items.json"));
        ConstructItemDatabase();

        //Debug.Log(FetchItemByID(0).Description);
    }

    public Item FetchItemByID(int id) {
        for (int i = 0; i < database.Count; i++)
            if (database [i].ID == id)
                return database [i];
        return null;
    }

    void ConstructItemDatabase()
    {
        for (int i = 0; i < itemData.Count; i++)
        {
            database.Add(new Item((int)itemData[i]["id"], itemData[i]["title"].ToString(), (int)itemData[i]["value"],
                (int)itemData[i]["stats"]["defence"], (int)itemData[i]["stats"]["energy"], itemData[i]["description"].ToString(),
                (bool)itemData[i]["stackable"], (int)itemData[i]["rarity"], itemData[i]["slug"].ToString()));
        }
    }
}

public class Item
{
    public int ID { get; set; }
    public string Title { get; set; }
    public int Value { get; set; }
    public int Defence { get; set; }
    public int Energy { get; set; }
    public string Description { get; set; }
    public bool Stackable { get; set; }
    public int Rarity { get; set; }
    public string Slug { get; set; }
    public Sprite Sprite { get; set; }

    public Item(int id, string title, int value, int defence, int energy, string description, bool stackable, int rarity, string slug)
    {
        this.ID = id;
        this.Title = title;
        this.Value = value;
        this.Energy = energy;
        this.Defence = defence;
        this.Description = description;
        this.Stackable = stackable;
        this.Rarity = rarity;
        this.Slug = slug;
        this.Sprite = Resources.Load<Sprite>("Sprites/Items/" + slug);

    }

    public Item()
    {
        this.ID = -1;
    }
}

ItemData script :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using System;

public class ItemData : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler, IPointerEnterHandler,IPointerExitHandler {

    public Item item;
    public int amount;
    public int slot;

    private Inventory inv;
    private UseItem useItem;
    private Tooltip tooltip;
    private Vector2 offset;


    void Start()
    {
        inv = GameObject.Find("Inventory").GetComponent<Inventory>();
        useItem = GameObject.Find("Inventory").GetComponent<UseItem>();
        tooltip = inv.GetComponent<Tooltip>();
        this.transform.SetParent(inv.slots[slot].transform);
        this.transform.position = inv.slots[slot].transform.position;
    }

    public void OnBeginDrag(PointerEventData eventData)
    {
        if (item != null) {
            offset = eventData.position - new Vector2(this.transform.position.x, this.transform.position.y);
            this.transform.SetParent(this.transform.parent.parent);
            this.transform.position = eventData.position - offset;
            GetComponent<CanvasGroup>().blocksRaycasts = false;
        }
    }

    public void OnDrag(PointerEventData eventData)
    {
        if (item != null) {
            this.transform.position = eventData.position - offset;
        }
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        this.transform.SetParent (inv.slots[slot].transform);
        this.transform.position = inv.slots [slot].transform.position;
        GetComponent<CanvasGroup>().blocksRaycasts = true;
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        tooltip.Deactivate();
    }

    public void OnPointerEnter(PointerEventData eventData)
    {
        tooltip.Activate(item);
    }
}

Thank !!! ^^

Are you receiving an error? What is the issue that you are having.

hello ! my problem problems are save inventory.
I tried writing a paragraph to save but it does not work @@

void OnGUI()
    {
        if (GUI.Button(new Rect(40, 400, 100, 40), "Save"))
            SaveInventory();
        if (GUI.Button(new Rect(40, 450, 100, 40), "Load"))
            LoadInventory();       
    }

    void SaveInventory()
    {
        for (int i = 0; i < items.Count; i++)
            PlayerPrefs.SetInt("Items " + i, items[i].ID);
        Debug.Log("Inventory saved");
    }

    void LoadInventory()
    {      
        for (int i = 0; i < items.Count; i++)          
            items[i] = PlayerPrefs.GetInt("Items " + i,-1) >= 0 ? items[PlayerPrefs.GetInt("Items " +i)]: new Item();       
        Debug.Log("Inventory loaded");
    }

What part does not work? Do you see the Debug.Log statements? And what do you mean “writing a paragraph”?

items values not saved, code on no work, I mount it in inventory script. Yes! i see the Debug.Log statements. My goal is to find a way to save the inventory above :slight_smile:

If you are modifying PlayerPrefs… you need to call PlayerPrefs.Save()