Saving custom list (inventory items)

Discussion and help for Easy Save 3
Post Reply
Kosta
Posts: 6
Joined: Wed Jan 01, 2020 3:58 pm

Saving custom list (inventory items)

Post by Kosta »

I have a custom list for my inventory items.
Now I don't know how to save and load custom list (items).

I guess it needs something like this?

Code: Select all

void SaveInventory()
    {
        foreach (var i in inventoryItems)
        {
            ES3.Save<ItemManager>("ItemData", i); // I dont think this is good way to sava list ...???
        }
    }

    void LoadInventory()
    {
        inventoryItems.Clear();
        inventoryItems.Add(ES3.Load("ItemData")); // Not working 
        //How to load saved data and put that into list (inventoryItems)?
        
    }
I tried to find a tutorial or solution online but there is nothing about it...
User avatar
Joel
Moodkie Staff
Posts: 4848
Joined: Wed Nov 07, 2012 10:32 pm

Re: Saving custom list (inventory items)

Post by Joel »

Hi there,

You can find information on saving Lists in the Saving and Loading Arrays and Collections section of the Getting Started guide.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Kosta
Posts: 6
Joined: Wed Jan 01, 2020 3:58 pm

Re: Saving custom list (inventory items)

Post by Kosta »

Hi,
Thanks for your reply.
I try with that but it not working.

Error: NotSupportedException: ES3Type.type is null when trying to create an ES3Type for System.Collections.Generic.List`1[ItemManager], possibly because the element type is not supported.

Code: Select all

void SaveInventory()
    {
        ES3.Save<List<ItemManager>>("InventoryData", inventoryItems);
    }

    void LoadInventory()
    {
        inventoryItems.Clear();
        if(ES3.KeyExists("InventoryData"))
        {
            inventoryItems = ES3.Load<List<ItemManager>>("InventoryData");
        }
        else
        {
            SaveInventory();
        }
    }
User avatar
Joel
Moodkie Staff
Posts: 4848
Joined: Wed Nov 07, 2012 10:32 pm

Re: Saving custom list (inventory items)

Post by Joel »

Hi there,

Please could you show me your ItemManager class so I can understand why it is not supported?

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Kosta
Posts: 6
Joined: Wed Jan 01, 2020 3:58 pm

Re: Saving custom list (inventory items)

Post by Kosta »

Code: Select all

using System;
using UnityEngine;

[System.Serializable]
public class ItemManager : IComparable<ItemManager>
{
    public ItemType.category itemCategory;
    public ItemType.type itemType;
    public ItemType.usage itemUsage;
    [Tooltip("How much hp (or any other consume) effect on player status. +10hp or -10hp etc...")]
    public float itemConsumeAmount;
    public Sprite icon;
    public bool stackable = false;
    public int maxStackable = 8;
    [TextArea] public string itemDescription;
    public float itemDurability;
    [TextArea] public string itemContribution;

    public ItemManager(ItemType.category _itemCategory, ItemType.type _itemType, 
    ItemType.usage _itemUsage, Sprite _icon, bool _stackable, int _maxStackable, 
    string _itemDescription, float _itemDurability, string _itemContribution, float _itemConsumeAmount)
    {
        itemCategory = _itemCategory;
        itemType = _itemType;
        itemUsage = _itemUsage;
        icon = _icon;
        stackable = _stackable;
        maxStackable = _maxStackable;
        itemDescription = _itemDescription;
        itemDurability = _itemDurability;
        itemContribution = _itemContribution;
        itemConsumeAmount = _itemConsumeAmount;
    }

    public int CompareTo(ItemManager other)
    {
        if(other == null)
        {
            return 1;
        }

        return 0;
    }
}

User avatar
Joel
Moodkie Staff
Posts: 4848
Joined: Wed Nov 07, 2012 10:32 pm

Re: Saving custom list (inventory items)

Post by Joel »

Hi there,

Your class needs a parameterless constructor to be serialisable.

See the Supported Types guide for more info: https://docs.moodkie.com/easy-save-3/es ... ted-types/

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Kosta
Posts: 6
Joined: Wed Jan 01, 2020 3:58 pm

Re: Saving custom list (inventory items)

Post by Kosta »

Thanks a lot!
Empty constructor solves my problem.
Post Reply