in skilltree, tutorial, unity

Let’s create a skill tree – Part I (Horizon: Zero Dawn)

In this first tutorial, we are going to learn how to make a skill tree. This skill tree will be similar to the one in Horizon: Zero Dawn. Over this series of tutorial, we are going to create the skill tree data structure, to learn how to use it, and to create a tool which will allow us to get a better control over the skill tree creation and modification. At the end of this tutorial, you should be able to make something like this:

First of all, let’s start by analyzing the skill tree from Horizon: Zero Dawn.

As you can see in the video above:

  • Each skill has two states: unlocked or locked.
  • Almost every skill depends on another skill. Once you unlock one skill, you would be able to unlock the following one.
  • Every skill has a cost.

In addition to that, we need to differentiate every skill. For that, we can use an ID as you would do in a database.

Creating the Skill and SkillTree classes

In order to accomplish the requirements explained above, we will need an integer that will work as our ID, a boolean to know whether the skill is already unlocked or not, a cost and an array of skill IDs that we are going to use as dependencies. So our Skill class should be something like this:

Furthermore, we will need a new class for managing a bunch of these skills. For this, we will define the class SkillTree:

Reading the skill tree

Of course, we will need to read and write the skill tree data into a file. For this, we will create a new class called SkillTreeReader and read from and write to it in a JSON file.

In order to make it easier to look for any skill, after reading the data, we populate a dictionary where we will be able to make a search by ID.

But… how should we define our skill tree? This is not the coolest thing to do, but we will get to that on the second part of this tutorial. By now, the file should look something like this:

You can copy/paste this JSON file for having the complete skill tree structure like the one seen in Horizon: Zero Dawn. You should place this file in the /Assets/SkillTree/Data folder.

Using the skill tree

The three main functions that we are going to use within the skill tree are the following ones:

  • Knowing if a skill is unlocked
  • Knowing if a skill could be unlocked (should satisfy dependencies and cost)
  • Unlocking a skill

So the class SkillTreeReader will look like this:

We can use the function IsSkillUnlocked(int id_skill) if we want to know whether the skill is already unlocked or not. The function CanSkillBeUnlocked(int id_skill) is used for knowing if all the dependencies (node dependencies and cost) are satisfied. And the function UnlockSkill(int id_Skill) is used for setting a skill as unlocked (if it’s possible) and returns true if all went OK or false if there were any errors.

Now is up to you to replicate the skill tree on Horizon: Zero Dawn. I made it by creating a simple canvas with a bunch of buttons using these functions above. Don’t forget to comment any doubt or suggestion and feel free to share your creations in the comments section or on Twitter (@AntonioClavain).

Conclusion

This is all for this first tutorial. In the next posts, we are going to improve the method for creating the JSON file by making a node editor for the Unity Editor. We are also going to create a simple gameplay example using a skill tree and make it possible to save the progression of the player.

I hope this tutorial was engaging and useful for you guys. I just want to remind you that I am open to any suggestions, questions, and comments.

See you soon! 🙂

Leave a Reply for Adam Cancel Reply

Write a Comment

Comment

  1. Thanks for this great tutorial! I’m super interested and can’t wait to see the rest. I had a question and a comment:

    Q: ArgumentException: Cannot deserialize JSON to new instances of type ‘SkillTree.’
    I can’t yet figure out what’s going wrong here (I’m no json expert) I’ve essentially copy pasted your code and followed along but something seems to have gone wrong.

    C: My one comment/constructive feedback would be that the tutorial has a bit of “draw the rest of the f***ing owl” going on. After supplying the code (which I think is smart instead of stepping through it line by line) you do talk about it but you gloss over some parts (particularly the loading of the json.) This could be because it’s the first part and more will be revealed as each part progresses but I felt like I got a basic gist of what’s going on but it felt like we went 0-100 very quickly

    Great work, I look forward to seeing more stuff from you! Thanks again!

    • Hello Keyan,

      Sorry, but I totally forgot to say that the JSON file should be placed in the /Assets/SkillTree/Data folder. That should fix it 🙂

      Folder structure

      Feedback received! Thank you so much, and yeah, I can see that maybe I went too fast along the features of this post. I will be more focus on explaining by steps on the next one.

      Thank you for your comments 🙂