Input management with Dependency Injection

Object oriented programming has a lot of patterns that can be very useful for making games. One of those patterns is the Dependency Injection, a pattern that helps to decouple classes that would otherwise be tightly connected. So let’s take something that’s really connected and see how dependency injection can help us: the input management.

Wait what’s this Dependency Injection?


Usually if you have a thing (call it client) that uses another thing (call it service), when you change the service, then you have to also change the client. And that’s bad. Let’s say the client is your game logic and you are porting your game from pc to mobile, and that therefore you need to switch from a keyboard + mouse input to a touch one. Since all inputs are changed (perhaps radically since your WASD is now a UI element) you now need to change some input-read line in your game logic even if you used an intermediate class to get those button inputs.

The Dependency Injection way to do it instead is to have the input manager call the game logic functions. Without it knowing whose functions they are. You just set them as callbacks and call them when needed. Who sets the callbacks? The naive option is: the client. But then you still have a direct dependency between the classes. Enter the DIC: Dependency Injection Container. He takes the callbacks from the client and gives them to the service, thus eliminating the dependency between them (and adding another class to your code, that’s not a free lunch).

And what are those de-leee-gates?

Delegate

A delegate is just a way to pass a function as an argument, it can also be stored as a variable and given a type name to be checked so that only the functions that match a certain signature can be stored or passed as a delegate of a specific type.

Let’s read some Input!

    [SerializeField]
    string XbuttonName = "Fire1";
   
// other button names 

    [SerializeField]
    string LeftStickHorizontalName = "Horizontal";
    [SerializeField]
    string LeftStickVerticalName = "Vertical";
//other axis names

First of all we’ll need the names of the input buttons and axis we’re going to read, for this example I’ve used a regular xbox controller. We’ll do this with the old unity input system, not the (currently) experimental one, so we’ll need a string name for it. If you’ve read my other tutorials you know I’ve a personal feud with strings, but this is one of the few cases you really have to use them: if you are building an input manager you don’t want to force whoever uses it to edit code just to rename an input field, so you really want to have that in the inspector, which means a serialized string. Notice that for thumbsticks we’ll need two axis per stick, so two thumbsticks means four axis.

    public static InputManager instance;

    [SerializeField]
    InputManagerDIC inputDIC;

    [SerializeField]
    float triggerSensibility = 0.2f;

As for the other variables, the instance reference will be used to make this class a singleton, the inputDIC is needed to ask for the injection, and the trigger sensibility trashold will be used to get a button behaviour from an axis, because back in my days triggers were fucking buttons and I like it that way.

public delegate void buttonReaction();
public delegate void axisEffect(Vector2 axisVal);

Although we could make this all with predefined System Actions, I’d rather estabilish a more specific interface that reminds whoever writes the game logic code what is supposed to act as a button and what is supposed to act as an axis. It’s just a reminder, nothing more.

good old controller
good old controller
    public static buttonReaction XbuttonPress = delegate () { };
    //other press callbacks ...
    public static buttonReaction XbuttonPressContinuous = delegate () { };
    //other continuous callbacks 
    public static axisEffect leftStickEffect = delegate (Vector2 a) { };
    public static axisEffect rightStickEffect = delegate (Vector2 a) { };
    public static System.Action InputStartRead = delegate () { };

Each callback is initialized to an empty delegate because if for whatever reason we don’t want to use something, we don’t want a nullreference exception to pop out after the change.

Now, we can define a lot of callbacks for each Input since every button has four relevant conditions:

  • just pressed
  • pressed (continuously)
  • just released
  • released (continuously)

In this example I’ll use four buttons and the triggers and read only two condition for the buttons (just pressed and continuous press) and one for the triggers (continuous press), for each of the conditions I want to read I need to define a callback.

The same goes for what to do with thumbsticks, but in that case I just want to read a direction out of them and let the game logic interpret it.

The last callback isn’t really needed but for this tutorial I’ve also built a public repository where you can download a test scene and I need to clean the UI state at the beginning of every frame, so I want a callback for that too.

void Awake()
    {
        if (instance == null)
            instance = this;
        else
            Destroy(gameObject);
        inputDIC.LoadInputManager();
    }

As I said before this is going to be a Singleton. And at the beginning of execution we want the DIC to inject his callbacks in the InputManager, so we’ll call his loading function here.

    void Update()
    {
        InputStartRead();
        if (Input.GetButtonDown(XbuttonName))
        { XbuttonPress(); }
        //read other buttonDowns
        if (Input.GetButton(XbuttonName))
        { XbuttonPressContinuous(); }
        //read other buttons
        if (Input.GetAxis(leftTriggerName) > triggerSensibility)
        { leftTriggerPressContinuous(); }
        if (Input.GetAxis(rightTriggerName) > triggerSensibility)
        { rightTriggerPressContinuous(); }

        leftStickEffect(new Vector2(Input.GetAxis(LeftStickHorizontalName), Input.GetAxis(LeftStickVerticalName)));
        rightStickEffect(new Vector2(Input.GetAxis(RightStickHorizontalName), Input.GetAxis(RightStickVerticalName)));
    }

And at last here’s the action. At first we call the “start reading” callback, then for each button we check the relevant states. Notice that for the trigger we read an axis input and only when it’s over the trashold we’ve set before we call a callback just as if it were a regular button. From the game logic standpoint that trigger will be undistinguishable from a button, it even uses the same delegate type for the callback. For the thumbsticks instead we’ll read the two axis in a single Vector2 variable and use that to call the appropriate axisEffect callback.

How about a UI class for testing this?

a really simple ui
a really simple ui

I’ve made it as basic as it gets, sorry but no fancy stuff here:

    [SerializeField]
    Toggle xButton;
    //other toggles
    [SerializeField]
    Text rStick;
    //other texts

For each button I’ll set a toggle on and off, while for the sticks I’ll show the direction in a text. All the references are passed with serialized fields in the inspector.

    public void LogCallTLCont() { ShowLogButton(lTriggerButton, "TL Cont"); }
    public void LogCallTRCont() { ShowLogButton(rTriggerButton, "TR Cont"); }
    public void LogCallA() { ShowLogButton(aButton, "A "); }
    public void LogCallB() { ShowLogButton(bButton, "B "); }
    public void LogCallX() { ShowLogButton(xButton, "X "); }
    public void LogCallY() { ShowLogButton(yButton, "Y "); }
    public void LogCallACont() { ShowLogButton(aButton, "A Cont"); }
    public void LogCallBCont() { ShowLogButton(bButton, "B Cont"); }
    public void LogCallXCont() { ShowLogButton(xButton, "X Cont"); }
    public void LogCallYCont() { ShowLogButton(yButton, "Y Cont"); }
    public void LogCallL(Vector2 direction) { ShowLogAxis(lStick, "L stick with dir", direction); }
    public void LogCallR(Vector2 direction) { ShowLogAxis(rStick, "R stick with dir", direction); }

    void ShowLogButton(Toggle toggle, string text)
    {
        toggle.isOn = true;
        Debug.Log(text);
    }

    void ShowLogAxis(Text field, string text, Vector2 direction)
    {
        field.text = direction.ToString();
        Debug.Log(text + direction);
    }

All the callbacks are actually using the same couple of functions, logging and setting an UI element each time. But who’s going to reset all those toggles when we didn’t read the button’s release? Our reset function of course:

    public void ResetUI()
    {
        xButton.isOn = false;
        yButton.isOn = false;
        aButton.isOn = false;
        bButton.isOn = false;
        lTriggerButton.isOn = false;
        rTriggerButton.isOn = false;
        rStick.text = Vector2.zero.ToString();
        lStick.text = Vector2.zero.ToString();
    }

 It’s Injection time

dependency injection input time
dependency injection input time

Also the DIC is really simple, all it does is to set the callbacks in the InputManager, so it only needs a load function and a field to specify from which class instance it should take the callbacks:

    [SerializeField]
    UserExample target;
    public void LoadInputManager()
    {
        InputManager.XbuttonPress = target.LogCallX;
        InputManager.YbuttonPress = target.LogCallY;
        InputManager.AbuttonPress = target.LogCallA;
        InputManager.BbuttonPress = target.LogCallB;
        InputManager.XbuttonPressContinuous = target.LogCallXCont;
        InputManager.YbuttonPressContinuous = target.LogCallYCont;
        InputManager.AbuttonPressContinuous = target.LogCallACont;
        InputManager.BbuttonPressContinuous = target.LogCallBCont;
        InputManager.leftStickEffect = target.LogCallL;
        InputManager.rightStickEffect = target.LogCallR;
        InputManager.leftTriggerPressContinuous = target.LogCallTLCont;
        InputManager.rightTriggerPressContinuous = target.LogCallTRCont;
        InputManager.InputStartRead = target.ResetUI;

    }

So, as you can see the InputManager has no dependecy towards the client class and the UserExample doesn’t even know that his functions are linked to an input. Any maintenance change on either class will stop here in the DIC and will be as trivial as just changing wich callback is assigned to what variable since that’s all that can happen here.

But what if I just changed Input Settings instead of doing all that?

That’s cool and that’s also the proper way to do it (until you are not porting from pc/console to mobile). Really, until you are not changing between radically different input sources in unity, you’re better off using Unity3d’s input system to remap controls and avoid changing code. I only used the Input management as the easiest-to-explain example, if one thinks this technique is just for that, he’s totally missing the point. This technique can (and according to some people should) be used for absolutely everything.

That’s all folks

Thanks for the read. This time no copy-paste, you get a repository with the whole project already set up and ready to use here. If you have any questions or comments please do express that either in the comments here or just hit me on twitter. And if you don’t want to lose my future stuff, consider my newsletter.

P.S.: I’m currently looking for a job, if you are interested take a look at my portfolio.

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

C# Garbageless List – a 0-garbage optimization for my event dispatcher

  • Part 1: basics event system
  • Part 2: event picker drawer for in-editor usage
  • Part 3: debugging with platform dependant compilation
  • Part 4: multiple dispatchers
  • Part 5: optimization [you are here]

This time you get to see the final form!

final form
Final form, for real.

As we’ve anticipated last week, there’s a problem with my implementation of the event dispatcher. It’s garbage. No, not the dispatcher, the problem is garbage. Lots and lots of garbage generated by hidden “optimizations” the compiler makes and by the c# implementation of the delegate.

A C# garbageless list? proof or GTFO!

The data was extracted sandwitching each call between two profiler BeginSample/EndSample calls, all inside a for loop, adding each time a dinamically-generated delegate so that they were actually different functions (with different hashcodes). Now, the main source of garbage was identified and eliminated thanks to this wonderful piece by somasim, it was the use of enums as keys for my double dictionary indexed by channels/events. Each time a reference was made, the kind compiler tought to box the enum with an object that would immediately be discarded, thus generating garbage.

garbageless list profile info
garbageless list profiler info – humbly tagged as “perfection”

Step 1: Garbageless dictionary use

To avoid that boxing requires a huge change in the implementation: indexing by integers. That would be obtained by casting the enums to int when calling the function, which doesn’t even require an explicit change in the calls, thus preserving retrocompatibility of the class. Man, I love enums.
This is what our data structure initializer becomes:

Dictionary<int, Dictionary<int, GarbagelessList>> initializeDicts()

Step 2: actually implementing the C# garbageless list

The second issue was the usage of the delegate += operator to enqueue delegates one to another. And here’s where it gets absolutely painful: the usual collections in c# always generate garbage when used. I tested them a lot and then decided that I can do better. (Or actually: that I can build something more optimized to my needs)

Now, this will need to go quite low level. Here’re the rules to build a garbageless list:

  1. thou shall not instantiate classes
  2. thou shall log an error each time a slight amount of garbage is generated
  3. thou shall not use default c# implemented data structures
  4. thou shall not stop until you beat the c# dictionary
  5. thou shall never ever free a bit of memory

Wait didn’t you say “garbageless”?

Here’s the problem: allocating an array of elements will always generate garbage in c# (100 int means 440B garbage, 200 goes for 880B and so on), and even creating an empity class can’t be done without wasting 16 bytes. So, in reality, we’ll get garbage both on the instantiation and whenever our default initial capacity is exceeded.

