Register<T>

Register is a generic dictionary-based collection that facilitates entry management. A common use for it might be static register-oriented classes, such as for example ObjectArchetypeRegister from the Elery Object Pooler tool.. You might want to see how to create a static register.

You should be careful if you use the Register everywhere for the same reason as you should be careful with a Dictionary. I’m not saying not to do it, just that it might be a bit of a memory-intensive situation. Why to use a Register instead of a generic Dicitionary? It has dictionary management pre coded, has also the LastRetrieved property, which is basically a one-item history, and a GetMultiple method which retrieves multiple entries in an array.

METHODS

T AddRegister(string identifier, T item)

AddRegister adds an item to the Register if the given key isn’t there already and returns the provided item. It’s an implementation of «Dictionary.Add()».

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
using NutTools;
using UnityEngine;

public class MyClass: MonoBehaviour
{
    public GameObject obj;
    Register<GameObject> reg = new Register<GameObject>();

    void Start()
    {
        reg.AddRegister("some key", obj);
    }
}

Clear

Clears all references from the Register but the LastRetrieved property.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
using NutTools;
using UnityEngine;

public class MyClass: MonoBehaviour
{
    public GameObject obj;
    Register<GameObject> reg = new Register<GameObject>();

    void Start()
    {
        reg.Clear();
    }
}

bool Contains(string identifier)

Returns if the identifier provided is registered.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
using NutTools;
using UnityEngine;

public class MyClass: MonoBehaviour
{
    public GameObject obj;
    Register<GameObject> reg = new Register<GameObject>();

    void Update()
    {
        if (reg.Contains("some key"))
        {
            // Do stuff…
        }
    }
}

T Get(string identifier)

Returns the object associated to the identifier string if it is present inside the Register.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
using NutTools;
using UnityEngine;

public class MyClass: MonoBehaviour
{
    public GameObject obj;
    Register<GameObject> reg = new Register<GameObject>();

    void Update()
    {
        GameObject game_object = reg.Get("some key");
        if (game_object != null)
        {
            // Do stuff…
        }
    }
}

T[] GetMultiple(params string[] identifiers)

Returns an array with the objects associated to the provided identifiers that can be found inside the Register.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
using NutTools;
using UnityEngine;

public class MyClass: MonoBehaviour
{
    public GameObject obj;
    Register<GameObject> reg = new Register<GameObject>();

    void Update()
    {
        GameObject[] game_objects = reg.GetMultiple("some key", "another key");
        if (game_objects != null)
        {
            // Do stuff…
        }
    }
}

bool TryAddRegister(string identifier, T item)

TryAddRegister adds an item to the Register if the given key isn’t there already and returns the success state of the operation. It’s an implementation of «Dictionary.Add()».

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
using NutTools;
using UnityEngine;

public class MyClass: MonoBehaviour
{
    public GameObject obj;
    Register<GameObject> reg = new Register<GameObject>();

    void Start()
    {
        if (reg.TryAddRegister("some key", obj))
        {
            // Do stuff…
        }
    }
}

bool TryGet(string identifier, out T item)

Gets the item relative to the identifier and returns the success state of the operation.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
using NutTools;
using UnityEngine;

public class MyClass: MonoBehaviour
{
    public GameObject obj;
    Register<GameObject> reg = new Register<GameObject>();

    void Start()
    {
        if (reg.TryGet("some key", out T g_obj))
        {
            // Do stuff…
        }
    }
}

bool TryGetMultiple(string[] identifiers, out T[] item)

Gets the items relative to the identifiers that exist and returns the success state of the operation.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
using NutTools;
using UnityEngine;

public class MyClass: MonoBehaviour
{
    public GameObject obj;
    Register<GameObject> reg = new Register<GameObject>();

    void Start()
    {
        string[] keys = new string[] { "some key", "another key" }
        if (reg.TryGetMultiple(keys, out GameObject[] objs))
        {
            // Do stuff…
        }
    }
}

bool TryUnregister(string identifier, out T item)

Tries to remove an entry

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
using NutTools;
using UnityEngine;

public class MyClass: MonoBehaviour
{
    public GameObject obj;
    Register<GameObject> reg = new Register<GameObject>();

    void Start()
    {
        if (reg.TryUnregister("some key"))
        {
            // Do stuff…
        }
    }
}

T Unregister(string identifier)

Removes an item from the Register if the given key is available. It’s an implementation of «Dictionary.Remove»

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
using NutTools;
using UnityEngine;

public class MyClass: MonoBehaviour
{
    public GameObject obj;
    Register<GameObject> reg = new Register<GameObject>();

    void OnDestroy()
    {
        reg.Unregister("some key");
    }
}

PROPERTIES

T LastRetrieved

It’s the last item you retrieved from the register. It cannot be cleared or set. It returns default if no item has been retrieved so far.

bool IsNull, bool IsEmpty, and bool IsNullOrEmpty

Tell if the internal dictionary is null or empty.

Empty and Null

Empty and Null representations of Register.


GET HELP

Join this Discord server to get help from the community, suggest new features, and vote on future updates!