NOTAN development – Using ScriptableObjects for weapons and armors

I have started a new project in an area that is out of my comfort zone, but exciting. I am developing my first video-game! It started as a PS Vita game, but it is possible that it will be released in more platforms later on.

This is being an enriching experience for me, so I have decided to document my discoveries in my blog. I will be writing a series of posts about my progress. Let’s call the game for now NOTAN.

Today I am going to talk about Scriptable Objects. They are just data containers, but with them I will be able to edit and manipulate my objects in the Unity Editor. This will be quite helpful.

In my game I am going to have a wide catalogue of weapons and armors. The weapons are going to have a certain type of attack: blunt, slash or ranged. The armors on the other side are going to have a resistance to every kind of attack, which will be ranked Low, Med, Strong or Super.

First let’s create Enumerations for this kinds of attributes

public enum WeaponType { Blunt, Slash, Ranged };
public enum Defense { Low, Med, High, Super };

Good! Now that those are defined, let’s create our data classes

// Weapon.cs
using UnityEngine;

public class Weapon : ScriptableObject {

    // Enums
    public enum WeaponType { Blunt, Slash, Ranged };

    // Attributes
    public WeaponType type;
}
// Armor.cs
using UnityEngine;

public class Armor : ScriptableObject {

    // Enums
    public enum Defense { Low, Med, High, Super };

    // Attributes
    public Defense blunt, slash, ranged;
}

We have defined an attribute for the weapons to decide the type of attack that it will have.

We have also defined 3 attributes on the Armor class. These will define what its resistance is to every tyre of attack.

Finally we also moved the enums inside the classes. Semantically they make sense to live there.

Now that our data structures are defined, let’s see how can we create them in Unity. We will aim to create an entry in the menu bar that allows us to create new instances of our classes. To achieve this, we will add the following directive on top of each class:

[CreateAssetMenu(fileName = "Armor", menuName = "NOTAN/Armor", order = 1)]

The first attribute is the class name that will be used to create the Scriptable Object. The second attribute will be the path that will be created to achieve such instance. The last parameter is the position that this element will take inside the menu.

Now let’s take a look at our finished classes

// Weapon.cs
using UnityEngine;

[CreateAssetMenu(fileName = "Weapon", menuName = "NOTAN/Weapon", order = 1)]
public class Weapon : ScriptableObject {

    // Enums
    public enum WeaponType { Blunt, Slash, Ranged };

    // Attributes
    public WeaponType type;
}
// Armor.cs
using UnityEngine;

[CreateAssetMenu(fileName = "Armor", menuName = "NOTAN/Armor", order = 2)]
public class Armor : ScriptableObject {

    // Enums
    public enum Defense { Low, Med, High, Super };

    // Attributes
    public Defense blunt, slash, ranged;
}

With this, our scriptable object is finished. If we try to create a new element inside one of our project’s folder, we will encounter the following menu.

Wonderful! Now we can create object with scripts attached to them. This will make them much easier to manipulate and use, thanks to Unity’s inspector.

How does it look in the inspector, you ask me? Well, let’s take a look.

Great, isn’t it? Now we can change the properties of our gear without even having to touch any code. And we can easily assign our gear to another Unity scripts as simple parameters!

I hope you liked this first episode on developing with Unity. Tell me your opinion and experience about Scriptable Objects in the comments section.

Cheers!

Leave a Reply

Your email address will not be published. Required fields are marked *