Writing Data-Driven Code in Unity : Event Nodes

Happy New Year!!! I thought it would be awesome to start the new year with another post before my holidays end! Today I will be going over ‘event nodes’, which is basically modular scripts that I write to do a diversity of things in a game such as move an object, play animations or sounds, or even just to check if the player has stepped on a switch or not. I will start with the abstract EventNode class:

EventNode works similar to a node used in Behaviour Trees, where at the end of the execution you get a status/result update of the node. In a behaviour tree, the status is used to determine what to do next. If the node returns a success status, the tree then continues to execute the next node. If the node returns fail, sometimes the tree just ends there but it also depends on which composite you are using (which I will explain later). If the node returns continue, it means that the process will continue executing the same node until it returns success or fail. At the same time, we count with an error state which I use it generally to validate input. It is always good to check the input just to make debugging easier later on. You will get the status whenever you fire the UpdateNode. Note that the Update function is protected to ensure that it can only be fired by defined through the execute functions.

The reason why there’s two Execute functions it’s because there are times where I might just want to fire UpdateNode without using the status at the end. I use the status generally in composites, or for condition in other processes. The EventNode also has an Enter and Exit function. Note that these are only virtual and not abstract since not all nodes might require initialization or termination.

The next step will be then to create different event nodes. I usually inherit EventNode into a few abstract nodes, since I tend to follow a behaviour-tree like structure. This is because I like how everything can be easily organized into their own category and still used them in different processes. We will start with the Action Node, which I use to create all events I need for logic and mechanics (e.g. move objects, apply physics, play audio, spawn effects, etc). The best way to know what set of actions nodes you might need is to think about the mechanics of your game, do you move? do you have push things around? what happens when you hit an enemy? what happens when you kill an enemy?. These questions tend to give you a general idea of what you might need to begin with but I have to say that things always come up :). Here’s what the action node class looks like and an example of a child:

Another set of event nodes I make are Conditions, and just as the name might suggest these are mostly used to check values, states, or whatever you might need to check for a process to continue or end. Here is what the Condition class looks like. I tend to add a description in the editor since sometimes its handy what variables or what the purpose of that condition is.

The conditions that I tend to use a lot are the ones to check values of ints, floats, etc. I’ve tried multiple ways of making this easier on my end without re-writing code and conditions a hundred times, and this is what I came up with today. First NumberCondition will be used to extend create conditions for ints and floats. I defined a function for each condition type : equals, higher, lower. I found that this allowed me to create a new number condition with a few lines of code.

Here’s what the IntCondition class looks like:

Tada! So that’s basically it for today. The next post will be about the Sequence composite, which I tend to make things work and explode! Hopefully you can find this useful and start writing more modular code that can be extended into a variety of things that can help you build things fast and easy.



0 thoughts on “Writing Data-Driven Code in Unity : Event Nodes

Leave a Reply

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