Is there a way to catch all messages with one function?

In order to interact with objects in the world, my player sends out a short raycast that sends messages to the object that it is hitting. Messages include things like Interact, MouseHover, Pickup, etc. For some objects, I want to send the message upwards through the hierarchy, and for others I don’t.

To solve this, I created a message repeater script that would catch messages and send them upwards. So by adding this script to select objects, I can choose which ones send the message upwards and which ones don’t.

I can easily add a function for each message in the MessageRepeater script, but I was curious if there is a way to catch any message being sent to the object. For example:

function CatchMessage (message : String) {
    SendMessageUpwards(message);
}

What do you mean by a 'message': Do you mean you want to be notified every time a message is sent to the gameObject containing the script via SendMessage, or perhaps every time a method is called on the script, or something else entirely?

–

Yes, I want a single function that will be called every time any message is sent to the script via the SendMessage function.

–

1 Answer

1

No not directly. SendMessage finds the function using reflection. You could however do something a bit clever and write your own version of SendMessage that looks for a catch all component on the target game object and calls that as well as sending the message normally.

I don’t know what language you are using, if you let me know then I will cobble together some script for you.

Edit: Code for MessageEx

Ok - so you would build a file called

#MessageEx.js

#pragma strict

interface MessageCatcher
{
	function CatchMessage(message : String, parameter);
}

static class MessageEx
{
	static function Send(target : GameObject, message : String, parameter)
	{
		Send(target.transform, message, parameter);
	}
	
	static function Send(target : Component, message : String)
	{
		Send(target, message, null);
	}
	
	static function Send(target : GameObject, message : String)
	{
		Send(target.transform, message, null);
	}
	
	static function Send(target : Component, message : String, parameter)
	{
		var catchers = target.GetComponents(MessageCatcher);
		for(var catcher : MessageCatcher in catchers)
		{
			catcher.CatchMessage(message, parameter);
		}
		target.SendMessage(message, parameter, SendMessageOptions.DontRequireReceiver);
	}
}

This makes you now send messages using

   MessageEx.Send(someGameObjectOrComponent, "SomeMessage", optionalParameter);

The message gets sent as normal but anything implementing MessageCatcher can also get all of the messages. You might build an automatic broadcaster for instance:

#MessageBroadcaster.js

#pragma strict

class MessageBroadcaster extends MonoBehaviour implements MessageCatcher
{
	function CatchMessage(message : String, parameter)
	{
		BroadcastMessage(message, parameter);
	}
	
}

So if you add this to an object, any message sent to it with the MessageEx.Send will also be broadcast to all children.

Or an upwards router:

#MessageRouter.js

#pragma strict

class MessageRouter extends MonoBehaviour implements MessageCatcher
{
	function CatchMessage(message : String, parameter)
	{
		SendMessageUpwards(message, parameter);
	}
	
}

I am using javascript. I like that idea, and I would love to see how it is done.

–

I've added the code for a Javascript version

–

Handling a message is just the same as normal.

–

Very cool. I'll try that out. Thank you very much, whydoidoit.

–