Hi, I wanted to share my way of avoiding string references with BroadcastMessage, since it’s so easy to make mistakes with strings. For other non-BroadcaseMessage situations I’d normally use a static class of string constants, but I thought I needed a better way for BroadcastMessage where the compiler can check method names and whatnot. So, hopefully this might be useful to someone
I created an interface that includes a method with the OnDamageTaken() signature:
public interface IDamageResponder {
void OnDamageTaken();
}
Then instead of calling BroadcastMessage with the string reference, I call this line which takes advantage of the compiler, so we can be sure there are no mistakes from string references:
BroadcastMessage(nameof(IDamageResponder.OnDamageTaken));
Finally, any class that wants to listen to the broadcast (such as the EnemyAI) can then implement the IDamageResponder interface, and the compiler will force us to implement the OnDamageTaken method.
It’s not perfect but I find it useful for preventing any issues arising if we need to change any method names in the future. I’d love to hear any other ways to achieve this kind of result too!