So, how do I broadcast a message to everything - to all gameObjects in my project?
I would have thought that maybe something like GameObject.BroadcastMessage(…) would do the trick, but it doesn’t.
TIA!
So, how do I broadcast a message to everything - to all gameObjects in my project?
I would have thought that maybe something like GameObject.BroadcastMessage(…) would do the trick, but it doesn’t.
TIA!
Have an array of all game objects, then loop through them and do SendMessage to each one. Or, make an empty game object in the root of your scene, and have all objects be children of it. Then BroadcastMessage on that object will send to everything.
–Eric
Dangit. I was hoping to avoid both of those approaches.
I was thinking maybe there was an implied master GO at the heart of my project, thus the “GameObject” reference.
I’m thinking though that it’s probably more engine/processor-efficient to shoot messages at specific objects (or an array thereof) instead of across the entire project space.
Thanks, Eric5h5. BTW, you’ve got to explain that username to me one day. ![]()
It’s just an auto-generated name from a long-ago website, since “EricH” was already taken…and I kept using it…
–Eric
even if there would have been one (or you add one yourself, such a root) it would still be hell inefficient as the broadcast internally does nothing but bruteforce search all GOs and send it there.
If you have such a wide number of targets I would recommend to consider events and let the monobehaviours that need to be informed “register” themself with the event handler
Yeah, dreamora (another interesting username), I thought as much.
Worse part is, I only want to send my message to a couple of objects right now. I just wondered if I could get away with being lazy about it, and accommodate future recipients that I have no a priori knowledge of.
I think Ill just stick with discrete messages for now.
Thanks a bunch you guys!
The “not knowing who to adress” or “being lazy and not wanting to know” is why I wrote the above.
Iwould not consider that lazy but smart, the one sending the message should never need to search for the recipients, it should be the recipients that tell him. Thats called Observer Pattern and exactly what the Event - Event Handler stuff above is about. It means that the class sending out the messages does not need to know who is going to be interested at all, all it needs to know is if there is anyone interested at all (eventhandler != null) and if so just send it there which will then automatically be send to all those who asked to be informed about it.
In a simpler form if you don’t want to learn about even handlers or so, you would add 2 functions to your code and 1 List
The two functions are “AddToListenerList” and “RemoveFromListenerList” and would accept a MonoBehaviour as the parameter you hand it.
then all your class needs to do afterwards is go through this one list and send the message to each of them ![]()
(next step of that would be interfaces so you can directly call the function without the overhead and garbage from SendMessage)
To give a realworld comparision: What you try to do with the messaging and wanted to do with broadcasting is what spam does. it just blindly and bruteforce style sends out millions of messages to people not even interested in hearing about it.
But what you want to do is allowing the people to register for a newsletter so those who are really interested in your stuff are informed about your stuff and the rest is left alone, thats what the Observer pattern or the “listener list” above are about ![]()
Boy, everyone who knows how to use them sure do make Unity’s Event Handlers sounds pretty good. ![]()
Your analogies have convinced me too - it’s finally time for me to learn how to use them.
I wonder if the folks at UT are planning any Unite '11 sessions on event handling - that would be awesome!
So, uh, where’s a good resource to get started with Event Handlers?
The Unity docs and Unify Wiki seem somewhat silent on the subject.
I similar approach is used in FSMs, so that is not only another reason to learn (as FSMs are very important to video games), but also opens up many more resources for research.
Thats cause its nothing that Unity offers but a general .NET thing.
For C# you can learn about them at http://msdn.microsoft.com/en-us/library/aa645739(v=vs.71).aspx for example
Dang.
Yeah, I saw the .Net stuff on the internet but I’m strictly UnityScript for the moment.
C# really isn’t much different from unity script (java script) and it offers more functionality like generics instead of the horrible java script array class. It’s worth switching imo.
UnityScript has generics too etc
But in this case UnityScript is really a bit on the “weak end of the stick”, I’m not sure its capable of full event handler code.
but Antony is right that UnityScript has not much left that C# can’t do.
Typing is the same for example so
var i = 10; works on C# as UnityScript as C# 3.0+ has the variable data type
but unlike UnityScript C# has many additional features that are super powerfull (LINQ and many more)
Oh, believe me, Andy, I want to learn C#. Everything about it sounds amazing and Unity seems to be swiftly gravitating toward it as the Most Favored Language.
I was kind of hoping that this book (due out December) will be my ticket to the C# promised land: http://www.amazon.com/Professional-Unity-Multi-Platform-Game-Development/dp/1118063376/ref=sr_1_1?ie=UTF8&qid=1312145399&sr=8-1.
In the meantime, if anyone can suggest a useful resource or two, I’m all ears. ![]()
Well in that case I would look at Blursts MessageManager (I think thats what was it was called) that is on their Blog.
A similar system also is on the UnifyWiki
I don’t know if i understand exactly but could you not do a SendMessage objects based on a layer or tag identifier, and give the objects you want to send a message to that Layer or tag property.
nobody said you can’t
it will just be slow as “search for something” vs “I know who I want” will always lose on the time to do end and the more objects you have in the world, the worse it becomes
with simple game object light games that don’t target mobile you can do it as the desktop can compensate for it.
But at multiple thousand game objects or on mobile, you will lose flat out
Interesting I’ll keep that in mind
There’s no reason to use the horrible Javascript Array class even in Unityscript, and Unityscript has generics. Remember, this is Unityscript, it’s not actually Javascript.
–Eric