Now, the code this time is awfully long and all needed, so I won’t explain every detail or I’d need a new series just for this. But I’ll run you through the core concepts before giving all the code to copy-paste:

  • encode everything into arrays of structs
  • use linked lists and manage their memory manually
  • use indexing to speed up the search

Also, before we start, be aware of the pros and cons: while this list doesn’t generate any garbage on regular use, it does generate garbage when constructor is called and also when the default capacity limit is passed; also, be aware that when you pass a delegate to it the function will be boxed by the compiler if you didn’t construct a delegate object in advance. And last but not least, the price for the zero garbage generation is… not freeing memory. Which means that you should budget your memory accurately when setting the default capacity variables. If you can muster a few hundred bytes of garbage every now and then, just remove the log error calls and set a reasonably small default value, so that you won’t allocate more than what peak consumption requires. If you absolutely want that 0, use a high default value.

First of all, meet the node struct:

    struct node
    {
        public gameEventHandler content;
        public int nextIndex;
        public int lastIndex;
    }

The next 9 hundreds of lines of code will be used to manage this cutie. It will be hosted into a jagged bidimensional Array of nodes, this means we can instantiate the second lot just by initializing the second field of the array, instead of using the Array.Resize call (which creates a bigger copy of the array and transforms the old one in garbage).

    void addBatch()
    {
        if (capacity > 0)
            UnityEngine.Debug.LogError("Had to extend GarbagelessList, set a higher DEFAULTCAPACITY for GarbagelessList or get garbage");
        ArrayData[capacity / DEFAULTCAPACITY] = new node[DEFAULTCAPACITY];

        for (int i = capacity; i < capacity + DEFAULTCAPACITY; i++)
        {
            setNextIndexPos(i, i + 1);
            setLastIndexAtPos(i, i - 1);
        }
        capacity += DEFAULTCAPACITY;

    }

The variablecapacity will be used to store the amount of nodes currently available, when this function is called by the constructor it has value 0, so the log error won’t trigger. To index the array data we use a int on int division that will return the highest integer smaller than the actual float result. After the instantiation is done, it’s time for the initialization, wich is taken care of inside the for. We’re building here a linked list of free nodes, so each node will link to its successor and the first one’s address will be stored inside the freeTail variable.

Now let’s look at how to get the position of a node inside a bidimensional array:

void setNextIndexPos(int key, int value)
    {
        ArrayData[key / DEFAULTCAPACITY][key % DEFAULTCAPACITY].nextIndex = value;
    }

The first operation is again an int on int division, while the second is the module operator that gives us the remainder of it. So for each integer one and only one node is indexed. I wrapped the access this way for two reasons: first, you can’t just return the node and edit it since it’s a struct, second, I experimented quite a bit before getting to the bidimensional array of structs and didn’t want to rewrite the access everywhere each time. Feel free to substitute the access functions with direct assignments/reads.

    int getFree()
    {
        //if arraydata is full, we need to expand it.
        if (Count >= capacity)
            addBatch();
        int result = freeHead;
        freeHead = getNextIndexAtPos(freeHead);
        return result;
    }

Another important bit of code is here, this is the function I use to get a free node: first it checks if there are any free nodes by comparison between the count and the current capacity of the list, then if needed it adds a new batch, and after that it updates the head of the free nodes list with the successor of the current one.

Access to the data

Now, to grant a fast access to the data I implemented an enumerator-like series of methods

    bool enumeratorJustReset = false;
    int currentIndex;
    public gameEventHandler Current
    {
        get
        {
            return getContentAtPos(currentIndex);
        }
    }


    public bool MoveNext()
    {
        if (Count == 0)
            return false;
        if (enumeratorJustReset)
        {
            enumeratorJustReset = false;
            return true;
        }
        if (currentIndex == tail)
            return false;
        currentIndex = getNextIndexAtPos(currentIndex);
        return true;
    }

    public void Reset()
    {
        currentIndex = head;
        enumeratorJustReset = true;
    }

Notice that I’ve not implemented the IEnumerator interface. Why? because that would require an object instantiation, and that would violate our 1st commandment. Instead I implemented the usual Reset-MoveNext-Current approach as methods of the class, same result, no garbage.

So, if I just stopped with that stuff (and all the accessory code to actually use it, like the add and remove functions) I’d get a zero-garbage list with horrible time consumption. By nature of a linked list, access is necessarily tape-like: you always need to traverse all the list from the head to the element you need to remove it. In my tests that meant a couple of orders of magnitude slower access when compared to standard Dictionary (especially on removal, which is where it’s required to find a specific delegate to erase).

unacceptable

So how can I solve this? Well, I cheated a bit and went on memory lane back to my computer science major years, and remembered a useful tool: indexing. Of course the index needed to allow for addition and removal and had to resist all temptations of garbage generation, so it would need to be a tree structure encoded on an array. And not just any tree structure, but a balanced one. So I opted to implement a Red-Black Tree (since every open source implementation I found relied on classes and other garbage-prone stuff). From scratch.

Fuck.

A tree of knowledge

Now, the indexing would require to identify the content of the original nodes and then use that to get the locations on the regular data array, but since you may very well add multiple identical listeners to a single event there was also the possibility of duplicates, so each index node would need to be able to refer to multiple locations. Thus an array was needed inside each index node. Meet the index’s own node:

struct indexerCouple
    {
        public int[] indexedArrayData;//-1=free slot
        public int hashValue;
        public int leftChildIndex;//default is 0
        public int rightChildIndex;//default is 0
        public int lastIndex;//default is 0
        public bool isRed;

        public int ContentCount;

        public void resize()
        {
            int oldSize = indexedArrayData.Length;
            Array.Resize<int>(ref indexedArrayData, indexedArrayData.Length * 2);
            for (int i = oldSize; i < indexedArrayData.Length; i++)
            {
                indexedArrayData[i] = -1;
            }
            UnityEngine.Debug.LogError("Had to extend indexedArrayData, set a higher DEFAULTCAPACITY for BST indexer indexedArrayData or get garbage");
        }
    }

The indexing parameter will therefore be the result of a GetHashCode function called on the gameEventHandler to be indexed. Addition and removal in an ordered tree are very simple, you just navigate the tree until you find either a leaf or a null pointer and then get one from the free list to add or put it in said list to remove.

    int findHash(int hashvalue, int currentNode)
    {
        if (currentNode == -1) return -1;
        if (getHashAtPos(currentNode) == hashvalue) return currentNode;
        return findHash(hashvalue, (getHashAtPos(currentNode) > hashvalue) ? getLeftChildIndexAtPos(currentNode) : getRightChildIndexAtPos(currentNode));
    }

As you can see the find function is actually quite simple, but it’s recursive, it calls itself for each node it goes through. If you never heard about this stuff here’s a guide to learn about it.

For RB trees insertion and deletion it’s not as simple as in lists or in plain binary search trees. If you want to get to understand what goes on with that part of the code, there’s no short cut. Here are some resources:

Basically what happens in insertion is that first I check if the node already exists, if so, I just add a new index to his array. Otherwise a new node is added to the tree and the violation of the RB properties is dealt with in a series of possible cases. When deleting, again, first I find if the node exists, then if it can just be substituted with a “lower” one I just copy the data and delete the other, otherwise the node is removed and again the RB property violations are dealt with in a series of cases.

I’ve also left a couple of debugging facilities for any problem that may eventually emerge (this optimized version is tested, but has never been used in production unlike its last version), I’d love to hear from anyone who tries to implement this code.

And with the use of this indexer we may finally access the data in our garbageless list by the means of a binary search, which is quite fast and passes the comparison with c# dictionaries.

And that’s all folks!

I thank you for bearing it with me with this ordeal of a tutorial, I know that a lot is missing but we’re already at almost a couple thousand words and probably only the truly desperate or genuinely curious made it this far. For any question on the unclear aspects of the code please don’t hesitate to contact me either through my Newsletter or my twitter account.

And by the way, I’m currently looking for a job, if you think you may have one for me, take a look at my portfolio!

using System;
using System.Collections.Generic;

public class GarbagelessList
{
    #region enumerator
    bool enumeratorJustReset = false;
    int currentIndex;
    public gameEventHandler Current
    {
        get
        {
            return getContentAtPos(currentIndex);
        }
    }


    public bool MoveNext()
    { 
        if (Count == 0)
            return false;
        if (enumeratorJustReset)
        {
            enumeratorJustReset = false;
            return true;
        }
        if (currentIndex == tail)
            return false;
        currentIndex = getNextIndexAtPos(currentIndex);
        return true;
    }

    public void Reset()
    {
        currentIndex = head;
        enumeratorJustReset = true;
    }

    public void Dispose()
    {
        throw new NotImplementedException();
    }
    #endregion


    const int DEFAULTCAPACITY = 1000;
    int capacity;
    public int Count { get; private set; }
    int head;
    int tail;
    int freeHead;
    node[][] ArrayData;
    int newIndexCache;
    struct node
    {
        public gameEventHandler content;
        public int nextIndex;//default is 0
        public int lastIndex;//default is 0
    }
    BST bstIndex;


    #region public functions
    public GarbagelessList()
    {
        ArrayData = new node[DEFAULTCAPACITY][];
        bstIndex = new BST();
        addBatch();
        head = -1;
        tail = -1;
        freeHead = 0;
        Count = 0;
    }

    public void Add(gameEventHandler toAdd)
    {
        //only on the first add we use an ad hoc setup
        if (head == -1)
        {
            head = freeHead;
            tail = freeHead;
            freeHead = getNextIndexAtPos(freeHead);
            setContentAtPos(head, toAdd);
            bstIndex.Add(head, toAdd.GetHashCode());
        }
        else
        {
            newIndexCache = getFree();
            freeHead = getNextIndexAtPos(freeHead);
            setNextIndexPos(tail, newIndexCache);
            setLastIndexAtPos(newIndexCache, tail);
            tail = newIndexCache;
            setContentAtPos(newIndexCache, toAdd);
            bstIndex.Add(newIndexCache, toAdd.GetHashCode());
        }
        ++Count;
    }

    public void Remove(gameEventHandler toRemove)
    {
        int i = bstIndex.getIndexList(toRemove.GetHashCode())[0];

        if (getContentAtPos(i) == toRemove)
        {
            if (head != i)
                setNextIndexPos(getLastIndexAtPos(i), getNextIndexAtPos(i));
            else
            { head = getNextIndexAtPos(i); }
            if (tail != i)
                setLastIndexAtPos(getNextIndexAtPos(i), getLastIndexAtPos(i));
            else
            { tail = getLastIndexAtPos(i); }

            setLastIndexAtPos(freeHead, i);
            setNextIndexPos(i, freeHead);
            freeHead = i;

            --Count;
        }
    }
    /*
    public gameEventHandler this[int key]
    {
        get
        {
            return getContentAtPos(key);
        }
        set
        {
            setContentAtPos(key, value);
        }
    }
    */
    #endregion
    #region arrayWrap
    gameEventHandler getContentAtPos(int key)
    {
        return ArrayData[key / DEFAULTCAPACITY][key % DEFAULTCAPACITY].content;
    }
    void setContentAtPos(int key, gameEventHandler value)
    {
        ArrayData[key / DEFAULTCAPACITY][key % DEFAULTCAPACITY].content = value;
    }
    int getNextIndexAtPos(int key)
    {
        return ArrayData[key / DEFAULTCAPACITY][key % DEFAULTCAPACITY].nextIndex;
    }
    void setNextIndexPos(int key, int value)
    {
        ArrayData[key / DEFAULTCAPACITY][key % DEFAULTCAPACITY].nextIndex = value;
    }
    int getLastIndexAtPos(int key)
    {
        return ArrayData[key / DEFAULTCAPACITY][key % DEFAULTCAPACITY].lastIndex;
    }
    void setLastIndexAtPos(int key, int value)
    {
        ArrayData[key / DEFAULTCAPACITY][key % DEFAULTCAPACITY].lastIndex = value;
    }
    #endregion

