Serialized Item Database Example Part 1!

addam davis
4 min readJul 23, 2021

--

Objective: Create an Item database and serialize it!

In this example, let’s say we want to build an item database that stores items. To do this we will need to define what items are. The first step to this process is to create an item class.

This item class will be used to define items. We need to delete the mono behavior because we are not going to attach this to any objects. This class will only be a reference or “blueprint” to what an item is.

Here, we create variables of what makes up an item. For this tutorial we are going to need a name, and ID, and a description. 3 traits for the item so far.

How do we make use of this? We need to create an item database behavior that is going to define items. We can create a new item by creating a new C# class “Item Database.” This class will stay MonoBehavior. Create an empty object, label it as “Item_Database” and attach the item database script to it.

Open the item database script. For this example, we want to create a dagger. We can initialize in the void start method like we have done before. Create an Item variable dagger.

Remember, we want to avoid hard coding and unnecessarily long code, so we want to use a constructor with parameters. A nice little bonus of using constructors is we can use more than one.

remember the parameters are just variables we can label as we please.

Now we want to create a new item.

We could have a function that creates items for us so we could just call that function inside the item database.

When we create an item, we can specify the traits of the item.

Then we can create a generic item.

The way we can make use of this is we can now call this method.

The problem with this is how do you retrieve the item just created. We can use a return type function return the item just created.

We can now temporarily remove the food item from the start method and add the food to the database variables.

Now replace the food item in the start method.

One of the benefits with working in Unity is something called Serialization. Serialization allows you to view custom classes in the inspector. If you made the item objects public or [SerializedField] you would not see them in the inspector.

Serializing data allows Unity to read it through the inspector. To serialize an object or custom class you can go to the class and above the class you add what’s called an attribute.

what this will allow you to do is, any variable of this type, for this example that would be Item, (dagger, fishing pole, food) we can now see in the inspector if you make them public.

The items aren’t defined, but we could fill them out if we wanted to. This could even be more beneficial to you. You could have designers now create assets or weapons or items.

This is a good place to leave it for today. I’ve shown you how to create a database for items in your game and how to serialize those objects to define in the inspector. Next time I’ll show you how to turn them into an array and add more definitions. As always, Don’t be afraid to experiment with your code, and I’ll see you in the next tutorial!

--

--

No responses yet