Save Inventory with Item ID and Count

Hello everybody! Sorry for my bad english , i’m french ^^’

I create a easy item system ,and a basic script to add this item into my inventory with instanciate a Item prefab and get the variable from my item and add them to my item prefab in the inventory

but today i want to create a list of Item with Specific ID to get Varable from specific Item
to easy load and instanciate the prefab item ^^

I lear more about List, i understand how it’s work !
BUT, how can i assign a ID number to get this specific Item with this own variable?
i create a ItemDB like that:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ItemDB : MonoBehaviour {
    public Sprite[] sprites;
    public static List<Item> itemList = new List<Item>();
    // Use this for initialization
    void Start () {
     
        // ITEM 1
        Item i0 = new Item();
        i0.name = "Little Sword";
        i0.type = Item.ItemType.Weapon;
        i0.itemSprite = sprites[0];
        itemList.Add(i0);
    }
 
    // Update is called once per frame
    void Update () {
     
    }
}

cause when i pick up my item on the ground(he has a specifique ID) and instanciate my Item in my inventory, i want to get my item variable with this specific ID
like :

i.name = ItemDB.itemList[IDOFMYITEM].name;
i.type= ItemDB.itemList[IDOFMYITEM].type;
ect ect… :confused:

Thank you to reading, have a good day !

Quentin :slight_smile:

There are a few options out there.. Here’s 1 to look at that’s pretty simple you might like for this task:

Thx! I will learn more about serialization! :slight_smile:

That example is super simple, and I think it will fit well for what you’re doing.

However, since you expanded your game a bit with adding saving, you might be adding a list of items, too. That way you can look them up (either by name or by ID) for when you’re restoring them from load :slight_smile:
Have fun :slight_smile:

So! thx @methos5k for you’re help first!

I read about serialization and it’s not what i want :confused:
Cause it’s for save my inventory with mysql
so i have a 2 table :
-Slot table
-Object count

So i need to save Item with their ID or name with a list like you said :)!

So i need to know how can i do a list! Do you know a exemple with 2-3 object ?
I don’t use list or array for my game so it’s my first time ^^

Thx for reading me! :slight_smile:

EDIT: I lear more about List, i understand how it’s work !
BUT, how can i assign a ID number to get this specific Item with this own variable?
i create a ItemDB like that:

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

public class ItemDB : MonoBehaviour {

    public Sprite[] sprites;
    public static List<Item> itemList = new List<Item>();

    // Use this for initialization
    void Start () {
      
        // ITEM 1
        Item i0 = new Item();
        i0.name = "Little Sword";
        i0.type = Item.ItemType.Weapon;
        i0.itemSprite = sprites[0];
        itemList.Add(i0);

    }
  
    // Update is called once per frame
    void Update () {
      
    }
}

cause when i pick up my item on the ground(he has a specifique ID) and instanciate my Item in my inventory, i want to get my item variable with this specific ID
like :

i.name = ItemDB.itemList[IDOFMYITEM].name;
i.type= ItemDB.itemList[IDOFMYITEM].type;

So i just want to know where the ID need to be place in the list and in my script ^^

I’m a little lost reading your post. I think I know what you’re asking…
If the list is fairly simple, you can make the item ID the same spot in the list (its index)
if I had a list of fruit:
0- orange
1 - banana
2 - apple
then List[1] would be a banana.

So, if you’re loading back from the database, and it’s item 2 you’d get an apple (3rd item).
Does that help? :slight_smile:

Oh! I undertstand ! Yes it’s exactly what i want so thank you very much!

I don’t understand just one last thing

how to get this index ? for exemple i want to pick up my item on collison, so i get the componenet “item” on my object, but what i need to write to get the Index?

Sorry for my bad explain … I’m french ^^’

1 Like

well, you said that you have an itemid to instantiate it. and it’s also stored in item as ‘type’ i think, right?
Are we talking about the same thing here? lol

Overall, you’d have a slot that you can store in your database as a number. Just to correct myself a little, I guess this would be 2 numbers. The slot # (Of the inventory) and the ItemID (#) of the item that is in the slot!
and its count
then you have a list of all items you could have in the game. The index is just its spot in the list, but it could match the itemid/type ; they’re all just “in order” that way.

When you take the slot from the table, you now know where it goes (slot #) and what it is (itemid/type). You can recreate the right item and put it in the right spot :slight_smile:

Yes ! that it is ! but i don’t understand all :confused:

i undertsand the “way” to do what you said, but i don’t how do do that with c# code :confused:

in collision with the item, i get the component item, so , i can get the ItemID (also the Index)

here my code before the List

Transform newItem;
newItem = Instantiate(itemPrefab, Vector3.zero, Quaternion.identity) as Transform;
newItem.SetParent(inventorySlot, false);
ItemSlots iteminventory = newItem.GetComponent<ItemSlots>();
Item itemScene = other.GetComponent<Item>();
 iteminventory.itemDescription = itemScene.itemDescription;
iteminventory.itemID = itemScene.itemID;
iteminventory.itemSprite = itemScene.itemSprite;

but know, if i have a list , i want to do something like

Inventory.add(database.item[Here the index that i get with collide]

and that is this index who cause me trouble, i don’t understand how to get it and what a have to put in the bracket index :confused:

EDIT: okay! i think about you said, can i put a INT variable in the bracket? like the ID of the item that i get previsously with collide?

i hope that i m more explicit here lol…
and thx again to help me!:slight_smile:

Yes, you can put an int in there. In fact, you can’t put anything other than an int in there :wink:
The item ID is what I was thinking you’d put there :slight_smile:

Greaaaaat ! Thanks a lot for you’re help! I’m done with my inventory system!

Have a good night, et thx again ! you save me lol !

lol that’s great :slight_smile: Congrats. Enjoy your game

1 Like