    #region memory management
    int getFree()
    {
        //if arraydata is full, we need to expand it.
        if (Count >= capacity)
            addBatch();
        int result = freeHead;
        freeHead = getNextIndexAtPos(freeHead);
        return result;
    }
    void addBatch()
    {
        if (capacity > 0)
            UnityEngine.Debug.LogError("Had to extend GarbagelessList, set a higher DEFAULTCAPACITY for GarbagelessList or get garbage");
        ArrayData[capacity / DEFAULTCAPACITY] = new node[DEFAULTCAPACITY];

        for (int i = capacity; i < capacity + DEFAULTCAPACITY; i++)
        {
            setNextIndexPos(i, i + 1);
            setLastIndexAtPos(i, i - 1);
        }
        capacity += DEFAULTCAPACITY;

    }
    #endregion

}


public class BST
{
    indexerCouple[][] ArrayData;
    const int DEFAULTCAPACITY = 1000;
    public BST()
    {

        ArrayData = new indexerCouple[DEFAULTCAPACITY][];
        indexlistHead = -1;
        poolHead = -1;
        capacity = 0;
        count = 0;
        addBatch();
    }

    #region indexercoupledef

    struct indexerCouple
    {
        public int[] indexedArrayData;//-1=free slot
        public int hashValue;
        public int leftChildIndex;//default is 0
        public int rightChildIndex;//default is 0
        public int lastIndex;//default is 0
        public bool isRed;

        public int ContentCount;

        public void resize()
        {
            int oldSize = indexedArrayData.Length;
            Array.Resize<int>(ref indexedArrayData, indexedArrayData.Length * 2);
            for (int i = oldSize; i < indexedArrayData.Length; i++)
            {
                indexedArrayData[i] = -1;
            }
            UnityEngine.Debug.LogError("Had to extend indexedArrayData, set a higher DEFAULTCAPACITY for BST indexer indexedArrayData or get garbage");
        }
    }

    public bool blackOrNull(int toCeck) { return (toCeck == -1) ? true : (!getIsRedAtPos(toCeck)); }

    public int Sibling(int index)
    {
        int lastIndex = getLastIndexAtPos(index);
        if (lastIndex == -1)
            return -1;
        if (index == getLeftChildIndexAtPos(lastIndex))
            return getRightChildIndexAtPos(lastIndex);
        else
            return getLeftChildIndexAtPos(lastIndex);
    }
    public bool isInternal(int index)
    {
        int lastIndex = getLastIndexAtPos(index);
        if (lastIndex == -1)
            return false;
        if (getLastIndexAtPos(lastIndex) == -1)
            return false;
        if (getRightChildIndexAtPos(lastIndex) == index && getLeftChildIndexAtPos(getLastIndexAtPos(lastIndex)) == lastIndex)
            return true;

        if (getLeftChildIndexAtPos(lastIndex) == index && getRightChildIndexAtPos(getLastIndexAtPos(lastIndex)) == lastIndex)
            return true;
        return false;
    }

    public int InternalChild(int index)
    {
        int lastIndex = getLastIndexAtPos(index);
        if (lastIndex == -1) return -1;
        if (getRightChildIndexAtPos(lastIndex) == index)
            return getLeftChildIndexAtPos(index);
        else
            return getRightChildIndexAtPos(index);

    }

    public int ExternalChild(int index)
    {
        int lastIndex = getLastIndexAtPos(index);
        if (lastIndex == -1) return -1;
        if (getRightChildIndexAtPos(lastIndex) == index)
            return getRightChildIndexAtPos(index);
        else
            return getLeftChildIndexAtPos(index);
    }


    public bool indexerContains(int index, int value)
    {
        for (int i = 0; i < getIndexedArrayDataAtPos(index).Length; i++)
        {
            if (getIndexedArrayDataAtPos(index, i) == value)
                return true;
        }
        return false;
    }
    public void indexerAdd(int index, int value)
    {
        for (int i = 0; i < getIndexedArrayDataAtPos(index).Length; i++)
        {
            if (getIndexedArrayDataAtPos(index, i) == -1)
            {
                setIndexedArrayDataIndexPos(index, i, value);
                incrementIndexedDataCountAtPos(index);
                return;
            }
        }
        resizeIndexedDataCountAtPos(index);
        indexerAdd(index, value);
    }
    public void indexerRemove(int index, int value)
    {
        for (int i = 0; i < getIndexedArrayDataAtPos(index).Length; i++)
        {
            if (getIndexedArrayDataAtPos(index, i) == value)
            {
                setIndexedArrayDataIndexPos(index, i, -1);
                decrementIndexedDataCountAtPos(index);
                return;
            }
        }
    }
    #endregion

    int indexlistHead;
    int poolHead;
    int capacity;
    int count;
    #region query
    public int[] getIndexList(int hashvalue)
    {
        int result = findHash(hashvalue, indexlistHead);
        if (result == -1)
            throw new KeyNotFoundException();
        else
            return getIndexedArrayDataAtPos(result);
    }
    int findHash(int hashvalue, int currentNode)
    {
        if (currentNode == -1) return -1;
        if (getHashAtPos(currentNode) == hashvalue) return currentNode;
        return findHash(hashvalue, (getHashAtPos(currentNode) > hashvalue) ? getLeftChildIndexAtPos(currentNode) : getRightChildIndexAtPos(currentNode));
    }
    int findHashParent(int hashvalue, int currentNode, int parentNode)
    {
        if (currentNode == -1) return parentNode;
        return findHashParent(hashvalue, (getHashAtPos(currentNode) > hashvalue) ? getLeftChildIndexAtPos(currentNode) : getRightChildIndexAtPos(currentNode), currentNode);
    }

    #endregion

    #region pool Management
    void addBatch()
    {
        if (capacity > 0)
            UnityEngine.Debug.LogError("Had to extend BST, set a higher DEFAULTCAPACITY for BST or get garbage");

        ArrayData[capacity / DEFAULTCAPACITY] = new indexerCouple[DEFAULTCAPACITY];

        poolHead = capacity;
        for (int i = capacity; i < capacity + DEFAULTCAPACITY; i++)
        {
            setLastIndexAtPos(i, i + 1);
            setHashIndexPos(i, -1);
            setLeftChildIndexAtPos(i, -1);
            setRightChildIndexAtPos(i, -1);
            int[] toBeSet = new int[DEFAULTCAPACITY];
            for (int j = 0; j < DEFAULTCAPACITY; j++)
            {
                toBeSet[j] = -1;
            }
            setIndexedArrayDataAtPos(i, toBeSet);
        }
        capacity += DEFAULTCAPACITY;

    }

    int makeNew()
    {
        if (poolHead < capacity)
        {
            int result = poolHead;
            poolHead = getLastIndexAtPos(poolHead);
            setLastIndexAtPos(result, -1);
            resetIndexedDataCountAtPos(result);
            return result;
        }
        else
        {
            addBatch();
            return makeNew();
        }
    }
    void dispose(int toDispose)
    {
        setLastIndexAtPos(toDispose, poolHead);
        poolHead = toDispose;
    }
    #endregion
    #region rotation
    void rotateLeft(int parent)
    {
        //x= parent, y=rightchild
        int rightChildIndex = getRightChildIndexAtPos(parent);
        //move rightchild to his new parent
        if (parent != indexlistHead)
        {
            if (getLeftChildIndexAtPos(getLastIndexAtPos(parent)) == parent)
                setLeftChildIndexAtPos(getLastIndexAtPos(parent), rightChildIndex);
            else
                setRightChildIndexAtPos(getLastIndexAtPos(parent), rightChildIndex);
        }
        else
        {
            indexlistHead = rightChildIndex;
        }
        setLastIndexAtPos(rightChildIndex, getLastIndexAtPos(parent));

        int B = getLeftChildIndexAtPos(rightChildIndex);

        //move parent as the new rightchild's leftchild
        setLeftChildIndexAtPos(rightChildIndex, parent);
        setLastIndexAtPos(parent, rightChildIndex);

        //move B to his new parent
        setRightChildIndexAtPos(parent, B);
        if (B != -1)
            setLastIndexAtPos(B, parent);

    }

    void rotateRight(int parent)
    {
        //x= parent, y=leftchild
        int leftChildIndex = getLeftChildIndexAtPos(parent);

        //move leftChild to his new parent
        if (parent != indexlistHead)
        {
            if (getLeftChildIndexAtPos(getLastIndexAtPos(parent)) == parent)
                setLeftChildIndexAtPos(getLastIndexAtPos(parent), leftChildIndex);
            else
                setRightChildIndexAtPos(getLastIndexAtPos(parent), leftChildIndex);
        }
        else
        {
            indexlistHead = leftChildIndex;
        }
        setLastIndexAtPos(leftChildIndex, getLastIndexAtPos(parent));

        int B = getRightChildIndexAtPos(leftChildIndex);

        //move parent as the new leftchild's rightchild
        setRightChildIndexAtPos(leftChildIndex, parent);
        setLastIndexAtPos(parent, leftChildIndex);

        //move B to his new parent
        setLeftChildIndexAtPos(parent, B);
        if (B != -1)
            setLastIndexAtPos(B, parent);

    }
    /// <summary>
    /// revert child-parent relationship through rotation
    /// </summary>
    /// <param name="parent"></param>
    /// <param name="child"></param>
    void rotationSwap(int parent, int child)
    {
        if (getLeftChildIndexAtPos(parent) == child)
            rotateRight(parent);
        else rotateLeft(parent);
    }
    #endregion
    #region insertion
    public void Add(int indexedArrayElement, int hashvalue)
    {
        if (indexlistHead == -1)
        {
            indexlistHead = makeNew();
            setHashIndexPos(indexlistHead, hashvalue);

            indexerAdd(indexlistHead, indexedArrayElement);
            setIsRedAtPos(indexlistHead, false);

        }
        else
        {
            int holder = findHash(hashvalue, indexlistHead);
            if (holder != -1)
            {
                if (!indexerContains(holder, indexedArrayElement))
                {
                    indexerAdd(holder, indexedArrayElement);
                }
            }
            else
            {
                holder = makeNew();
                setHashIndexPos(holder, hashvalue);
                indexerAdd(holder, indexedArrayElement);
                redBlackInsertion(holder);
            }
        }
        ++count;
    }
    /// <summary>
    /// ensure that this is called only when there is no duplicate for toAdd hashvalue in the tree and tree is not empity
    /// </summary>
    /// <param name="toAdd"></param>
    void redBlackInsertion(int toAdd)
    {

        setIsRedAtPos(toAdd, true);

        int parent = findHashParent(getHashAtPos(toAdd), indexlistHead, -1);
        setLastIndexAtPos(toAdd, parent);
        if (getHashAtPos(parent) > getHashAtPos(toAdd))
            setLeftChildIndexAtPos(parent, toAdd);
        else
            setRightChildIndexAtPos(parent, toAdd);
        if (getIsRedAtPos(parent))
            fixRBPropertyViolations(toAdd);
    }
    void fixRBPropertyViolations(int current)
    {
        if (getIsRedAtPos(current))
        {
            if (getLastIndexAtPos(current) == -1)
                setIsRedAtPos(current, false);//if this is root, just recolor it and double reds are solved
            else if (getIsRedAtPos(getLastIndexAtPos(current)))//if parent of current is black there is no double red violation
            {
                //if parent of current is red, then it has at least a grandparent
                int parent = getLastIndexAtPos(current);
                int uncle = Sibling(parent);
                int grandparent = getLastIndexAtPos(parent);

                if (uncle != -1)
                    if (getIsRedAtPos(uncle) && (!getIsRedAtPos(grandparent)))
                    {//case 1, just switch color and recur
                        setIsRedAtPos(parent, false);
                        setIsRedAtPos(uncle, false);
                        setIsRedAtPos(grandparent, true);
                        fixRBPropertyViolations(grandparent);
                        return;
                    }
                if (isInternal(current))
                {
                    //case 2
                    rotationSwap(parent, current);
                    fixRBPropertyViolations(parent);
                    return;
                }
                //case 3
                setIsRedAtPos(grandparent, true);
                setIsRedAtPos(parent, false);
                rotationSwap(grandparent, parent);
            }

        }

    }
    #endregion
    #region deletion
    public void Remove(int indexedArrayElement, int hashvalue)
    {
        if (indexlistHead == -1)
            throw new KeyNotFoundException();
        int holder = findHash(hashvalue, indexlistHead);
        if (holder == -1)
            throw new KeyNotFoundException();
        else
        {
            if (!indexerContains(holder, indexedArrayElement))
                throw new KeyNotFoundException();
            else
            {
                indexerRemove(holder, indexedArrayElement);
                if (getIndexedDataCountAtPos(holder) <= 0)
                {
                    Remove(holder);
                    --count;
                }

            }
        }
    }
    int getInOrderNextInSubTree(int node)
    {
        if (getRightChildIndexAtPos(node) == -1)
            return -1;

        int result = getRightChildIndexAtPos(node);
        while (getLeftChildIndexAtPos(result) != -1)
            result = getLeftChildIndexAtPos(result);
        return result;
    }
    int getInOrderPreviousInSubTree(int node)
    {
        if (getLeftChildIndexAtPos(node) == -1)
            return -1;

        int result = getLeftChildIndexAtPos(node);
        while (getRightChildIndexAtPos(result) != -1)
            result = getRightChildIndexAtPos(result);
        return result;
    }
    void Remove(int toRemove)
    {
        //if has 2 child, switch places with next.
        if (getRightChildIndexAtPos(toRemove) != -1 && getLeftChildIndexAtPos(toRemove) != -1)
        {
            int toSwitchWith = getInOrderNextInSubTree(toRemove);
            setHashIndexPos(toRemove, getHashAtPos(toSwitchWith));
            setIndexedArrayDataAtPos(toRemove, getIndexedArrayDataAtPos(toSwitchWith));
            Remove(toSwitchWith);
            return;
        }
        else
        {

            //if is red leaf, remove
            if (getRightChildIndexAtPos(toRemove) == -1 && getLeftChildIndexAtPos(toRemove) == -1)
            {
                int parent = getLastIndexAtPos(toRemove);
                if (parent != -1)//if isn't root
                {
                    int sibling = Sibling(toRemove);
                    if (getLeftChildIndexAtPos(parent) == toRemove)//if it's a left child
                    {
                        setLeftChildIndexAtPos(parent, -1);
                    }
                    else//if it's a right child
                    {
                        setRightChildIndexAtPos(parent, -1);
                    }

                    if (!getIsRedAtPos(toRemove))
                        solveDoubleBlack(parent, toRemove, sibling);
                }
                else
                {//if is root, and has no children, delete tree
                    indexlistHead = -1;
                }
                dispose(toRemove);

                return;
            }
            //if has one child
            if (getLeftChildIndexAtPos(toRemove) != -1)
                deleteWithChild(getLeftChildIndexAtPos(toRemove));
            else
                deleteWithChild(getRightChildIndexAtPos(toRemove));
        }

    }

