Game engine and events

Hi all.
I wanted to know how you handle events in a game.
Using a bunch of if in each update call? (ugly bbut that is only my pov)
Custom event system as csharp provides?
Which way in you opinion/experience is the better one to avoid spaghetti code?

Maybe you could consider something like a new class called Event, and then add Events that need to happen to a list… And then run through the list using a foreach loop…

You could have sub-classes of Event, like ‘Kill_Player : Event’…

This way, you can run all sorts of behaviors with NO if statements… Like you would with enemies and such…

1 Like

The concept of “events” is tightly related to the concept of the Observer pattern.

It’s not easy to provide a complete solution without setting up a Entity/component system that can get rather complex.

If you are looking for the most straightforward path, you can use the default C# event system. But I would recommend to use Weak Events, because with the default C# events when you forget to unsubscribe an event, your instances will never be collected by the GC and this can introduce memory leaks easily.

There is also a very simple C# pub/sub library here that my work for you.

1 Like

Thanks for all your answers @raizam and @monopalle.
I see now it is not an easy part in a game engine.
At first glance i thought eventhandlers from winforms or wpf would be easybut it sounds it is not the case afterall.
I ll have a try with pubsub and if it does not suit my needs i will try to spend some time implementing my own weak ref. way.

IMHO, since you’re probably coding a game, you should consider garbage collection as well.
Events will most probably result in garbage:
Shouldn’t be much though…

I love the last line of Shawn’s conclusion:

Or you could dodge the issue by avoiding events altogether in your main gameplay code.

So… without events, how to notify for ex, all AIs a moon has exploded and is no more available to hide/build upon ?
I can’t decently make all units check every frame if the moon (and if there are 100 ? 1000 ? 1000000?) still exists, the “database” will have too much work for the same query sent many times per frame.

I would still use events / code them myself / probably use C# events with value-types as argument in your case but I’d be rather cautious about it / accept that that eventing system is an integral part of my game-engine and double check.

Check out the observer pattern on Game Programming Patterns.

For a scenario like this, the computer controlling a coordinated force, there are several things you can do…

Dont have each unit check for conditions… Check conditions once, and then filter down commands, as if a human were playing.

There are many ways of doing this ofcoarse…

And if you wont use an event or command class, another way might be having a class called AI…
AI has knowledge of all its resources, units, enemies, and win/loss conditions.
It checks 5% of all conditions per frame.
AI weighs the conditions against each other, and adjusts priorities as data rolls in…

AI passes these priorities (as an overload) to units in its Update cycle…

for example:

AI is starving for recources. AI sets “Gather new resources” as 1st priority.
Units under AI control recieve Update(“Gather new resources”)…

Units that are Fighters take “gather new resources” to mean: patrol near neareast resources.
Units that are Workers take “gather new resources” to mean: gather from nearest resource.
Units that are Buildings start build workers… etc…