I recently discovered messenger systems such as the Advanced C# Messenger on the wiki and I’m hazy on a few things.
There are a few different takes on the messenger on the wiki and they all split up implementation into two files – Callback.cs and Messenger.cs. Why is this? Callback.cs just contains some prototypes of delegates. I moved those prototypes to the top of Messenger.cs and everything seemed to work fine. Is there something I’m missing here? Also, I then tried replacing these custom delegate types with Action<>. Again, is there anything I’m overlooking by doing this? Everything seemed to work okay.
The other concern is that I’ve read these event systems can potentially foul up garbage collection due to the messenger holding on to references to delegates. Am I correct in assuming that if I always remember to RemoveListener for every AddListener this problem can be avoided?
I don’t think it matters much where the delegate definitions are defined as far as the compiler is concerned, that appears to be a matter of personal preference. I don’t really see any benefit to using Action unless you need to capture local state like a closure, but as I said I’m not familiar with these scripts and the use case they address.