Page 1 of 1

Message Events via script

Posted: Wed Jan 08, 2025 9:54 am
by NicoMozes
Hi Tony,

Happy Wednesday! And happy 2025! I hope you are very well.

I have tried for a few months to figure this one myself, but no luck. I am trying to subscribe via script to the message system.

For example, I have a Quest Control component in a gameObject, and I'm using this:

questControl.SendToMessageSystem("Building:Selected");

But I cannot figure out how to receive this message. I'm trying to use Message Events, and I have looked at OnMessage, but I cannot understand how to use the MessageArgs via script. --> messageEvents.OnMessage(???);

Is there a way to do this via script? So far, I have used the Message Event component and just do everything via inspector, but the list of Messages to Listen for is quite long and I need to reduce it.

Thanks in advance!

Cheers
Nico

Re: Message Events via script

Posted: Wed Jan 08, 2025 10:42 am
by Tony Li
Hi and Happy New Year!

There's an example script in How To: Respond To Quest State Changes in Script

It listens for the message defined in the constant QuestMachineMessages.QuestStateChangedMessage, but you can listen for any message instead, like:

Code: Select all

MessageSystem.AddListener(this, "Building", "Selected");
Note: You could also send along a reference to the specific building that was selected, like:

Code: Select all

MessageSystem.SendMessage(this, "Building", "Selected", currentBuildingSelection);
and get that building reference from MessageArgs.firstValue.

Re: Message Events via script

Posted: Wed Jan 08, 2025 1:25 pm
by NicoMozes
Thanks Toni! That was exactly how to do it!

Cheers and keep up the amazing work you do!

Cheers,
Nico

Re: Message Events via script

Posted: Wed Jan 08, 2025 2:10 pm
by Tony Li
Thanks, and glad to help!