Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

C# Callback Notification/Messaging System and States Demo

Discussion in 'Community Learning & Teaching' started by GarBenjamin, Oct 8, 2014.

  1. GarBenjamin

    GarBenjamin

    Joined:
    Dec 26, 2013
    Posts:
    7,441
    Hey all.

    I am off work this afternoon and was thinking after mowing the lawn I might create a demo showing how to build a C# Callback Notification/Messaging Class and implement states to control game objects.

    I don't mean a video. Instead just a Unity project that contains all of the code including the Callback Notification/Messaging class that can be modified as needed for your projects.

    The actual demo to illustrate the use of the object to object communication as well as states would be something simple. Right now I am thinking basically a couple squares A and B representing two soldiers out on a mission to locate the player. Along the way they'd encounter something like maybe a pond and the leader would decide to set up camp. He'd order his buddy (subordinate actually) to fetch some wood and build a fire. After a rest they'd resume their mission.

    The whole point of that would be to show how to use states to control what they are currently doing and to use the Callback Notification System to communicate. Like the leader would ask the other dude for his current status, current coordinates and so forth and would use that information to issue orders to the other guy. The other dude, the "follower", would respond to the requests for information and carry out movement and action orders.

    The reason I am thinking of doing this is because I have seen many posts asking how to implement states or AI and also many posts asking about communication between different game objects. Usually people talk about setting up direct references to the other game objects which leads to tightly coupled code. Just wanted to show another option that I personally prefer.

    Maybe this doesn't belong here because what I am proposing is just some well-documented code along with a completed Callback Notification/Messaging System (source) and a demo project to illustrate states and using the Callback class. Please note, this is not about Events. This will be a managed Callback Notification/Messaging class handling subscribers and publishers and communication to all subscribers of a particular NotificationType or communication to a specific subscriber of a NotificationType.

    I don't want to step on anybody's toes though and I did a quick search and noticed a Notification/Event class on the Asset Store. So, not sure if I should do this and just share it for free. That might not be too cool.

    That's why I am asking for feedback. Would it still be cool to do it? My Notification system would just broadcast to all subscribers and communicate directly with a single subscriber by their Unity InstanceID. No messing with names, tags or other strings because I don't like that stuff when it comes to things like games. May use some for collision checking though.

    Anywho, I am heading out to mow now. Please comment and let me know if there is any interest in this and if you think it would be okay to do it despite someone currently selling an event / notification system on the Asset Store.

    Thanks!
     
  2. GoesTo11

    GoesTo11

    Joined:
    Jul 22, 2014
    Posts:
    604
    I'd be interested and I think that it is totally cool even though something similar is on the asset store. So your method is different than the .Net events system described on unitygems.com?
     
    GarBenjamin likes this.
  3. GarBenjamin

    GarBenjamin

    Joined:
    Dec 26, 2013
    Posts:
    7,441
    Not familiar with unity gems so I'm not sure. I would think so since I just made my own Notifications Manager class. But since there are only a few distinct approaches to take they may be similar. Hmm... I will do a search and see if I can find their system.

    I started out making references to other gameobjects and their scripts like all of the examples around the net show. It's not a completely terrible way of doing it but as a project grows in size I think it will make things pretty messy and on top of tbat it means all of the different gameobjects and scripts end up with references to each other for some very tight coupling. That's why I made a Notifications Manager. It is used in my WIP platform game.

    Anyway let me do a quick search for unity gems system.
     
  4. GarBenjamin

    GarBenjamin

    Joined:
    Dec 26, 2013
    Posts:
    7,441
    Yeah their system is straightforward events. Events are cool very useful at times. I use them in some projects at work especially for dealing with error handling in interfaces and communicating important events to consumers of my services. But what I am using in Unity is pure callbacks. The concept is similar to events as far as one object notifying another of something but the delegate based system is more flexible in my opinion.

    Let's see if there is any more interest in this.