- Home /
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.
Answer by BjoernS · Jan 15, 2013 at 12:02 PM
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...
Answer by studio1h · Jan 13, 2015 at 02:46 AM
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;
}
Answer by Kryptos · Jul 25, 2012 at 12:02 PM
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).
here is a good description of the benifits i suppose. I really like the messaging system, it is indeed a custom implementation of delegates, but it follows the observer pattern.
I will take a look at it after work. Thanks for the info.
Did anyone get any further with this? I'm trying to use the C# version of NotificationCenter and I'm also stuck on how to send data.
I have not figured out how to send data (The script above does not work), but have been successful with this:
http://wiki.unity3d.com/index.php?title=CSharp$$anonymous$$essenger_Extended
Answer by BjoernS · Jan 15, 2013 at 12:02 PM
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; }
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613074655im_/https://answers.unity.com/themes/thub/images/avi.jpg)