Item Database help

Hi, I want to create an item database. I found this: Item Database - Questions & Answers - Unity Discussions . Can someone explain me or write an example code how to create Item class?

Why not use serialization

I suggest this tutorial:
https://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/persistence-data-saving-loading

Then just write your class as normal and use serialization to persist the players items for later.

**edit: Please make sure this is good practice for Unity. I know serialization is fast and efficient for windows applications but am still fairly new to Unity.

1 Like

From the link you posted it would look something like this:

using UnityEngine;

public class Item {
 
    public string _name;
    public int _cost;
    public int _weight;

}
using UnityEngine;
using System.Collections.Generic;

public class ItemDatabase {

    public Dictionary<string, Item> _items = new Dictionary<string, Item>();

}

An item system is a little more complex than it seems at first. There are the base items in the database (dictionary), then there are the instances that exist “in the world”, or in a characters’ inventory. Then there’s the visual 3D model of the item in the world or in a character’s hand. You’ll also need to decide if the item should be a MonoBehaviour or not (should it attach to a GameObject as a component).

There are many ways to do it. Your item database could even be physical items parented to a GameObject.

1 Like