- Home /
BroadcastMessage has no receiver: should I care?
Hi! I have a button, it broadcasts messages to its children, which the user can unparent. If the user is a bit silly, he might hit the button whilst there are no objects to receive the message. Should I prevent the broadcasting if there's no one on the receiving end, or just leave it at that? In other words, check for receivers every time the button is presse,d or allow a useless BroadCastMessage?
Thanks for your advice!
Gregzo
could you just wrap your message stuff in an
if(theres some children or whatever I'm doing here){
then do what I'm doing;
}
then, if there isn't, it wont.. ??
Sure, but I was wondering if there's a downside to Broadcasting in solitude...
Answer by jahroy · Dec 15, 2011 at 06:30 PM
If you look closely at the documentation, you'll notice there's a third parameter, which is a SendMessageOptions argument.
The default value is SendOptions.RequireReceiver, which, according to the documentation, will print an error message if the message is not received by any component.
If you set it to SendOptions.DontRequireReceiver, the error message will not be printed.
I believe the downside to using BroadcastMessage is that it will operate on all children, which may not be super efficient. It probably doesn't matter unless you're doing it every frame.
If the object has no children (or a small number of children) the impact should be nothing (or almost nothing).
Yep, aware of that, just wondering if there was a downside to a receiverless BroadCast$$anonymous$$essage , because it seems to me all the DontRequireReceiver parameter does is preventing the error message in the console, no?
Yes. $$anonymous$$y answer is my attempt at answering your question. That's all I got.
I think the overhead of Broadcast$$anonymous$$essage is calling it on each component/child of an object.
I think that's all you need to know.
It basically says, here's component A, does it have a function named B? If so, use reflection to call it. If not, move on to the next component.
If you're okay with that, there is no downside.