-
Notifications
You must be signed in to change notification settings - Fork 1
Pausing Logic #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,30 +2,58 @@ | |
|
|
||
| namespace Mediabox.GameKit.GameManager { | ||
| public class PauseHandler { | ||
| float timeScaleBeforePause; | ||
| float volumeBeforePause; | ||
| bool isPaused; | ||
| float timeScaleBeforePause = 1; | ||
| float volumeBeforePause = 1; | ||
|
Comment on lines
+5
to
+6
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These initialized values wouldn't be necessary if it wasn't for the double-pause change below.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These are actually in case someone were to use a subscribed pauseEvent, and somehow removed the subscription before the unpause. In that case no value for the timescalebeforepause would be recorded, and the default being 0, would mean that the game would "unpause" to timescale 0.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (reiterating a note from above, if we move the Pause into the Game base. Then I think we need to initialize these values in each game, as the memory for them will be lost as the games unload and then load anew.) |
||
| bool isPaused = false; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (am I being marked down for style sheets?) :P |
||
|
|
||
| public delegate void OnPause(bool pausing); | ||
| public event OnPause onPause; | ||
|
Comment on lines
+9
to
+10
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The PauseHandler is currently only being used as a The flow would be:
Then again, I don't think that there's any need for this right now, as long as
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I actually think this change is much better than having the pause handler at all. As we currently have to do the work of locating the PauseHandler before we can subscribe to its delegate event to do the unique pausing functions we want. Putting the default pause into the Game base, and then letting us override it I think will be much simpler for new users to the plugin to work with.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The Game base float value for timeScaleBeforePause, will have to be initialized as 1. As the Game will unload when exiting, and a new Game will load when entering, and then that game will not have a stored value to react to the UnPause. This might be a nicer pattern, as we don't have to call Reset values at all, as each new game, when initialized will be providing reset values. |
||
|
|
||
|
|
||
| public void Pause() { | ||
| if (!this.isPaused) { | ||
| this.timeScaleBeforePause = Time.timeScale; | ||
| this.volumeBeforePause = AudioListener.volume; | ||
| this.isPaused = true; | ||
| } else { | ||
|
|
||
|
|
||
| if (this.isPaused){ | ||
| Debug.LogWarning("Pause() has been called multiple times. To avoid unintended behavior, it is recommended to ensure calling Unpause() before Pausing again."); | ||
| } | ||
| Time.timeScale = 0f; | ||
|
|
||
| this.isPaused = true; | ||
| this.volumeBeforePause = AudioListener.volume; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can skew output for Games where
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I figured this would allow mediabox to reset the game back to a playable state if all else failed for the user. For example, something happens when pausing that messes up the game. Instead of being locked in a time.timescale = 0 state. The user can use the wonderz button (blue arrow) to exit the game fully, pick the game again (or a new game), now the values for timeBeforePause have been hard reset. They can enter the game with the timescale corrected, and they can continue on playing without having to force quit the app.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Basically if they get to this point, we've already messed up, and we get the error flag while testing. But for the end user the product is still "serviceable" even while broken. |
||
| AudioListener.volume = 0f; | ||
|
|
||
| if (onPause != null){ | ||
| if (onPause.GetInvocationList().Length > 0){ | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not necessary as far as I know, since delegates don't pass a
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was going off of the belief that the delegate if null when you add the first subscriber will make a new delegate, and that the += operator was overridden for that use case. So it was possible to subscribe and then unsubscribe leaving a delegate that has an empty invocation list. However I have not tested this, should be simple enough to create a test tonight. |
||
| onPause.Invoke(true); | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| this.timeScaleBeforePause = Time.timeScale; | ||
| Time.timeScale = 0f; | ||
|
|
||
| } | ||
|
|
||
| public void Unpause() { | ||
| if (this.isPaused) { | ||
| Time.timeScale = this.timeScaleBeforePause; | ||
| AudioListener.volume = this.volumeBeforePause; | ||
| this.isPaused = false; | ||
| } else { | ||
|
|
||
| if (!this.isPaused){ | ||
| Debug.LogWarning("Unpause() has been called multiple times. To avoid unintended behavior, it is recommended to ensure calling Pause() before Unpausing again."); | ||
| } | ||
|
|
||
| this.isPaused = false; | ||
| AudioListener.volume = this.volumeBeforePause; | ||
| if (onPause != null){ | ||
| if (onPause.GetInvocationList().Length > 0){ | ||
| onPause.Invoke(false); | ||
| return; | ||
| } | ||
| } | ||
| Time.timeScale = this.timeScaleBeforePause; | ||
| } | ||
|
|
||
| public void ResetPauseValues(){ | ||
| onPause = null; | ||
| timeScaleBeforePause = 1; | ||
| volumeBeforePause = 1; | ||
| } | ||
|
Comment on lines
+53
to
57
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I really like this function! :)
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah! now I remember why I wrote it like this. Not having Daniel's code, I couldn't tell if UnPause was called before or after the game was loaded. If I set isPause to false before the unpause it would cause a double-unpause error. |
||
|
|
||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Totally makes sense! I'd love to merge that change into
main!Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To the point above, this reset game is called at the end of a game when content is unloaded, and at the start, so setting isPause to false, would mean we can't unpause when the next game is loaded. Trapping us in a paused state.