    /// <summary>
    /// substitute the toDestroy node with the source node inside the tree, preserving all the content and label data of source, with flags for each tie
    /// </summary>
    /// <param name="source"></param>
    /// <param name="toDestroy"></param>
    void substitute(int source, int toDestroy, bool preserveLast = false, bool preserveLeft = false, bool preserveRight = false)
    {
        if (preserveLast)
        {
            if (getLastIndexAtPos(toDestroy) != -1)
            {
                if (getLeftChildIndexAtPos(getLastIndexAtPos(toDestroy)) == toDestroy)
                    setLeftChildIndexAtPos(getLastIndexAtPos(toDestroy), source);
                else
                    setRightChildIndexAtPos(getLastIndexAtPos(toDestroy), source);
            }
            setLastIndexAtPos(source, getLastIndexAtPos(toDestroy));
        }
        if (preserveRight)
        {
            if (getRightChildIndexAtPos(toDestroy) != -1)
            {
                setLastIndexAtPos(getRightChildIndexAtPos(toDestroy), source);
            }
            setRightChildIndexAtPos(source, getRightChildIndexAtPos(toDestroy));
        }
        if (preserveLeft)
        {
            if (getLeftChildIndexAtPos(toDestroy) != -1)
            {
                setLastIndexAtPos(getLeftChildIndexAtPos(toDestroy), source);
            }
            setLeftChildIndexAtPos(source, getLeftChildIndexAtPos(toDestroy));
        }
        dispose(toDestroy);

    }

    void deleteWithChild(int child)
    {
        if (getIsRedAtPos(child))
        {
            setIsRedAtPos(child, false);
            substitute(child, getLastIndexAtPos(child), preserveLast: true);
            return;
        }
        else
        {
            substitute(child, getLastIndexAtPos(child), preserveLast: true);
            int parent = getLastIndexAtPos(child);
            if (!getIsRedAtPos(parent))
            {
                //case when there's double black
                if (parent != -1)
                    solveDoubleBlack(parent, child, Sibling(child));
            }
        }
    }

    void solveDoubleBlack(int doubleBlackParent, int child, int sibling)
    {
        //if is root, stop it
        if (doubleBlackParent == -1)
            return;
        //if is chain with red parent, just make it black
        if (sibling == -1 && getIsRedAtPos(doubleBlackParent))
        { setIsRedAtPos(doubleBlackParent, false); return; }
        //if is chain with black parent, just recur on him
        if (sibling == -1 && !getIsRedAtPos(doubleBlackParent))
        { solveDoubleBlack(getLastIndexAtPos(doubleBlackParent), doubleBlackParent, Sibling(doubleBlackParent)); return; }

        //otherwise, both parent and sibling exist, deal with doubleblack
        recognizeDoubleBlackCase(doubleBlackParent, child, sibling);

    }

    void recognizeDoubleBlackCase(int Parent, int doubleBlack, int sibling)
    {
        //PrintPretty(Parent, " parent", true, false);
        //PrintPretty(doubleBlack, " doubleblack ", true, false);
        //PrintPretty(sibling, " sibling", true, false);
        if ((!getIsRedAtPos(Parent)) && (!blackOrNull(sibling)))
            solveCaseOne(Parent, doubleBlack, sibling);
        else if ((getIsRedAtPos(Parent)) && blackOrNull(sibling) && (blackOrNull(InternalChild(sibling)) && blackOrNull(ExternalChild(sibling))))
            solveCaseTwo(Parent, doubleBlack, sibling);
        else if ((!getIsRedAtPos(Parent)) && blackOrNull(sibling) && (blackOrNull(InternalChild(sibling)) && blackOrNull(ExternalChild(sibling))))
            solveCaseThree(Parent, doubleBlack, sibling);
        else if (blackOrNull(sibling) && ((!blackOrNull(InternalChild(sibling))) && blackOrNull(ExternalChild(sibling))))
            solveCaseFour(Parent, doubleBlack, sibling);
        else if (blackOrNull(sibling) && (!blackOrNull(ExternalChild(sibling))))
            solveCaseFive(Parent, doubleBlack, sibling);
        else
            throw new InvalidOperationException(" unrecognized case with p red:" + getIsRedAtPos(Parent) + " sibling blackornull:" + blackOrNull(sibling) + " internal blackornull:" + blackOrNull(InternalChild(sibling)) + " external blackornull:" + blackOrNull(ExternalChild(sibling)));
    }
    void solveCaseOne(int Parent, int doubleBlack, int sibling)
    {
        int futureSibling = InternalChild(sibling);
        bool temp = getIsRedAtPos(Parent);
        setIsRedAtPos(Parent, getIsRedAtPos(sibling));
        setIsRedAtPos(sibling, temp);
        rotationSwap(Parent, sibling);
        solveDoubleBlack(Parent, doubleBlack, futureSibling);
    }
    void solveCaseTwo(int Parent, int doubleBlack, int sibling)
    {
        setIsRedAtPos(sibling, true);
        setIsRedAtPos(Parent, false);
    }
    void solveCaseThree(int Parent, int doubleBlack, int sibling)
    {
        setIsRedAtPos(sibling, true);
        solveDoubleBlack(getLastIndexAtPos(Parent), Parent, Sibling(Parent));
    }
    void solveCaseFour(int Parent, int doubleBlack, int sibling)
    {
        int futureSibling = InternalChild(sibling);
        setIsRedAtPos(InternalChild(sibling), false);
        setIsRedAtPos(sibling, true);
        rotationSwap(sibling, InternalChild(sibling));
        printRBTree();
        isRBTree();
        solveCaseFive(Parent, doubleBlack, futureSibling);
    }
    void solveCaseFive(int Parent, int doubleBlack, int sibling)
    {
        setIsRedAtPos(sibling, getIsRedAtPos(Parent));
        setIsRedAtPos(Parent, false);
        setIsRedAtPos(ExternalChild(sibling), false);

        rotationSwap(Parent, sibling);

    }

    #endregion
    #region debug
    public bool isRBTree() { int pathcountvar = 0; return isRBTree(indexlistHead, out pathcountvar); }
    bool isRBTree(int subTreeHead, out int pathCount)
    {

        if (subTreeHead == -1)
        {
            pathCount = 0;
            return true;
        }
        int lpathCount;
        bool left = isRBTree(getLeftChildIndexAtPos(subTreeHead), out lpathCount);
        int rpathCount;
        bool right = isRBTree(getRightChildIndexAtPos(subTreeHead), out rpathCount);
        pathCount = lpathCount;

        if (getLastIndexAtPos(subTreeHead) == -1)
        {
            pathCount = 0;
            if (getIsRedAtPos(subTreeHead))
            {
                UnityEngine.Debug.LogError("Red root detected! on hash " + getHashAtPos(subTreeHead));
                return false;
            }
        }
        else
        {
            if (!getIsRedAtPos(subTreeHead))
            { ++pathCount; }
            else
                if (getIsRedAtPos(getLastIndexAtPos(subTreeHead)))
            {
                UnityEngine.Debug.LogError("Double red detected! on hash " + getHashAtPos(subTreeHead));
                return false;
            }
        }
        if (lpathCount != rpathCount)
            UnityEngine.Debug.LogError("black child count not matching on hash " + getHashAtPos(subTreeHead));

        return (lpathCount == rpathCount && left && right);

    }
    public void printRBTree()
    {
        if (indexlistHead == -1)
            UnityEngine.Debug.Log(" empity ---");
        else
        {
            UnityEngine.Debug.Log(" BST: ");
            PrintPretty(indexlistHead, "", true, true);
        }

    }
    public void PrintPretty(int index, string indent, bool last, bool recursive)
    {
        string result = indent;
        if (last)
        {
            result += "\\-";
            indent += "  ";
        }
        else
        {
            result += "|-";
            indent += "| ";
        }
        if (getLastIndexAtPos(index) != -1)
        {
            result += (getLeftChildIndexAtPos(getLastIndexAtPos(index)) == index) ? "L->" : "R->";
        }
        int arraycount = getIndexedArrayDataAtPos(index).Length;
        result += getHashAtPos(index) + (getIsRedAtPos(index) ? " _R_" : " _B_") + " content size " + arraycount;
        for (int i = 0; i < arraycount; i++)
        {
            if (getIndexedArrayDataAtPos(index, i) != -1)
                result += " [" + i + "]=" + getIndexedArrayDataAtPos(index, i) + "|";
        }
        UnityEngine.Debug.Log(result);
        if (recursive)
        {
            if (getLeftChildIndexAtPos(index) != -1)
                PrintPretty(getLeftChildIndexAtPos(index), indent, getRightChildIndexAtPos(index) == -1, true);
            if (getRightChildIndexAtPos(index) != -1)
                PrintPretty(getRightChildIndexAtPos(index), indent, true, true);
        }

    }
    #endregion

