Add pause button as C# events reference #29
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
JIRA Ticket
Changelog
OnPauseAction
in ourGameInput
so that other GameObjects can listen on pause eventsPauseManager
for managing the pause state. Currently, all it will do is log the current state.How it works
Sooner rather than later, any game project will require communication between different GameObjects, MonoBehaviours, and ScriptableObjects.
And if you don’t manage this complexity correctly, it’ll come back and bite you in the behind in the form of long nights of debugging and self-hatred.
A popular pattern I’ve seen to facilitate communication is through C# delegates and events.
But what are they exactly?
Delegates
After reading the conceptual guide in the Microsoft documentation, here are a few notable points:
const namedFunction = function () { /* … */ }
in JavaScript.Action
,Func
, andPredicate
types to avoid declaring new types for delegates. The C# language has you covered. You can then define type aliases, such asusing MyAlias = Action<int>
for example.?
andInvoke
. Delegates may be null, so unless you can guarantee the presence of a delegate, you should invoke delegates withSomeDelegate?.Invoke(…)
.The difference between delegates and events
However, you may be confused about the difference between
delegate
andevent
— some examples:What gives? It seems like they both do the same thing, and the docs even mention that
event
is implemented usingdelegate
.The docs explain that the
event
keyword is like how C# properties encapsulate underlying C# fields:That is, while you can add/remove delegates from
DirectoryChanged
, you can’t reassignDirectoryChanged
entirely. You can’t writeDirectoryChanged = someNewHandler
and overwrite things, the C# compiler won’t allow it.The
event
keyword, when thrown atop the standardEventHandler
delegate, provides a form of encapsulation that you don’t get with the delegates alone.That’s nice. So what can I do with this?
You can use this as a decoupling mechanism between different GameObjects and MonoBehaviours, of course!
Instead of hard-coding behavior in a
PauseManager
for when the Pause button is pressed, have other parts of your game listen for a Pause event, and handle within their respective classes.You can see the
Files changed
tab that is part of this pull request for code examples, and also see the visual result of what I created below.Quality Assurance
It's a simple
P
to Pause, andP
to Unpause control: