Search Results for

    Show / Hide Table of Contents

    Getting Started

    The general usecase of GlobalMessage is to communicate gameplay events between atl east two gameplay systems without coupling them in a limiting way (i.e. not on the code- or prefab-/scene-level).

    The way to achieve this is as follows:

    1. Create a new MessageType.
    2. Set up logic for receiving this MessageType in the second system.
    3. Set up logic for sending this MessageType in the first system.

    If you learn best by getting your hands dirty in an already set up Unity scene, have a look at the Samples section.

    We will continue to set up a basic message-sender-receiver-relation with a component-based workflow.

    Note

    GlobalMessage also supports other workflows that may suit you or your project better (i.e. code-focused, exposing references in custom scripts), so make sure to read up on the more detailed sections of the manual.


    ✉️ Creating a new MessageType

    Creating a new message via context menu

    Right click inside the project view and navigate to Create > GlobalMessage > MessageType. Name the file MyNewMessage

    New MessageType and its EMO

    This will create

    1. a new MessageType in form of a script file
    2. an Editor Message Object (EMO) in form of a ScriptableObject (after automatic recompile)

    The MessageType can be used in code while the EMO can be used in the Editor to reference the MessageType via UI.


    📥 Receiving a Message

    1. Start by creating a new C# script with the name LogOnReceive:
    using UnityEngine;
    
    public class LogOnReceive : MonoBehaviour
    {
        public void MyReceiveMethod()
        {
            Debug.Log("Message received!");
        }
    }
    
    1. Create a new game object in your current scene. Name it Receiver Game Object
    2. Add the LogOnReceive component to the Receiver Game Object
    3. Add GlobalMessage's MessageReceiver component to the game object
      4.1. Select MyNewMessage as the Editor Message Object
      4.2. Select LogOnReceive's method MyReceiveMethod as a Receiver

    Receiver Game Object should now look like this:

    Receiver game object final form


    🐞 Testing Message Reception

    GlobalMessage offers comfortable tools for testing, debugging and keeping a an overview on the message structure of you project. First we want to check if our GameObject registers itself correctly as a Receiver for MyNewMessage.

    1. Open the Overview Window under Window > GlobalMessage and search for MyNewMessage.
    2. Start the scene. The Receiver Count should go up to one.

    Overview Window

    1. To find out who this Receiver is, double click the entry and the Info Window will appear.
    2. Navigate to the Receivers Tab. It shows the Receiver GameObject as a current Receiver.

    Info Window: Receivers tab

    Important

    GlobalMessage only displays the current Receivers. As most gameplay logic (i.e. the MessageReceiver component) only operates during Play Mode you have to start the scene before the info can be displayed.


    Now that we are certain Receiver registration is working correctly, we can go on to test if the Message is correctly processed by our Receiver. If everything works correctly, LogOnReceive component should print Message received! to the console.

    We can verify this very easily by triggering the message form the editor directly.

    1. While still in Play Mode, select the Info Tab in the Info Window.
    2. Click on the Send from Editor button.
    3. The log should appear in the console.

    Info Window: Info Tab

    Great! Now let's continue with sending Messages.


    📤 Sending a Message

    1. Create a new C# Script with the name SendOnRepeat:
    using System.Collections;
    using UnityEngine;
    using UnityEngine.Events;
    
    public class SendOnRepeat : MonoBehaviour
    {
        public UnityEvent OnRepeat = new UnityEvent();
    
        // Starts the loop
        private void OnEnable()
        {
            StartCoroutine(RepeatCoroutine());
        }
    
        // Triggers the "OnRepeat" event every 2 seconds while game object is active
        private IEnumerator RepeatCoroutine()
        {
            while (true)
            {
                yield return new WaitForSeconds(2.0f);
                OnRepeat?.Invoke();
            }
        }
    }
    
    1. Create a new GameObject in your current scene. Name it Sender Game Object.
    2. Add GlobalMessage's MessageSender component to the GameObject
      3.1. Select MyNewMessage as the Editor Message Object
    3. Add the SendOnRepeat component to the Sender Game Object
      4.1. Add MessageSender's method SendGlobalMessage to the On Repeat event

    Sender Game Object should now look like this:

    Sender GameObject final form


    🐞 Testing Message Senders

    Testing the Sender logic with our current setup is very easy: start the scene and the Sender should cause LogOnReceive to log Message Received! to the console every two seconds. Of course, GlobalMessage also offers more detailed tools to debug Senders.

    1. While the scene is still playing, open the Overview Window again. (Window > GlobalMessage and search for MyNewMessage)
    2. You should see the Sender Count increase every two seconds.

    Overview Window with Sender Count focused

    1. Double click the entry to open the Info Window. (Alternatively you can also open it by selecting the EMO in the Project View and clicking the Show full Info View in its Inspector View)
    2. Select the Senders Tab to see more info about each Sender (which is always Sender Game Object in our case).

    Info Window: Senders Tab


    🎉 Congratulations

    Great, you have just mastered the basics of GlobalMessage. If you want to learn more, head over to the Samples section, browse the Manual on your own, or start creating a project using GlobalMessage right away.

    In This Article
    Back to top Malte HusungLeave a ReviewLegal Noticethank you ❤️