    #region arrayWrap
    int getIndexedDataCountAtPos(int key)
    {
        return ArrayData[key / DEFAULTCAPACITY][key % DEFAULTCAPACITY].ContentCount;
    }
    void resizeIndexedDataCountAtPos(int key)
    {
        ArrayData[key / DEFAULTCAPACITY][key % DEFAULTCAPACITY].resize();
    }
    void resetIndexedDataCountAtPos(int key)
    {
        ArrayData[key / DEFAULTCAPACITY][key % DEFAULTCAPACITY].ContentCount = 0;
    }
    void incrementIndexedDataCountAtPos(int key)
    {
        ++ArrayData[key / DEFAULTCAPACITY][key % DEFAULTCAPACITY].ContentCount;
    }
    void decrementIndexedDataCountAtPos(int key)
    {
        --ArrayData[key / DEFAULTCAPACITY][key % DEFAULTCAPACITY].ContentCount;
    }
    int[] getIndexedArrayDataAtPos(int key)
    {
        return ArrayData[key / DEFAULTCAPACITY][key % DEFAULTCAPACITY].indexedArrayData;
    }
    void setIndexedArrayDataAtPos(int key, int[] indexedArrayData)
    {
        ArrayData[key / DEFAULTCAPACITY][key % DEFAULTCAPACITY].indexedArrayData = indexedArrayData;
    }
    int getIndexedArrayDataAtPos(int key, int subPos)
    {
        return ArrayData[key / DEFAULTCAPACITY][key % DEFAULTCAPACITY].indexedArrayData[subPos];
    }
    void setIndexedArrayDataIndexPos(int key, int subPos, int value)
    {
        ArrayData[key / DEFAULTCAPACITY][key % DEFAULTCAPACITY].indexedArrayData[subPos] = value;
    }
    int getHashAtPos(int key)
    {
        return ArrayData[key / DEFAULTCAPACITY][key % DEFAULTCAPACITY].hashValue;
    }
    void setHashIndexPos(int key, int value)
    {
        ArrayData[key / DEFAULTCAPACITY][key % DEFAULTCAPACITY].hashValue = value;
    }
    int getRightChildIndexAtPos(int key)
    {
        return ArrayData[key / DEFAULTCAPACITY][key % DEFAULTCAPACITY].rightChildIndex;
    }
    void setRightChildIndexAtPos(int key, int value)
    {
        ArrayData[key / DEFAULTCAPACITY][key % DEFAULTCAPACITY].rightChildIndex = value;
    }
    int getLeftChildIndexAtPos(int key)
    {
        return ArrayData[key / DEFAULTCAPACITY][key % DEFAULTCAPACITY].leftChildIndex;
    }
    void setLeftChildIndexAtPos(int key, int value)
    {
        ArrayData[key / DEFAULTCAPACITY][key % DEFAULTCAPACITY].leftChildIndex = value;
    }

    int getLastIndexAtPos(int key)
    {
        return ArrayData[key / DEFAULTCAPACITY][key % DEFAULTCAPACITY].lastIndex;
    }
    void setLastIndexAtPos(int key, int value)
    {
        ArrayData[key / DEFAULTCAPACITY][key % DEFAULTCAPACITY].lastIndex = value;
    }

    bool getIsRedAtPos(int key)
    {
        return ArrayData[key / DEFAULTCAPACITY][key % DEFAULTCAPACITY].isRed;
    }
    void setIsRedAtPos(int key, bool value)
    {
        ArrayData[key / DEFAULTCAPACITY][key % DEFAULTCAPACITY].isRed = value;
    }
    #endregion
}

 

using System;
using System.Collections.Generic;
using UnityEngine;
/*
This eventHandlerManager supports registering/unregistering listeners for specific events and it's usable 
even in Awake functions since its actual initialization happens in Reset time through the InitializeDictionary static function
This class is usable both as a static reference and as a specific instance.
eventHandlerManager has also a debug feature that can be used to check on wether events 
are happening and where (if any) crashes are happening in a series of callbacks for the same event.
To debug the static instance through the inspector an instance of the script needs to be added in the scene but it's not needed for usage.
It's the script's responsibility to remove listeners when the script is destroyed, otherwise there will be an error in the callback chain that will
block the execution of the successive callbacks.
*/
public class eventHandlerManager : MonoBehaviour
{
    //platform-conditional compilation helps to avoid the debug overhead in the final build.
#if UNITY_EDITOR
    //each flag controls a different debug message
    public bool debug;//print debug when any event is broadcasted, unless debugSpecificEvent is true, in which case only that one event is debugged
    public bool debugAdds; //print debug when any listener is added
    public bool debugRemovals;//print debug when any listener is removed
    public bool debugSpecificEvent;//print debug when a specific event is broadcasted, after each callback, 
    public EventPicker picker; //component that gives the UI element to select the event to debug with dropdowns
    //invoked after a debugAll check
    bool shouldDebugEvent(int ev)
    {
        return (!debugSpecificEvent) || (ev == picker.Selected);
    }
    public static eventHandlerManager globalDebugController;
    public bool debugGlobally;//if true activates the debugging for the global eventHandlerManager
    /*
     in reaction to setting the debugGlobally variable, set the instance as 
    globalDebugController so that its debug fields are used for the static
    functions to control debugging, it also ensures that only one debugGlobally 
    variable can be set as true in all the instances of the eventHandlerManager
    */
    void OnValidate()
    {
        if (debugSpecificEvent)
            debug = true;
        if (debugGlobally)
        {
            eventHandlerManager temp = globalDebugController;
            globalDebugController = this;
            if (temp != null)
                if (temp.GetInstanceID() != this.GetInstanceID())
                    temp.debugGlobally = false;
        }
        else
        {
            if (globalDebugController != null)
                if (globalDebugController.GetInstanceID() == this.GetInstanceID())
                    globalDebugController = null;
        }
    }
#endif
    //these dictionaries host the callback delegates, they are initialized by the initializeDicts functions, so that they are already initialized when the first Awake is called.
    static Dictionary<int, Dictionary<int, GarbagelessList>> globalListenerFunctions = initializeDicts();
    Dictionary<int, Dictionary<int, GarbagelessList>> ListenerFunctions = initializeDicts();
    #region broadcast
    public static void globalBroadcast(MonoBehaviour source, eventChannels evType, int ev, object e)
    {
#if UNITY_EDITOR
        if (globalDebugController == null)
            executeBroadcast(false, false, source, evType, ev, e, globalListenerFunctions);
        else
#endif
            executeBroadcast(
#if UNITY_EDITOR
                globalDebugController.debug, globalDebugController.shouldDebugEvent(ev),
#endif
                 source, evType, ev, e, globalListenerFunctions);
    }
    public void Broadcast(MonoBehaviour source, eventChannels evType, int ev, object e)
    {
        executeBroadcast(
#if UNITY_EDITOR
                debug, shouldDebugEvent(ev),
#endif
                source, evType, ev, e, ListenerFunctions);
    }
    static void executeBroadcast(
#if UNITY_EDITOR
        bool debug, bool specific,
#endif
        MonoBehaviour source, eventChannels evType, int ev, object e, Dictionary<int, Dictionary<int, GarbagelessList>> target)
    {
#if UNITY_EDITOR
        //if the flags are true prints a list of what is going to be invoked before execution

        if (debug && specific)
        {
            Debug.Log("EventHandleManager Broadcast" + evType + " - " + ev + " calling " + target[(int)evType][ev].Count + " functions:");
            int i = 0;
            target[(int)evType][ev].Reset();
            while (target[(int)evType][ev].MoveNext())
            {
                Debug.Log(evType + " - " + ev + " [" + i + "](" + target[(int)evType][ev].Current.Method.DeclaringType.ToString() + ">" + target[(int)evType][ev].Current.ToString() + ")");
                ++i;
            }
            Debug.Log("EventHandleManager Broadcast - START");
        }

#endif
        //invoke event delegates
        target[(int)evType][ev].Reset();
        while (target[(int)evType][ev].MoveNext())
        {
            target[(int)evType][ev].Current(e);
        }
#if UNITY_EDITOR
        if (debug && specific)
        {
            Debug.Log("EventHandleManager Broadcast - OVER");
        }
#endif
    }
    #endregion
    #region AddListener
    public static void globalAddListener(eventChannels evType, int ev, gameEventHandler eventListener)
    {
#if UNITY_EDITOR
        if (globalDebugController == null)
            executeAddListener(false, false, false, evType, ev, eventListener, globalListenerFunctions);
        else
#endif
            executeAddListener(
#if UNITY_EDITOR
                globalDebugController.debug, globalDebugController.shouldDebugEvent(ev), globalDebugController.debugAdds,
#endif
                 evType, ev, eventListener, globalListenerFunctions);
    }
    public void AddListener(eventChannels evType, int ev, gameEventHandler eventListener)
    {
        executeAddListener(
#if UNITY_EDITOR
                debug, shouldDebugEvent(ev), debugAdds,
#endif
         evType, ev, eventListener, ListenerFunctions);
    }
    static void executeAddListener(
#if UNITY_EDITOR
                bool debug, bool specific, bool debugAdds,
#endif
         eventChannels evType, int ev, gameEventHandler eventListener, Dictionary<int, Dictionary<int, GarbagelessList>> target)
    {
        target[(int)evType][ev].Add(eventListener);
#if UNITY_EDITOR
        //if this event's execution should be logged, add a debugging delegate before the method
        if (debug && specific)
        {
            target[(int)evType][ev].Add(new gameEventHandler(delegate (object e)
             {
                 Debug.Log("EventHandleManager execution" + evType + " - " + ev +
                     " finished " + eventListener.Method.DeclaringType.ToString() + " >" + eventListener.Method.ToString());
             }));
        }
        if (debugAdds)
            Debug.Log("EventHandleManager Added event " + evType + " - " + ev +
                        " by " + eventListener.Method.DeclaringType.ToString() + " >" + eventListener.Method.ToString());
#endif
    }
    #endregion
    #region RemoveListener
    public static void globalRemoveListener(eventChannels evType, int ev, gameEventHandler eventListener)
    {
#if UNITY_EDITOR
        if (globalDebugController == null)
            executeRemoveListener(false, evType, ev, eventListener, globalListenerFunctions);
        else
#endif
            executeRemoveListener(
#if UNITY_EDITOR
                globalDebugController.debugRemovals,
#endif
                 evType, ev, eventListener, globalListenerFunctions);
    }
    public void RemoveListener(eventChannels evType, int ev, gameEventHandler eventListener)
    {
        executeRemoveListener(
#if UNITY_EDITOR
                debugRemovals,
#endif
                evType, ev, eventListener, ListenerFunctions);
    }
    static void executeRemoveListener(
#if UNITY_EDITOR
                bool debugRemovals,
#endif
        eventChannels evType, int ev, gameEventHandler eventListener, Dictionary<int, Dictionary<int, GarbagelessList>> target)
    {
#if UNITY_EDITOR
        if (debugRemovals)
            Debug.Log("EventHandleManager Removed event " + evType + " - " + ev +
                        " by " + eventListener.Method.DeclaringType.ToString() + " >" + eventListener.Method.ToString());
#endif
        target[(int)evType][ev].Remove(eventListener);
    }
    #endregion

    public void OnDestroy()
    {
        ListenerFunctions = initializeDicts();
    }

    /*
    this method initializes the ListenerFunctions dictionary by doubly indexing first by eventChannel and then by specific event. The initialization consists in an empity gameEventHandler to which new listeners will eventually be added with the AddListener functions
    */
    static Dictionary<int, Dictionary<int, GarbagelessList>> initializeDicts()
    {
        //gets the information on the structure of channels from ChannelEnums
        Dictionary<eventChannels, Array> enumChannelEventList = ChannelEnums.getChannelEnumList();
        Dictionary<int, Dictionary<int, GarbagelessList>> result = new Dictionary<int, Dictionary<int, GarbagelessList>>();
        foreach (var val in (eventChannels[])Enum.GetValues(typeof(eventChannels)))
        {
            result.Add((int)val, new Dictionary<int, GarbagelessList>());
            foreach (var ev in enumChannelEventList[val])
            {
                //adds an empity gameEventHandler for each event
                result[(int)val].Add((int)ev, new GarbagelessList());
            }
        }
        return result;
    }
}
//delegate signature for the callback functions
public delegate void gameEventHandler(object e);

 

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Localized events for in-object communication

  • Part 1: basics event system
  • Part 2: event picker drawer for in-editor usage
  • Part 3: debugging with platform dependant compilation
  • Part 4: multiple dispatchers [you are here]
  • Part 5: optimization

