Skip to content
This repository has been archived by the owner on Aug 7, 2021. It is now read-only.

ConsoleExecution

Porrith Suong edited this page May 2, 2018 · 3 revisions

Console Execution

Any method subscribed through the GlobalEventHandler or RelativeEventHandler can be invoked by the Developer Console. Since each event is a string type, you can write the event name into the input field and execute the event.

The only caveat is that events triggered through the console must not have spaces or any character that acts as the input's delimiter. For example,

heal all

vs

healall

The former, "heal all" would be parsed as [eventName] [arg0] and attempt to invoke an event "heal" and pass an argument "all" indicating that the method subscribed must take a string parameter. If no method exists, nothing will happen. Whereas, "healall" will just be passed [eventName] and invoke any subscribed method with an event signature as "healall".

Registering GameObjects to the Console

GameObjects or any object need to be registered by some unique ID such that they can be referenced and invoked by the console. The unique Id is typically a signed integer and if it's a MonoBehaviour or Scriptable Object it can naturally be GetInstanceId() as Unity ensures that each InstanceId is unique within the scene.

The IdCache is a scriptable object which stores all registered object references for later use. To store the object into the IdCache:

using Console;  // The IdCache lives in the Console namespace
using UnityEngine;
using GlobalEvents;

public class Enemy : MonoBehaviour {
    private void OnEnable() {
        // Stores the instance ID when the object is enabled
        IdCache.CacheInstanceId(this.GetInstanceID(), this);
    }

    private void OnDisable() {
        // Removes the instanceId when the object is disabled
        IdCache.RemoveInstanceId(this.GetInstanceID(), this);
    }
}

Viewing the Registered GameObjects

For developers building a game, you can view the cached objects and their Ids in the IdCache.asset file in the Project. Clicking on the Scriptable Object provides a custom inspector shown below:

cached-ids

To display the Ids in the console, the console command:

showids <integer>

Displays an number of Ids and their cached objects in the console. This feature is a WIP, since I need to figure out how to scale the the grid group with the quantity of text.

cached-ids-console

Logging Console Ouputs

To log an event to the console, for each method subscribed, simply invoke ConsoleOutput.Log(string:message, Color:textColor). To access the static method, you must use the Console.UI namespace.

using Console;
using Console.UI; // Use this namespace to log output messages
using GlobalEvents;
using UnityEngine;

public class Health : MonoBehaviour {

    private float health;

    private void OnEnable() {
        RelativeEventHandler.SubscribeEvent("heal", this, "Heal");
        IdCache.CacheInstanceId(this.GetInstanceID(), this); 
    }

    private void OnDisable() {
        IdCache.RemoveInstanceId(this.GetInstanceID(), this);
        RelativeEventHandler.UnsubscribeEvent("heal", this, "Heal");
    }

    private void Heal(float amount) {
        health += amount;
        // Like Unity's Debug.Log, except this one will go into the ConsoleOutput UI
        ConsoleOutput.Log(string.Format("{0} healed {1} units!", this.name, amount), Color.green);
    }
}

You can pass a richtext formatted string to bold, italicize, or colourize different parts of the message.