CSharpNotificationCenter Posting Messages with data

Hey guys, I am using the CSharpNotificationCenter and i cant work out how to attach some data to my message that i am posting. There isn’t really any examples on the Wiki. Could someone help me out here? The example given on the wiki does not (unfortunetly) send any data. When I try to send a hashtable in Post to a function listening that accepts a HAshtable, I get an error: MissingMethodException: The best match for method ErrorDialogue has some invalid parameter.

I never used this script, but it looks like of a custom implementation of delegates, which are supported by Unity. I would like to know the advantage of using this script instead of built-in delegates (after my first read of the community page I didn’t see any).

See: delegate (C# Reference) and Delegates (C# Programming Guide)

See also: Delegates Tutorial


edit: I think that the receiving method should have this kind of signature:

void MethodName(object arg)
{
    // cast inside the method
    Hashtable hash = arg as Hashtable;
    if (hash == null)
    {
        // error
    }
}

But I’m not sure since I never used Unity built-in messaging system (which is the final step of this implementation).

public class Notification {

    public Component sender;
    public string name;
    public Hashtable data;
    public Notification (Component aSender, string aName) { sender = aSender; name = aName; data = null; }
    public Notification (Component aSender, string aName, Hashtable aData) { sender = aSender; name = aName; data = aData; }
 
 
}

for me this solution worked:

* Example use Post Notification / AddObserver with data:
 * 
 * 1) Example use Post Notification:
 * 
	// Post Notification with data:
	Hashtable hashtable = new Hashtable();
	hashtable.Add("attribute1", 1000);
	hashtable.Add("attribute2", 55);	
	NotificationCenter.DefaultCenter.PostNotification(this, "OnBumperCollision", hashtable);
 * 
 *  2) Example AddObserver and reuse posted data of Notification:
 * 
	using UnityEngine;
	using System.Collections;
	public class testObserveB : MonoBehaviour {
		void Start () {
			NotificationCenter.DefaultCenter.AddObserver(this, "OnBumperCollision");
		}
		void OnBumperCollision (Notification note)
		{
			Debug.Log("OnBumperCollision detected!");
			// reuse data if it exists
			if (note.data!=null) {
				Debug.Log("Sender="+note.sender.name);
				Debug.Log("Data:");
				foreach (DictionaryEntry entry in note.data)
				{
				    Debug.Log(" "+entry.Key +"="+ entry.Value);
				}
			} else {
				Debug.Log("data is null.");
			}
		}	
	}

but befor this can work, the Notification class must be accessible from outside:

	public class Notification {
	 
			//public Notification (GameObject aSender, string aName, Hashtable aData)
			//{
			//	throw new System.NotImplementedException ();
			//}
	 
	    public Component sender;
	    public string name;
	    public Hashtable data;
	    public Notification (Component aSender, string aName) { sender = aSender; name = aName; data = null; }
	    public Notification (Component aSender, string aName, Hashtable aData) { sender = aSender; name = aName; data = aData; }
	 
	 
	}

this was not the case with the script in the wiki…

To send/access a notification’s data parameter you must first send some data along with your notification:

NotificationCenter.DefaultCenter.PostNotification(this, "OnPlayerEnter", someVectorThree);

Then:

void Start()
    {
        NotificationCenter.DefaultCenter.AddObserver(this, "OnPlayerEnter");
    }

void OnPlayerEnter(NotificationCenter.Notification note)
    {
        Vector3 someVectorThree = note.data;
    }