Now we’ll see how to use the event dispatcher just everywhere. After all it’s not just objects that need to communicate with each other but also components of the same object. Why shouldn’t we apply the same approach inside an object then and use an in-object event system?

internal broadcast systems
internal broadcast systems

It would be good development practice of course to minimize code duplication while keeping separated the stuff that should be, so we naturally will want to avoid rewriting another event dispatcher from scratch (which means modifying the dispatcher we’ve been using all along) but we don’t want that it’s easy to mistakenly use the wrong events on the wrong object, so its best that each type of gameobject (e.g.:each prefab) has its own eventChannel.

Wait, wasn’t it all static references?

Yes, it was. That’s good if you only have one broadcast to rule them all, but as many of you will be thinking that isn’t going to work for a localized event dispatcher. We will need to separate static references and local references, while at the same time avoiding code duplication, because that would hinder code maintenance. And we still want to debug global broadcasts, of course.

Added difficulty: we just don’t want that pesky debug code in our deployment build.

So here’s the plan: the idea is to have one instance “proclaim itself” the controller of global debugging, so that we have an inspector interface to control that debug. Also, every function call will now need to exist in two versions, one global and one local.

The global debug controller

One voice, hear everywhere
One voice, hear everywhere
    public static eventHandlerManager globalDebugController;
    public bool debugGlobally;

    void OnValidate()
    {
        //old stuff

        if (debugGlobally)
        {
            eventHandlerManager temp = globalDebugController;
            globalDebugController = this;
            if (temp != null)
                if (temp.GetInstanceID() != this.GetInstanceID())
                    temp.debugGlobally = false;
        }
        else
        {
            if (globalDebugController != null)
                if (globalDebugController.GetInstanceID() == this.GetInstanceID())
                    globalDebugController = null;
        }
    }

Let’s begin by declaring a new static variable that will hold the reference to the one and only global debug controller, plus a confortable bool to control wich instance will act as a global controller. We could of course write a custom interface or extend the editor to manage this variable, but in a quick and dirty solution we can just use the validation step to check wether the flag is on or of and set or reset the static reference accordingly. Of course we should ensure that there’s only one instance with an active debugGlobally flag, therefore when one is activated we ensure that if there was another instance in charge before its flag gets set to false.

The data and the functions

Obviously we’ll also need to separate local lists of listeners from global ones, so where there was just a static ListenerFunctions we’ll instead have:

    static Dictionary<eventChannels, Dictionary<Enum, gameEventHandler>> globalListenerFunctions = initializeDicts();
    Dictionary<eventChannels, Dictionary<Enum, gameEventHandler>> ListenerFunctions = initializeDicts();

And once that the data is separated we need to separate also its access, so for every function that we had before, we’ll need 3 now. One to hold the actual procedure in a unified manner so that we don’t duplicate code, one to do the global broadcasts, the last one to make local broadcasts (as an alternative you may consider just passing the dispatcher reference as an argument and reserve a dispatcher to be the “global one”).

Since the change is the same on all the 3 functions I’ll go in detail just for one of them, the broadcast. Let’s start from the actual procedure:

    static void executeBroadcast(
#if UNITY_EDITOR
        bool debug, bool specific,
#endif
        MonoBehaviour source, eventChannels evType, Enum ev, eventArgExtend e, Dictionary<eventChannels, Dictionary<Enum, gameEventHandler>> target)
    {
#if UNITY_EDITOR
        //if the flags are true prints a list of what is going to be invoked before execution
        var list = target[evType][ev].GetInvocationList();
        if (list != null && debug && specific)
        {
            Debug.Log("EventHandleManager Broadcast" + evType + " - " + ev + " calling " + list.Length + " functions:");
            for (int i = 0; i < list.Length; i++)
            {
                Debug.Log(evType + " - " + ev + " [" + i + "](" + list[i].Method.DeclaringType.ToString() + ">" + list[i].Method.ToString() + ")");
            }
        }
#endif
        //invoke event delegates
        target[evType][ev](e);
#if UNITY_EDITOR
        if (debug && specific)
        {
            Debug.Log("EventHandleManager Broadcast - OVER");
        }
#endif
    }

I’ll admit, it looks quite the mess. Those #if/#endif in the middle of the function declaration are ugly AF, no way to deny it. But as we’ve said before, we don’t want to waste resources in the deployment build and since this will be a core component (if you are going to use it on every object) we need to optimize it. Actually, this isn’t even optimized enough, as Alessio Greco (to which I’m really grateful) made me realize, combining c# delegates will generate garbage, so we’ll have to fix this with a manual management of the listener references, but we’ll do this next week.

this is getting complicated
this is getting complicated

So, in detail, we’ve got the debug control arguments in the function signature inside a platform dependent if, but we also have a new argument target which can either be the global or the local variable holding the dictionary of listeners, so that this procedure can handle both calls.

    public void Broadcast(MonoBehaviour source, eventChannels evType, Enum ev, eventArgExtend e)
    {
        executeBroadcast(
#if UNITY_EDITOR
                debug, shouldDebugEvent(ev),
#endif
                source, evType, ev, e, ListenerFunctions);
    }

This is the local version of the function. It’s not static, of course, and will automatically pass its own ListenerFunctions to the executeBroadcast function, along with his own debug flags (that, of course, will still be platform dependent).

    public static void globalBroadcast(MonoBehaviour source, eventChannels evType, Enum ev, eventArgExtend e)
    {
#if UNITY_EDITOR
        if (globalDebugController == null)
            executeBroadcast(false, false, source, evType, ev, e, globalListenerFunctions);
        else
#endif
            executeBroadcast(
#if UNITY_EDITOR
                globalDebugController.debug, globalDebugController.shouldDebugEvent(ev),
#endif
                 source, evType, ev, e, globalListenerFunctions);
    }

And at last we’ve got our global call, just for clarity this is what will end up in the deployment build:

    public static void globalBroadcast(MonoBehaviour source, eventChannels evType, Enum ev, eventArgExtend e)
    {
            executeBroadcast( source, evType, ev, e, globalListenerFunctions);
    }

the rest is just a check to see if we’re debugging and to pass the global flags to the execute call, along with the globalListenerFunctions dictionary.

And for the AddListener and RemoveListener functions, just rinse and repeat.

As usual, the copy-pasteable code is down here, join me in the next and last part to delve into the pleasures of optimizaton as we’ll implement our own list of listeners to call. Register to the newsletter if you don’t want to lose it and hit me on twitter for any questions!

public class eventHandlerManager : MonoBehaviour
{
    //platform-conditional compilation helps to avoid the debug overhead in the final build.
#if UNITY_EDITOR
    //each flag controls a different debug message
    public bool debug;//print debug when any event is broadcasted, unless debugSpecificEvent is true, in which case only that one event is debugged
    public bool debugAdds; //print debug when any listener is added
    public bool debugRemovals;//print debug when any listener is removed
    public bool debugSpecificEvent;//print debug when a specific event is broadcasted, after each callback, 
    public EventPicker picker; //component that gives the UI element to select the event to debug with dropdowns
    //invoked after a debugAll check
    bool shouldDebugEvent(Enum ev)
    {
        return (!debugSpecificEvent) || (ev.ToString().Equals(picker.Selected.ToString()));
    }
    public static eventHandlerManager globalDebugController;
    public bool debugGlobally;//if true activates the debugging for the global eventHandlerManager
    /*
     in reaction to setting the debugGlobally variable, set the instance as 
    globalDebugController so that its debug fields are used for the static
    functions to control debugging, it also ensures that only one debugGlobally 
    variable can be set as true in all the instances of the eventHandlerManager
    */
    void OnValidate()
    {
        if (debugSpecificEvent)
            debug = true;
        if (debugGlobally)
        {
            eventHandlerManager temp = globalDebugController;
            globalDebugController = this;
            if (temp != null)
                if (temp.GetInstanceID() != this.GetInstanceID())
                    temp.debugGlobally = false;
        }
        else
        {
            if (globalDebugController != null)
                if (globalDebugController.GetInstanceID() == this.GetInstanceID())
                    globalDebugController = null;
        }
    }
#endif
    //these dictionaries host the callback delegates, they are initialized by the initializeDicts functions, so that they are already initialized when the first Awake is called.
    public static Dictionary<eventChannels, Dictionary<Enum, gameEventHandler>> globalListenerFunctions = initializeDicts();
    public Dictionary<eventChannels, Dictionary<Enum, gameEventHandler>> ListenerFunctions = initializeDicts();
    #region broadcast
    public static void globalBroadcast(MonoBehaviour source, eventChannels evType, Enum ev, eventArgExtend e)
    {
#if UNITY_EDITOR
        if (globalDebugController == null)
            executeBroadcast(false, false, source, evType, ev, e, globalListenerFunctions);
        else
#endif
            executeBroadcast(
#if UNITY_EDITOR
                globalDebugController.debug, globalDebugController.shouldDebugEvent(ev),
#endif
                 source, evType, ev, e, globalListenerFunctions);
    }
    public void Broadcast(MonoBehaviour source, eventChannels evType, Enum ev, eventArgExtend e)
    {
        executeBroadcast(
#if UNITY_EDITOR
                debug, shouldDebugEvent(ev),
#endif
                source, evType, ev, e, ListenerFunctions);
    }
    static void executeBroadcast(
#if UNITY_EDITOR
        bool debug, bool specific,
#endif
        MonoBehaviour source, eventChannels evType, Enum ev, eventArgExtend e, Dictionary<eventChannels, Dictionary<Enum, gameEventHandler>> target)
    {
#if UNITY_EDITOR
        //if the flags are true prints a list of what is going to be invoked before execution
        var list = target[evType][ev].GetInvocationList();
        if (list != null && debug && specific)
        {
            Debug.Log("EventHandleManager Broadcast" + evType + " - " + ev + " calling " + list.Length + " functions:");
            for (int i = 0; i < list.Length; i++)
            {
                Debug.Log(evType + " - " + ev + " [" + i + "](" + list[i].Method.DeclaringType.ToString() + ">" + list[i].Method.ToString() + ")");
            }
        }
#endif
        //invoke event delegates
        target[evType][ev](e);
#if UNITY_EDITOR
        if (debug && specific)
        {
            Debug.Log("EventHandleManager Broadcast - OVER");
        }
#endif
    }
    #endregion
    #region AddListener
    public static void globalAddListener(eventChannels evType, Enum ev, gameEventHandler eventListener)
    {
#if UNITY_EDITOR
        if (globalDebugController == null)
            executeAddListener(false, false, false, evType, ev, eventListener, globalListenerFunctions);
        else
#endif
            executeAddListener(
#if UNITY_EDITOR
                globalDebugController.debug, globalDebugController.shouldDebugEvent(ev), globalDebugController.debugAdds,
#endif
                 evType, ev, eventListener, globalListenerFunctions);
    }
    public void AddListener(eventChannels evType, Enum ev, gameEventHandler eventListener)
    {
        executeAddListener(
#if UNITY_EDITOR
                debug, shouldDebugEvent(ev), debugAdds,
#endif
         evType, ev, eventListener, ListenerFunctions);
    }
    static void executeAddListener(
#if UNITY_EDITOR
                bool debug, bool specific, bool debugAdds,
#endif
         eventChannels evType, Enum ev, gameEventHandler eventListener, Dictionary<eventChannels, Dictionary<Enum, gameEventHandler>> target)
    {
#if UNITY_EDITOR
        //if this event's execution should be logged, add a debugging delegate before the method
        if (debug && specific)
            target[evType][ev] += new gameEventHandler(delegate (eventArgExtend e)
            {
                Debug.Log("EventHandleManager execution" + evType + " - " + ev +
                    " finished " + eventListener.Method.DeclaringType.ToString() + " >" + eventListener.Method.ToString());
            });
        if (debugAdds)
            Debug.Log("EventHandleManager Added event " + evType + " - " + ev +
                        " by " + eventListener.Method.DeclaringType.ToString() + " >" + eventListener.Method.ToString());
#endif
        target[evType][ev] += eventListener;
    }
    #endregion
    #region RemoveListener
    public static void globalRemoveListener(eventChannels evType, Enum ev, gameEventHandler eventListener)
    {
#if UNITY_EDITOR
        if (globalDebugController == null)
            executeRemoveListener(false, evType, ev, eventListener, globalListenerFunctions);
        else
#endif
            executeRemoveListener(
#if UNITY_EDITOR
                globalDebugController.debugRemovals,
#endif
                 evType, ev, eventListener, globalListenerFunctions);
    }
    public void RemoveListener(eventChannels evType, Enum ev, gameEventHandler eventListener)
    {
        executeRemoveListener(
#if UNITY_EDITOR
                debugRemovals,
#endif
                evType, ev, eventListener, ListenerFunctions);
    }
    static void executeRemoveListener(
#if UNITY_EDITOR
                bool debugRemovals,
#endif
        eventChannels evType, Enum ev, gameEventHandler eventListener, Dictionary<eventChannels, Dictionary<Enum, gameEventHandler>> target)
    {
#if UNITY_EDITOR
        if (debugRemovals)
            Debug.Log("EventHandleManager Removed event " + evType + " - " + ev +
                        " by " + eventListener.Method.DeclaringType.ToString() + " >" + eventListener.Method.ToString());
#endif
        target[evType][ev] -= eventListener;
    }
    #endregion
    public void OnDestroy()
    {
        ListenerFunctions= initializeDicts();
    }
    /*
    this method initializes the ListenerFunctions dictionary by doubly indexing first by eventChannel and then by specific event. The initialization consists in an empity gameEventHandler to which new listeners will eventually be added with the AddListener functions
    */
    static Dictionary<eventChannels, Dictionary<Enum, gameEventHandler>> initializeDicts()
    {
        //gets the information on the structure of channels from ChannelEnums
        Dictionary<eventChannels, Array> enumChannelEventList = ChannelEnums.getChannelEnumList();
        Dictionary<eventChannels, Dictionary<Enum, gameEventHandler>> result = new Dictionary<eventChannels, Dictionary<Enum, gameEventHandler>>();
        foreach (var val in (eventChannels[])Enum.GetValues(typeof(eventChannels)))
        {
            result.Add(val, new Dictionary<Enum, gameEventHandler>());
            foreach (var ev in enumChannelEventList[val])
            {
                //adds an empity gameEventHandler for each event
                result[val].Add((Enum)ev, new gameEventHandler(delegate (eventArgExtend e) { }));
            }
        }
        return result;
    }
}
//delegate signature for the callback functions
public delegate void gameEventHandler(eventArgExtend e);
//argument class to be extended to add fields to the callbacks it without changing the signature
public class eventArgExtend : System.EventArgs
{
}

 

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Platform dependent compilation: debug on event dispatcher

  • Part 1: basics event system
  • Part 2: event picker drawer for in-editor usage
  • Part 3: debugging with platform dependant compilation [you are here]
  • Part 4: multiple dispatchers
  • Part 5: optimization

