UnTraDe
UnTraDe

Reputation: 3867

Game event handling system in C#

I'm building a game and trying to use an event system. This is the main idea of how it is implemented:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Blocker2
{
    public delegate void OnPlayerMoved(PlayerMovedEvent e);
    public delegate void OnPlayerSpawned(PlayerSpawnedEvent e);
    public delegate void OnBlockBreak(BlockBreakEvent e);
    public delegate void OnBlockPlaced(BlockPlacedEvent e);

    public static class EventHandler
    {
        private static List<OnPlayerMoved> _onPlayerMoved;
        private static List<OnPlayerSpawned> _onPlayerSpawned;
        private static List<OnBlockBreak> _onBlockBreak;
        private static List<OnBlockPlaced> _onBlockPlaced;


        static EventHandler()
        {

        }

        public static void Subscribe()
        {

        }

        // -------------------------- Player Related Events --------------------------
        public static void OnPlayerMoved(PlayerMovedEvent e)
        {
            foreach (OnPlayerMoved del in _onPlayerMoved)
            {
                del(e);
            }
        }

        public static void OnPlayerSpawned(PlayerSpawnedEvent e)
        {
            foreach (OnPlayerSpawned del in _onPlayerSpawned)
            {
                del(e);
            }
        }

        // -------------------------- Block Related Events --------------------------
        public static void OnBlockBreak(BlockBreakEvent e)
        {
            foreach (OnBlockBreak del in _onBlockBreak)
            {
                del(e);
            }
        }

        public static void OnBlockPlaced(BlockPlacedEvent e)
        {
            foreach (OnBlockPlaced del in _onBlockPlaced)
            {
                del(e);
            }
        }
    }
}

And there going to be alot more events, and I think this method going to make the code very very complex. There is a better way to do it? (Considering performance and maintability of the code). Thanks in advanced! Sorry for my bad english.

Upvotes: 1

Views: 2712

Answers (1)

Reed Copsey
Reed Copsey

Reputation: 564451

Why don't you use standard C# events? They will handle this the same way, since an event allows more than a single subscriber.

The standard event mechanism in C# allows multiple subscribers to subscribe to an event, and would look like:

public static event EventHandler<PlayerMovedEventArgs> PlayerMoved;

// Often, you'll have a method to raise the event:
public static void OnPlayerMoved(PlayerMovedEventArgs args)
{
    var handler = PlayerMoved;
    if (handler != null)
        handler(null, args);
}

That being said, I would recommend putting these into the class where they are related, and not having them all global/static. You could then potentially make the method to raise the event private to that class, which would allow you to keep the design more maintainable.

For example, the PlayerMoved event probably would be more appropriate within some class representing your world (or a piece of the world), and in there in a non-static fashion.

Upvotes: 4

Related Questions