Pokemon in Unity: Basics of Scriptable Objects
Overview: Recently I finished up a Technical Demo of the Pokemon Leveling system as part of my practice of self improvement through established game mechanics. When I decided to tackle this challenge, I decided that I wanted to use Scriptable Objects to mimic a Designer friendly system since there are over 900 Pokemon not counting the recently announced new region. For this demo, I only included the first Pokemon, Bulbasaur, and its two evolutions.
Unity’s Scriptable Objects System comes in two parts, the Script and the Object. For ease of discussion, I will call these the Constructor and the Object. The reason why Scriptable Objects are so powerful in Unity is because of the power it allows the Developers to pass on to the Designers without either having to worry about the integrity of code later. They also allow for greater customization while using less space.
For this Pokemon project, we will use Scriptable Object to create and store the Pokemon Species data. That is, all the information that is not different between pokemon of the same type but is different between different pokemon, for example Bulbasaur or Pikachu.
Here is a snippet of Base information about the Bulbasuar species of pokemon and is similar in structure to every other pokemon species. This is the basic make up of every Bulbasaur every encounter during a game. We can create Object that contains these stats.
This is the Constructor script for the the Pokemon Species Scriptable Objects. As you can see, It contains the common information that goes into each pokemon. Dex (in game catalog) number, Species Name, Type(s), Base Stats, whether it evolves and what level and into what species, and finally the moves that it can learn by gaining levels. For those familiar with the franchise, I know that there is more basic stats than are list here, but this is a ‘simple’ case.
The first line of code is key to being able to let Designers make new pokemon in the system that we will build. It adds a new section at the top of the Create menu in the Project view as well as giving the new Object a default name.
Here is an example of the Object created with the above code. Note the special icon in the top left of the Inspector, that is the icon for the Data Container Scriptable Object. It is important to remember that these Objects can not be added to a Game Object as a component and it can be brought into the Scene. These objects do persist in you game on the other hand.
When organizing your project, it may help to keep your Scriptable Objects separate from your regular scripts. I do this with an additional folder under the Scripts folder and folders for each of the different types of Scriptable Objects in the project. You can see that I am using Scriptable Objects for Moves, Pokemon Species and Types.
In the next article, we look at how to use these Objects in a game environment.