In today’s tutorial I’ll delve in a bit of meta-programming, the obscure art of telling the compiler what to do to generate code, instead of plainly writing it yourself. But don’t worry, at the end of the day, we’ll be using just an if. In this tutorial we’ll be editing my event-system in order to add a debugging feature to it, so that we can log what handlers get invoked on what events and comfortably control that in the inspector.

Platform Dependent Compilation
Platform Dependent Compilation allows you to change code depending on target platform

What’s Platform Dependent Compilation again?

It’s a nice trick you can use in unity to avoid burdening one platform with the code that’s only required for another one. And something that will render your in-editor tests useless when found in some platform-specific plugin, as I learned the painful way. In Unity3d is done by taking one compiler directive to check if some constants are already defined. Unity will define these constants according to build target. If you are developing, for instance, with pc as build target and write #if UNITY_ANDROID all the following code until the #endif will be as commented from the IDE’s point of view.

And when it’s time to build your code, that stuff will just be treated as a comment and not be included in your final build… unless you are building for that platform. In that case that code will be there as if nothing happened. This can be very useful, for instance, when it comes to dealing with input methods in a cross-platform game that’s both on mobile and pc.

Lets debug this
Lets debug this

How do we use Platform dependent compilation to debug an event system?

Basically, the only place where we’ll need it is the editor, so we’ll check for the variable UNITY_EDITOR and that’s it.

And here’s what we’ll need to add to our event script in terms of variables:

#if UNITY_EDITOR
    public bool debug;//print debug when any event is broadcasted, unless debugSpecificEvent is true, in which case only that one event is debugged
    public bool debugAdds; //print debug when any listener is added
    public bool debugRemovals;//print debug when any listener is removed
    public bool debugSpecificEvent;//print debug when a specific event is broadcasted, after each callback, 
    public EventPicker picker; //component that gives the UI element to select the event to debug with dropdowns
    //invoked after a debugAll check
    bool shouldDebugEvent(Enum ev)
    {
        return (!debugSpecificEvent) || (ev.ToString().Equals(picker.Selected.ToString()));
    }
    
    void OnValidate()
    {
        if (debugSpecificEvent)
            debug = true;
    }
#endif

So, inside the platform dependent code block we insert a series of flags and an EventPicker so that we’ll be able to manage what to debug when and even select a specific event to debug in isolation.

There are a couple of lines of code there that aren’t actually variables, let’s explain them:

  • shouldDebugEvent is a function, returns true in two cases, either when we’re not debugging a specific event, or when the event in argument is the one selected in thepicker.
  • OnValidate is a monobehaviour method that is invoked every time the data is changed in the inspector. In this case we want to ensure that when we’re debugging a specific event thedebugflag isn’t forgotten since it’ll need to be true to actually print anything.

Then we’ll need to change quite drastically our Broadcast , AddListener and RemoveListener functions.

This is our new broadcast:

    public static void Broadcast(eventChannels evType, Enum ev, eventArgExtend e)
    {
#if UNITY_EDITOR
        //if the flags are true prints a list of what is going to be invoked before execution
        var list = ListenerFunctions[evType][ev].GetInvocationList();
        if (list != null && debug &&  shouldDebugEvent(ev))
        {
            Debug.Log("EventHandleManager Broadcast" + evType + " - " + ev + " calling " + list.Length + " functions:");
            for (int i = 0; i < list.Length; i++)
            {
                Debug.Log(evType + " - " + ev + " [" + i + "](" + list[i].Method.DeclaringType.ToString() + ">" + list[i].Method.ToString() + ")");
            }
        }
#endif
        //invoke event delegates
        ListenerFunctions[evType][ev](e);
#if UNITY_EDITOR
        if (debug &&  shouldDebugEvent(ev))
        {
            Debug.Log("EventHandleManager Broadcast - OVER");
        }
#endif
    }

The first thing that’s coming to many people’s mind right now is: “wait, what the fuck is that Method.ToString()?!”. Well, it’s C# reflection again. It keeps creeping in, but what we say to the god of the 10’000-words-long tutorials?

t4w9s7

So, just know that that debug code will print the name of the classes and the functions that the event dispatcher will invoke.
In the beginning we recover the list of functions that will be called from the delegate with GetInvocationList, then (provided this event broadcast should be debugged) for each one of them we’ll print its name and source, along with the position number.

Then we invoke the functions, and at last we print a nice “over” after the event reactions are finished.

Registering and unregistering

    static void AddListener(eventChannels evType, Enum ev, gameEventHandler eventListener)
    {
#if UNITY_EDITOR
        //if this event's execution should be logged, add a debugging delegate before the method
        if (debug && shouldDebugEvent(ev))
            ListenerFunctions[evType][ev] += new gameEventHandler(delegate (eventArgExtend e)
            {
                Debug.Log("EventHandleManager execution" + evType + " - " + ev +
                    " finished " + eventListener.Method.DeclaringType.ToString() + " >" + eventListener.Method.ToString());
            });
        if (debugAdds)
            Debug.Log("EventHandleManager Added event " + evType + " - " + ev +
                        " by " + eventListener.Method.DeclaringType.ToString() + " >" + eventListener.Method.ToString());
#endif
        ListenerFunctions[evType][ev] += eventListener;
    }

Here we do two things: in case the event broadcast debugging is active for this event we add a delegate that prints event channel, event name, invoking class and method name. Then, if we’re debugging registration we also log that, again with the same debug message.

The latter will also be done for the unregister function:

    public static void RemoveListener(eventChannels evType, Enum ev, gameEventHandler eventListener, Dictionary<eventChannels, Dictionary<Enum, gameEventHandler>> target)
    {
#if UNITY_EDITOR
        if (debugRemovals)
            Debug.Log("EventHandleManager Removed event " + evType + " - " + ev +
                        " by " + eventListener.Method.DeclaringType.ToString() + " >" + eventListener.Method.ToString());
#endif
        ListenerFunctions[evType][ev] -= eventListener;
    }

Here again we log all the same information when the listener is removed from the event.

So, wrapping this up, we’ve added debugging features to our event dispatcher and can now place it in any scene so that we can handle the debug operations in it using the inspector. As usual the full code is down here for copy-paste purposes, we’ll get next time to the semi-final form of the script that will allow not just one single dispatcher in the whole game but multiple independent channels of communication so that for instance a single character can have its own internal event system. Register to the newsletter if you don’t want to lose it and hit me on twitter for any questions!

And by the way, I’m looking for a job right now, check out my portfolio here!

using System;
using System.Collections.Generic;
using UnityEngine;

public class eventHandlerManager : MonoBehaviour
{
#if UNITY_EDITOR
    public bool debug;//print debug when any event is broadcasted, unless debugSpecificEvent is true, in which case only that one event is debugged
    public bool debugAdds; //print debug when any listener is added
    public bool debugRemovals;//print debug when any listener is removed
    public bool debugSpecificEvent;//print debug when a specific event is broadcasted, after each callback, 
    public EventPicker picker; //component that gives the UI element to select the event to debug with dropdowns
    //invoked after a debugAll check
    bool shouldDebugEvent(Enum ev)
    {
        return (!debugSpecificEvent) || (ev.ToString().Equals(picker.Selected.ToString()));
    }
    
    void OnValidate()
    {
        if (debugSpecificEvent)
            debug = true;
    }
#endif
    public static Dictionary<eventChannels, Dictionary<Enum, gameEventHandler>> ListenerFunctions = initializeDicts();

    public static void Broadcast(eventChannels evType, Enum ev, eventArgExtend e)
    {
#if UNITY_EDITOR
        //if the flags are true prints a list of what is going to be invoked before execution
        var list = ListenerFunctions[evType][ev].GetInvocationList();
        if (list != null && debug &&  shouldDebugEvent(ev))
        {
            Debug.Log("EventHandleManager Broadcast" + evType + " - " + ev + " calling " + list.Length + " functions:");
            for (int i = 0; i < list.Length; i++)
            {
                Debug.Log(evType + " - " + ev + " [" + i + "](" + list[i].Method.DeclaringType.ToString() + ">" + list[i].Method.ToString() + ")");
            }
        }
#endif
        //invoke event delegates
        ListenerFunctions[evType][ev](e);
#if UNITY_EDITOR
        if (debug &&  shouldDebugEvent(ev))
        {
            Debug.Log("EventHandleManager Broadcast - OVER");
        }
#endif
    }
    static void AddListener(eventChannels evType, Enum ev, gameEventHandler eventListener)
    {
#if UNITY_EDITOR
        //if this event's execution should be logged, add a debugging delegate before the method
        if (debug && shouldDebugEvent(ev))
            ListenerFunctions[evType][ev] += new gameEventHandler(delegate (eventArgExtend e)
            {
                Debug.Log("EventHandleManager execution" + evType + " - " + ev +
                    " finished " + eventListener.Method.DeclaringType.ToString() + " >" + eventListener.Method.ToString());
            });
        if (debugAdds)
            Debug.Log("EventHandleManager Added event " + evType + " - " + ev +
                        " by " + eventListener.Method.DeclaringType.ToString() + " >" + eventListener.Method.ToString());
#endif
        ListenerFunctions[evType][ev] += eventListener;
    }

