Base of Database- Game Dev Series 133

Objective: creating database with custom class.

Let’s assume that you are going to create your first item shop in RPG game, and you need to create a database to store tons of items. What would you do?
In Unity, you can easily create your own database by using custom class.

Class item

Create a C# script and name it “Item”. Since we would create our own class, it will not inheritance any class. Remove “MonoBehaviour”.

Now we need to declare what data should an item has.

With these declaration, we can create new item in the script of shop.

Creating new item

There are multi ways to create a new item.
The first way is to create a variable of Item, and declare the data of it in method.

However, this way is taking too much time and wasting lots of space. To make it more efficient, there is another way called “Constructer”, which we can create it in our Item class.

Create a public class Item inside Item script, and pass in all data it needed.

Inside this constructer, set the passed in data as a new data of an item.

And we can create a new item again in ShopDatabase with this new way of declaration. When creating an item in this way, it should pop out the hint of what data you should put in.

As you can see, we create a same item called bread with 1 line of code instead of 5 lines.

Easier way to create items

Although it becomes quicker to create an item, it still need to input these data in script line by line. To make it more easier for game designer to create new items, we can make our custom class as “Serializable” and make it can be created in Inspector.

Above the name of class Item, add one line to make it serializable.

Then we can create a public array of item to allow us to create items in Inspector.

Back to editor, you should be able to create new item in the Inspector of ShopDatabase.

Now it would be easier for you to create more and more items to your shop.

A passionate gamer whose goal is to work in video game development.