How To: Use Pixel Crushers Message System

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
User avatar
Tony Li
Posts: 22049
Joined: Thu Jul 18, 2013 1:27 pm

How To: Use Pixel Crushers Message System

Post by Tony Li »

This post describes how to use the Pixel Crushers Message System, which is a general-purpose message system that's heavily used by Quest Machine and can also be used by the Dialogue System.

Messages have three parts:
  • The message (a string or StringField)
  • An optional parameter (a string or StringField) [EDIT: parameter required, but it can be a blank string]
  • Zero or more optional values of any type.
An example message could be:
  • Message: "Entered"
  • Parameter: "Jungle"
  • (No extra values)
To send that message, use the MessageSystem class's MessageSystem.SendMessage() method.. Example:

Code: Select all

// Send Entered + Jungle from 'this' (the class/object that is running this code):
PixelCrushers.MessageSystem.SendMessage(this, "Entered", "Jungle");
To make a script listen for messages, implement the C# interface IMessageHandler and register to listen for specific types of messages. Example:

Code: Select all

public class EntranceAlerter : MonoBehaviour, IMessageHandler
{
    void OnEnable()
    {
        // Listen for any "Entered" + "(whatever)" messages:
        PixelCrushers.MessageSystem.AddListener(this, "Entered", string.Empty); // Empty string means any parameter.
    }
    
    void OnDisable()
    {
        PixelCrushers.MessageSystem.RemoveListener(this);
    }
    
    void OnMessage(MessageArgs messageArgs)
    {
        if (messageArgs.message == "Entered")
        {
            if (messageArgs.parameter == "Jungle")
            {
                DialogueManager.ShowAlert("Welcome to the jungle, we got fun and games!");
                PlayMusic("GnR");
            }
            else            
            {
                DialogueManager.ShowAlert("Entered some boring area named " + messageArgs.parameter);
            }
        }
    }
}
---

Sender and target:
Messages are always sent from a sender. The sender is specified in the first parameter to MessageSystem.SendMessage.

You can also address a message to a specific target by using MessageSystem.SendMessageWithTarget. The message will still be sent to all listeners, but messageArgs.target will be set to the target. The target listener (or any listener interested in messages to the target) can check messageArgs.target.
NotVeryProfessional
Posts: 150
Joined: Mon Nov 23, 2020 6:35 am

Re: How To: Use Pixel Crushers Message System

Post by NotVeryProfessional »

Brilliant, though it appears the string parameter isn't optional, as the system complains if you leave it off. ("no overload method has 2 parameters").
User avatar
Tony Li
Posts: 22049
Joined: Thu Jul 18, 2013 1:27 pm

Re: How To: Use Pixel Crushers Message System

Post by Tony Li »

Sorry, by "optional" I meant that you can just pass an empty string.
NotVeryProfessional
Posts: 150
Joined: Mon Nov 23, 2020 6:35 am

Re: How To: Use Pixel Crushers Message System

Post by NotVeryProfessional »

One more question, so I understand correctly:

It is always the OnMessage() method that gets called, you can't specify a method to call as in other message systems?


To clarify, right now with TigerForce Easy Message, I set it up like this:

Code: Select all

			EventManager.StartListening("SETTLEMENT_SELECTED", OnSettlementSelected);
			EventManager.StartListening("CHARACTER_ARRIVED", OnCharacterArrived);
			EventManager.StartListening("GAMETICK", UpdateContent);
I would have to change the to a switch() statement in OnMessage or something?
User avatar
Tony Li
Posts: 22049
Joined: Thu Jul 18, 2013 1:27 pm

Re: How To: Use Pixel Crushers Message System

Post by Tony Li »

Yes, that's correct.
Post Reply