    public static void RemoveListener(eventChannels evType, Enum ev, gameEventHandler eventListener, Dictionary<eventChannels, Dictionary<Enum, gameEventHandler>> target)
    {
#if UNITY_EDITOR
        if (debugRemovals)
            Debug.Log("EventHandleManager Removed event " + evType + " - " + ev +
                        " by " + eventListener.Method.DeclaringType.ToString() + " >" + eventListener.Method.ToString());
#endif
        ListenerFunctions[evType][ev] -= eventListener;
    }
    public void OnDestroy()
    {
        ListenerFunctions = initializeDicts();
    }

    static Dictionary<eventChannels, Dictionary<Enum, gameEventHandler>> initializeDicts()
    {
        Dictionary<eventChannels, Array> enumChannelEventList = ChannelEnums.getChannelEnumList();
        Dictionary<eventChannels, Dictionary<Enum, gameEventHandler>> result = new Dictionary<eventChannels, Dictionary<Enum, gameEventHandler>>();
        foreach (var val in (eventChannels[])Enum.GetValues(typeof(eventChannels)))
        {
            result.Add(val, new Dictionary<Enum, gameEventHandler>());
            foreach (var ev in enumChannelEventList[val])
            {
                result[val].Add((Enum)ev, new gameEventHandler(delegate (eventArgExtend e) { }));
            }
        }
        return result;
    }
}
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Event system: tutorial for Unity3D & C#

  • Part 1: basics event system [you are here]
  • Part 2: event picker drawer for in-editor usage
  • Part 3: debugging with platform dependant compilation
  • Part 4: multiple dispatchers
  • Part 5: optimization

Today I’ll start presenting you the single most useful piece of code that I’ve ever written. Since writing it I used it in every single project I’ve made, however simple. Even in game jams.
I’m speaking of an Event System. A class with no other purpose than to let other objects communicate with each other, with as little overhead as possible. And it’s implemented with delegates.

And it’s way better than SendMessage, if you ask me.

event_driven_programming

What’s an Event System?

Those of you who already know, just skip this paragraph. Still with me? good. The main problem you’ll have with SendMessage is that it needs to use a method’s name as an input. So the object sending the message not only needs to know what method should be invoked by the receiver, but also his name. In a case sensitive way. Want to change that name? too bad, the IDE won’t change the string for you. You’ll have to remember every single one of those and do it by yourself… or never change a function name ever again. Or remove one.

Sounds bad? it is. It’s a maintenance nightmare. Plus, it uses strings. Fuck strings. Strings are evil. They use your memory, trigger your garbage collector and spit on your mother. Never use them unless at gunpoint… and even then think twice about it and instead use an enum.ToString() if you can.

So what’s the solution? we need something more like this: you need to send a message between objects (or scripts) when a “thing” happens. So the scripts in the other object react to the “thing”. The caller script doesn’t need to know who or how will do anything in reaction to the “thing”. It just needs to shout out “Hey! I have a thing here!” and eventually how big the “thing” is. For instance, “Hey! I have a 3.5 Damage here!”. Then the health script subtracts the damage and the particle script spills blood everywhere. But the collider doesn’t need to know that. The “thing” is an Event. Shouting is a Broadcast. The reactions are called Callbacks or Handlers.

But this is nothing I invented, you can just study it here or here, or even here.

What’s a Delegate?

As before, if you know already, just skip ahead. Delegates are something I wish they did teach me in university, because they are just awesome. To put it bluntly: in C# you can treat functions as if they were variables and pass them as an argument to other functions. This means that you can change behaviours of an object at runtime. Or have an object hold and call a function without it even knowing what it does. Absolute decoupling. A maintenance heaven.

Before you can use a delegate, you first need to declare its type. Which means telling the compiler which return type and what arguments will be assigned to it. Not its name. Not its content. Just the signature structure. Of course, if you use as a signature something like:

public delegate object nameFunct(object o);

you can then pass anything and get anything (but say goodbye to compiler type-checks).

After you have declared a delegate type, you can then declare a delegate variable and assign to it a function with a matching signature. Just like you would do with any other variable. If you still need to delve deeper you can go here or here.

Get on with it!

gowi-2

Now, if you have seen my portfolio the event system there can be quite intimidating, but don’t worry, we won’t be doing that version right now. We’ll start with version 0.0.3 while that one is 1.0.1 [edit: this was before optimizing for the last part of the tutorial]. That makes about 100 rows of code and 20 headaches of difference.

Before we get to the main class, the event dispatcher, let’s define some stuff we’ll use there.

public delegate void gameEventHandler(eventArgExtend e);

This will be our event signature. Nothing to return because we don’t want the event sender to have anything to do with the receivers. The event type is this thing here:

public class eventArgExtend : System.EventArgs { }

Why didn’t I use directlySystem.EventArgs? Because in the future I may want to add something between those parentheses and when it happens it will cost me nothing to do so. Had I used directly that type it could require more work to do it.

Now, let’s get to something more interesting: defining the events themselves. To do so I’ll use something infinitely superior to strings: enums.

public enum eventChannels
{
    inGame
}
public enum inGameChannelEvents
{
    thing
}

Now we’ll need a comfortable way to pass this information to the dispatcher. For this I just created a class with a static function:

public class ChannelEnums
{
    public static Dictionary<eventChannels, System.Array> getChannelEnumList()
    {

        Dictionary<eventChannels, System.Array> enumChannelEventList = new Dictionary<eventChannels, System.Array>();
        enumChannelEventList.Add(eventChannels.inGame, System.Enum.GetValues(typeof(inGameChannelEvents)));
        return enumChannelEventList;
    }
}

Notice: adding a channel requires to add a new enum type, and there is no automated way to set a link between an enum value ineventChannelsand an other enumtype. So for each channel you need to add, you’ll have to write a new row of code like the one just before thereturn.

What we did here is to create a static function for the dispatcher. The function will recovery every event channel and every corresponding array of values, so that we can initialize the list of listeners in the dispatcher. All in the nice form of a Dictionary.

Now let’s get to the Event System

Our event system needs to be usable at EVERY stage of game play. This includes the Awake function of the first objects to be present in the first scene. Which means that I can’t use the Awake function to initialize the event system, or I would get in a race condition. That’s fucked up. But years of Unity3D ninjutsu allowed me to discover a precious thing: default values are initialized before Awake, and you can get them with static functions.

public class eventHandlerManager : MonoBehaviour
{
    static Dictionary<eventChannels, Dictionary<Enum, gameEventHandler>> ListenerFunctions = initializeDicts();

What’s thatinitializeDicts? we’ll see later. For now just look at the type of ListenerFunctions: it’s a dictionary of dictionaries. It allows us to index gameEventHandler delegates first by channel and then by event.

Now we can write the key functions of the dispatcher: one to raise an event, one to add a delegate as listener, one to remove it.

public static void Broadcast( eventChannels evType,Enum ev,eventArgExtend e) 
	{
		ListenerFunctions[evType][ev](e);
	}
	
	public static void AddListener(eventChannels evType,Enum ev, gameEventHandler eventListener)
	{
		ListenerFunctions[evType][ev]+=eventListener;
	}
	public static void RemoveListener(eventChannels evType,Enum ev, gameEventHandler eventListener)
	{
		ListenerFunctions[evType][ev]-=eventListener;
	}

Why are they all static? because that way you don’t need to get the other classes to find the instance. Yes, this is limiting: you can’t have a damage event that only gets notified to one character. But for something more selective we’ll need to make a lot of changes, for the time being just add an identifier as field in the argument. Then have the receivers check that value. We’ll get back to that in future tutorials.

This is all that’s needed on the outside to use this event handler. Other classes that want to react to an event just need to declare a function that matches thegameEventHandlersignature and then invoke theaddListenerfunction giving that function as the last argument (without parentheses), and do the same for removal like this:

eventHandlerManager.AddListener(eventChannels.inGame, inGameChannelEvents.thing, onThing);

The broadcast use is even simpler:

eventHandlerManager.Broadcast(eventChannels.inGame, inGameChannelEvents.thing, new eventArgExtend());

So, now we’re almost done. There is just one last detail for the magic to fully work: how do we initialize and cleanListenerFunctions?

    public void OnDestroy()
    {
        ListenerFunctions = initializeDicts();
    }

    static Dictionary<eventChannels, Dictionary<Enum, gameEventHandler>> initializeDicts()
    {
        Dictionary<eventChannels, Array> enumChannelEventList = ChannelEnums.getChannelEnumList();
        Dictionary<eventChannels, Dictionary<Enum, gameEventHandler>> result = new Dictionary<eventChannels, Dictionary<Enum, gameEventHandler>>();
        foreach (var val in (eventChannels[])Enum.GetValues(typeof(eventChannels)))
        {
            result.Add(val, new Dictionary<Enum, gameEventHandler>());
            foreach (var ev in enumChannelEventList[val])
            {
                result[val].Add((Enum)ev, new gameEventHandler(delegate (eventArgExtend e) { }));
            }
        }
        return result;
    }

Wait a minute: did I initialize ListenerFunctions on destroy? Yes, because that is a static field. It will survive to the existence of the eventHandlerManager instance. And since that should only happen on a scene load, I can be sure that nothing should stay in the dictionary and that the new scene has a ready new clean slate to work on when the first Awake is called.

The initialization works by first getting the enum data from the static function we defined before in ChannelEnums, then using that data to initialize every field of the dictionary with an empitygameEventHandler; this way we can later add the Handlers with a+=instead of checking for a null field every time.

That’s all folks!

Not really. Actually there is a lot more that we can add to this class, mainly for debugging purposes, plus the “local” version of the event dispatcher that I mentioned before. But this tutorial is already huge, so maybe it’s better to deal with that stuff next week. If you want to be sure not losing it, subscribe to my newsletter. For any feedback comments are below or you can just add me on twitter.

using System;
using System.Collections.Generic;
using UnityEngine;

public class eventHandlerManager : MonoBehaviour
{
    public static Dictionary<eventChannels, Dictionary<Enum, gameEventHandler>> ListenerFunctions = initializeDicts();

    public static void Broadcast(eventChannels evType, Enum ev, eventArgExtend e)
    {
        ListenerFunctions[evType][ev](e);
    }

    public static void AddListener(eventChannels evType, Enum ev, gameEventHandler eventListener)
    {
        ListenerFunctions[evType][ev] += eventListener;
    }
    public static void RemoveListener(eventChannels evType, Enum ev, gameEventHandler eventListener)
    {
        ListenerFunctions[evType][ev] -= eventListener;
    }

    public void OnDestroy()
    {
        ListenerFunctions = initializeDicts();
    }

    static Dictionary<eventChannels, Dictionary<Enum, gameEventHandler>> initializeDicts()
    {
        Dictionary<eventChannels, Array> enumChannelEventList = ChannelEnums.getChannelEnumList();
        Dictionary<eventChannels, Dictionary<Enum, gameEventHandler>> result = new Dictionary<eventChannels, Dictionary<Enum, gameEventHandler>>();
        foreach (var val in (eventChannels[])Enum.GetValues(typeof(eventChannels)))
        {
            result.Add(val, new Dictionary<Enum, gameEventHandler>());
            foreach (var ev in enumChannelEventList[val])
            {
                result[val].Add((Enum)ev, new gameEventHandler(delegate (eventArgExtend e) { }));
            }
        }
        return result;
    }
}

public enum eventChannels
{
    inGame
}

public enum inGameChannelEvents
{
    thing
}

public class eventArgExtend : System.EventArgs { }

public delegate void gameEventHandler(eventArgExtend e);

public class ChannelEnums
{
    public static Dictionary<eventChannels, System.Array> getChannelEnumList()
    {

        Dictionary<eventChannels, System.Array> enumChannelEventList = new Dictionary<eventChannels, System.Array>();
        enumChannelEventList.Add(eventChannels.inGame, System.Enum.GetValues(typeof(inGameChannelEvents)));
        return enumChannelEventList;
    }
}

 

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •