1

In the program I am creating, I have a large amount of enums

enum kEvents
{
    Start = 0,
    End,
    EntityCreated,
}

In this program, there is a huge list of Entities. Each entity holds a list of "kEvents" that it is "Registered to"

Every time an object says "Call the Start event" I have to iterate through every single entity in the game, and find out if they are "listening" for that event.

Note: I know that c# has event handlers, but I was hoping to create this enum system from the ground up instead.

This being the case, what is the best way to:

  1. Hold the collection of enums in each entity object
  2. Check if the entity holds the triggered enum

I am wondering if Enums, being basically numbers, have a lower level, quicker way to handle this than a List<T> of objects.

Chuck Savage
  • 11,274
  • 6
  • 46
  • 65
MintyAnt
  • 2,558
  • 5
  • 22
  • 36

3 Answers3

2

Every time an object says "Call the Start event" I have to iterate through every single entity in the game, and find out if they are "listening" for that event.

You're doing it wrong! Iterating over every single object and checking if they're registered for an event is very inefficient! This is the typical Observer design pattern and there are several ways to go about this, here are a couple:

  1. Have a single event which gets raised and has an enum parameter for event type (every subscriber gets it).
  2. Have a dictionary of enums and corresponding events.

Here is what option 1 might look like:

delegate void OnEventDelegate(kEvents anEvent);
public class MyEventObservable
{
    public event OnEventDelegate OnEvent;
}

public class MyEventObserver
{
    // Constructors and all
    // ...

    public void OnEventReceived(kEvents anEvent)
    {
        switch(anEvent)
        {
            // switch through all the events and handle the ones that you need
        }
    }
}

MyEventObserver observer = new MyEventObserver();
MyEventObservable observable = new MyEventObservable();
observable.OnEvent += new OnEventDelegate(observer.OnEventReceived);

Here is option 2:

public class MyEventObservable
{
    private Dictionary<kEvents, List<IObserver>> _observers;
    
    MyEventObservable()
    {
        // Initialize the dictionary with all the events
    }
    
    public void RegisterObserver(kEvents event, IObserver observer)
    {
        _observers[event].Add(observer);
    }
}

interface class IObserver
{    
    void Notify(kEvents anEvent);
}

public MyEventObserver: IObserver
{
    // Constructors and all
    // ...
    
    // Override the IObserver
    public void Notify(kEvents anEvent)
    {
        switch(anEvent)
        {
            // switch through all the events and handle the ones that you need
        }
    }
}

MyEventObserver observer = new MyEventObserver();
MyEventObservable observable = new MyEventObservable();
observable.RegisterObserver(kEvents.Start, observer);

Option two will reduce the number of events each observer has to handle, but it comes at the cost of having to register for every event separately (which adds coding complexity). This means that option 2 will work faster because there are fewer function calls to make. Depending on how "crazy" you want to get with the performance, there might be other options that can help you to speed it up but this should set you on the right track.

P.S. I have not compiled any of the code, but it should give you a general idea of how things ought to work.

Community
  • 1
  • 1
Kiril
  • 37,748
  • 29
  • 161
  • 218
  • I did a double-take wondering why my answer had been double-posted before realizing we both quoted the same statement from the question. And that is not a co-incidence :) – Miserable Variable Apr 04 '12 at 00:32
  • Thanks for the verbose explanation of your method. I definitley should be using a switch statement, not "holding enums" :P As for your method, I will try an impliment something like it. It sort of looks like it uses event handlers, but the idea is the same. – MintyAnt Apr 05 '12 at 17:13
1

It seems to me that you want to implement some kind of publish/subscribe scheme, some centralized event sink that the Entities can subscribe to events and provide a callback when that event happens, then other Entities would Rise the event and the event sink would call all the Entities that are subscribed to that event.... but I may have not understood the question :)

Something like:

How to access controls in other class

Community
  • 1
  • 1
Jaime
  • 6,606
  • 1
  • 23
  • 41
1

Every time an object says "Call the Start event" I have to iterate through every single entity in the game, and find out if they are "listening" for that event.

If I have correctly understood what you are doing then you are doing it backwards. the knowledge of which events an entity is listening to cannot be (only) in the event itself. There must be another data structure that holds this information.

That other data structure could be something as simple as a Map from the enum constant to a list of entities.

Miserable Variable
  • 27,314
  • 13
  • 69
  • 124