How would you go about implementing an Event System? | BP and/or C++

Hey there,

this question came to my mind a few days ago and i want to hear some of you guys, who maybe already made something like this.

So first of all, what do i mean by “Event System”?

I mean a system that somehow handles all gameplay driven and changing events in the game world.

I will give you a small example:

Imagine you have a little adventure game like the legend of zelda. Now you come into a town and speak with the NPCs. They are telling you that
a certain wood bridge in the north is leading to a temple, but it is not save.

You, as the mighty hero, are still trying to cross this bridge and now the event comes in:

As soon as you hit the bridge, it falls apart. This will have an effect on all NPCs in that town, because they are now telling you that the bridge broke and now one can pass anymore.

Another one of these “events” could be, that you fought some mighty enemy and the battle blocked a road by letting stones fall down from a near mountain. Now you can’t cross the road anymore
until somethine else, later in the game, happens.

Now i’m asking myself how i would handle these kinds of things. I could just create a class for them and set some meshs and animation or something, combined with a bool for “happened” or not.

But this doesn’t seem like a good idea.

Then i thought about an EventManager. Maybe a UObject in the GameState class that uses interfaces to let the events register themselves at begin play.

I also want to have a good way to save the state of these events. If i save the game and reload it, i want to have the events that already happend to be saved and reloaded.
Getting all actors of class + saving the bool would be an option, but still not a good one in my eyes.

Letting the EventManager go through the registered events and set their bool to the saved one would be a better approach i guess, but i’m still not certain about it.

Maybe this is all totaly wrong what i am trying to do, so it would be nice if we could have a little dicussion on ways to implement a good EventSystem that handles events for me and others.
Also events that have more than one stage would be interesting.

So a small list of what this “Event System” should be used for:

  • Keeping track of events in the world
  • Saving and Reloading the events
  • Change other actors based on the events (like the NPCs text after the bridge collapsed)
  • Handle events that have more than 2 states

I am still a beginner in these kinds of game logic things and maybe i’m overthinking this, but i would love me some good discussion and ideas about it.

You don’t need to post actual code or BPs. This is just about getting ideas and systems together.

(: Thanks in advance

I’m at a similiar point at the moment where I need to keep track of an arbitrary number of events that may or may not affect each other and may be required to advance the game or not. To make things worse it’s a small environment that will change with these events and I shudder at the thought of just piling up trigger volumes and functionality, it’s going to get out of hand quickly. Can’t go into too much specifics as I’m under NDA, but a generalized discussion about how to approach such a thing architecturally would be interesting.

I know i will probably be shamed upon for saying this but the help file, emphasis on the name “Help File”

I will just leave a bump here (:

eXi, I too have been looking into this. At the moment I have a rather crude system involving some trigger volumes, various interfaces and a couple of Boolean values stored in the necessary NPCs.

I have it set up so that when the player enters the trigger volume it naturally triggers the event. It then calls the interface that changes the eventHappened Boolean stored with the various NPCs that need to know about that event.

It seems alittle crude but it’s what I’m currently working with, trying to think of ways to improve it without over complicating things.

Hope my ramblings both make sense and are helpful.

Ok, here’s one way to do it.

First up, implement a hierarchical finite state machine. These are really pretty simple, so go look up HFSM and code one of those.

The HFSM will be the driver for transitions in global (and local in some cases) state.

So to supplement this, you have a generic messaging system (which I believe UE already has, plus the AI event system?).

The key to it all is this:

Anything can bind to state transitions, either globally (state has gone from X to Y) or per transition (state Y just got entered). All of this is notified by the HFSM via the messaging system, or via an observer pattern, or both :wink:

So as actions happen out in the world, they send messages to the HFSM (lets call it the “Game State Controller” for want of a better term) and the HFSM transitions to different states accordingly. The transitions cause more messages/observers to be called depending on the state being transitioned from/to. It then becomes a simple matter of defining all the state you need in the game and making it work in the HFSM. I usually prefer to write a quick toolset to do this.

How would it work in practice?

Say your bridge falling example. You’d maybe break it down into world regions, so you’d have a “near village X” main state and a few “crossing the bridge to get to X” and “entering X” substates. So within the “crossing the bridge to get to X” state, you might have two substates “bridge ok” and “bridge down”, as the hero crosses the bridge, you hit a trigger which sends a message to the HFSM, which then transitions from “bridge ok” to “bridge down”. The villagers have all either registered as observers of the bridge down state, or have registered to receive the message the HFSM sends saying “new state is now mainstate->crossing the bridge->bridge down” and change their own behavior accordingly.

You could actually implement all this with the behavior tree code to be honest, but for this kind of thing I still prefer state machines. I guess its because there’s an actual statefulness that makes the game design easier.

So to summarize. A central system that holds state. I’d suggest hierarchically (as in HFSM) because it makes a lot of sense for complex games. Either an observer pattern based, or event/message based notification system and some tool to author the various states and transition criteria (or a file format that can be parsed).

I’ve actually used a similar system for many different types of gameplay in Unity. Plus I use the same system to drive UI state too. Its REALLY simple stuff to implement and generally pretty powerful if you can wrap your head around the states you need.

Thanks zoombaup for the insights! I was actually reading about FSM’s and also considering behavior trees for this type of thing. Guess I’m on the right track.

ok i have thought about it, but what happens when you overload the game with variables for examples monsters killed of a type to complete a quest to gain exp?, oh but wait then there is leveling and skills and quickslots, character customisation, gear, etc.

Create array or list methods for trriged any events/states in modules and use casting for Event Manager.