PDPFU - Observer Pattern - Statics and Delegate

I use Observer a lot these days now I’ve veered away from Singetons

public static Action<Collectable> OnCollect = delegate { };

I tend to use a Static Action so don’t need to call GetComponent.
Are there any issues with this? Other than you dont like static!

I also add = delegate { };
on the end of my Action and so dont need to check if its not null and just call it.

OnCollect(collectable);

From what I remember UnityEvents were not so good passing multiple bits around when I was playing with them so I tend to just use the C# one.
Plus if you have a number of UnityEvents set up in the Inspector and you accidently reset or corrupt it, it can be a nightmare trying to remember what needed to know about it so I found ‘hardcoding’ it better and safer

@sampattuzzi is the ‘discussion area’ supposed to still be there or was that for reviewers as that what the contents seem to be mostly about (I’m here via the Humble Bundle)

Good to see you at this short one.

There weren’t reviewers for the patterns course.

They’re fine for it they just need an extra empty class. I like them. I use them a lot. They’re a little limited to what you can pass when you’re hooking things up through the inspector, but for most purposes, they’re like Actions.

Statics pretty much turn portions of classes into singletons, so they kind of act the same way, but they live through object death, so if you’re not aware, they can cause some gotchas when you load another scene, but if you’re registering and unregistering, you should be fine.

It was for early feedback. I’m going to phase that out now.

C# Trick: Null Coalescense
This works with delegates, Actions, Events, and UnityEvents:

OnCollect?.Invoke(collectable);

If the delegate, Action, Event, or UnityEvent is empty, it won’t Invoke.

Privacy & Terms