Class Under a Class- Game Dev Series 134

S.J. Jason Liu
3 min readSep 24, 2021

--

Objective: create custom class that inheritance from another class.

Photo by Henry Hustava on Unsplash

We have used interface in the past articles. In this tutorial, we would create a quite similar function but not exactly the same. We would inheritance from a custom class.
When would this function be common in game? As we did in the last article, this would be the main part of an RPG game. This time we would go deeper to separate different type of item in RPG game with this function.

Inheritance from item

We have created an Item script as base data to create an item. However, we might have some weapons and some foods in inventory of Player. The data of these items would not possible to be the same. That is a good time to inheritance from Item.

Create a script called Weapon, and removing the MonoBehaviour but adding Item this time.

Now we can declare what data should a weapon has.

Then make it serializable to be available to adjust in Inspector.

And with same setting, let’s create another class called Eatable.

Now we can add these type of items into our database.

Complete data of an item

Open ShopDatabase script, and create an array to both Weapon and Eatable.

Now if we add some new items, weapons, or eatables in game, we can see the specific data column of that type of item, which would contain some data from script Item.

As you can see it is not just reveal the data we created for both type of item, but also inheritance the base data from Item.
And that would be easier for you to create lots of unique and sortable items.

--

--

S.J. Jason Liu

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