Handling events

What do you do when you have hundreds of events to manage? And if you are checking them every frame? That can result in a big optimization problem. Here comes my solution!

post_image

Introduction

Events management are really important in the game I am working at, so we need a good and optimum way to fire them. But first of all, we need to understand the game context.

By the way, this game is Virtual Rhythm and the above cute monkey name is Bango! You can check our Twitter and maybe follow us (infinite dev love if so)

In Virtual Rhythm, you will be able to play with Bango and his friends. A lot of minigames will be available to test your rhythm habilities and have a great birthday party (Yes, it's Bango's bday!).

In this minigame, you will see 3 monkeys close to you and you have to break the planks they raise.

Creating the events

After this little introduction, let's talk about how we create and manage our events. We have an events editor scene made by Dani where we can set up everything to make every game scene work

Above we have the events timeline. You can see them in groups of three; each group refers to an specific monkey (in other minigames we will have different elements to play with, instead of monkeys).

Take a look at the first group: every monkey have 3 events. First one is the monkey anticipation, so the player can easily know which monkey is going to raise the plank. The second one is the monkey plank-raising event. That will trigger both sound and animation. And the last one is the player input event. This event states the start frame of the input. After this, an input manager will be the one calculating the time for a normal or a perfect hit!

An example of how it works:

And how do event logic works?

This is the events structure:

public class Groove
{
  public int type = 0;
  public int frameStart;
  public int frameEnd;
}

In the end, each song have at least 200 events, but the count could easily raise up to 1000 in a more complex song. Checking them in-game would result in a 1000 iteration-loop each frame!

Here is where the party begins: we store all of them in an array (okay, it is a List for now), but we use another one called buffer to manage just a few of the events.

This is how youtube videos algorithm works. Not all the video is loaded in one hit but it is in little chunks that are continuously loading and unloading while the video is playing

[SerializeField]
int bufferSize;
private List<Groove> allEvents;
private List<Groove> bufferedEvents;

for (currentEvent = 0; currentEvent < bufferSize &&
 currentEvent < totalEventsCount; ++currentEvent)
{
    bufferedEvents.Add(allEvents[currentEvent]);
}

So we create a List of n elements, chosen in the editor

buffer_size

This way, every frame we only check 5 events max to know if they need to trigger or end. And after one event ends, it's array element is deleted and a new one takes the old position.

So this is the end of my first post. I hope you liked it!