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ā¦
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.
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.
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ā¦