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?
Other?
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: https://blogs.msdn.microsoft.com/shawnhar/2007/07/09/delegates-events-and-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.

ggg
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ā€¦