|
| 1 | +[tags]: / "expert,module,hscript" |
| 2 | + |
| 3 | +# Custom Pause and Game Over states |
| 4 | + |
| 5 | +Creating custom pause and game over states was made much easier in the 0.8.0 update, allowing you to use two new vital properties in [PlayState](https://thekade.net/funkin-api/funkin/play/PlayState.html#shouldSubstatePause): |
| 6 | + |
| 7 | +- `shouldSubstatePause` |
| 8 | +- `isGameOverState` |
| 9 | + |
| 10 | +These two properties allow you to set if the default functionality of the next *sub-state* PlayState opens. |
| 11 | + |
| 12 | + |
| 13 | +## Custom Pause Menu |
| 14 | + |
| 15 | +### Module Setup |
| 16 | + |
| 17 | +A setup for a module is typically required, although a song/stage script could also work (as they also have access to the [onPause](https://thekade.net/funkin-api/funkin/modding/events/PauseScriptEvent.html) event) |
| 18 | + |
| 19 | +The difference between a Module and a Song script here would be what level of integration you want the pause menu to be. Do you want it to be overriding every pause? Or only on one song? |
| 20 | + |
| 21 | +With a module you can do both, but it's up to you if you want to do it another way. |
| 22 | + |
| 23 | +```haxe |
| 24 | +package kade.hex.modules; |
| 25 | +
|
| 26 | +import funkin.modding.module.Module; |
| 27 | +import funkin.play.PlayState; |
| 28 | +// Your custom pause state would be here |
| 29 | +import kade.hex.substates.HexPauseMenu; |
| 30 | +
|
| 31 | +// ... |
| 32 | +
|
| 33 | +class Hex_Hud extends Module |
| 34 | +{ |
| 35 | + public var shouldUseHexHud:Bool = false; |
| 36 | +
|
| 37 | + // ... |
| 38 | +
|
| 39 | + public override function onSongLoaded(event:SongLoadScriptEvent) |
| 40 | + { |
| 41 | + // A check to see if we're in the right note style for this hud |
| 42 | + if (PlayState.instance.noteStyle.id == "hex") |
| 43 | + shouldUseHexHud = true; |
| 44 | +
|
| 45 | + // ... |
| 46 | + } |
| 47 | +
|
| 48 | + public override function onPause(event:PauseScriptEvent) |
| 49 | + { |
| 50 | + if (!shouldUseHexHud) |
| 51 | + return; |
| 52 | + // Ignore base game's pause menu |
| 53 | + event.cancelEvent(); |
| 54 | +
|
| 55 | + // Flags to 'pause' the game |
| 56 | + PlayState.instance.persistentUpdate = false; |
| 57 | + PlayState.instance.persistentDraw = true; |
| 58 | +
|
| 59 | + // Create and show our pause menu |
| 60 | + final pauseSubState = new HexPauseMenu(); |
| 61 | + PlayState.instance.shouldSubstatePause = true; |
| 62 | + PlayState.instance.openSubState(pauseSubState); |
| 63 | + } |
| 64 | +
|
| 65 | + // ... |
| 66 | +} |
| 67 | +
|
| 68 | +``` |
| 69 | + |
| 70 | +## Custom Game Over |
| 71 | + |
| 72 | +A custom game over is similar, as it is also a sub-state that pauses the game. Though it is a bit different by also needing you to set the `isGameOverState` to `true` (along with other things) |
| 73 | + |
| 74 | +### Module Setup |
| 75 | + |
| 76 | +```haxe |
| 77 | +import kade.hex.substates.HexGameOverMenu; |
| 78 | +
|
| 79 | +// ... |
| 80 | +
|
| 81 | +class Hex_Hud extends Module |
| 82 | +{ |
| 83 | + // ... |
| 84 | +
|
| 85 | + public override function onGameOver(event:ScriptEvent) |
| 86 | + { |
| 87 | + if (!shouldUseHexHud) |
| 88 | + return; |
| 89 | +
|
| 90 | + // Ignore base game's game over menu |
| 91 | + event.cancelEvent(); |
| 92 | +
|
| 93 | + // General game over flags the game sets when calling its own game over |
| 94 | +
|
| 95 | + PlayState.instance.persistentUpdate = false; |
| 96 | + PlayState.instance.persistentDraw = true; |
| 97 | + |
| 98 | + PlayState.instance.isPlayerDying = true; |
| 99 | +
|
| 100 | + PlayState.instance.playerStrumline.clean(); |
| 101 | + PlayState.instance.opponentStrumline.clean(); |
| 102 | +
|
| 103 | + PlayState.instance.vwooshTimer.cancel(); |
| 104 | +
|
| 105 | + PlayState.instance.songScore = 0; |
| 106 | +
|
| 107 | + final gameOverSub = new HexGameOverMenu(); |
| 108 | +
|
| 109 | + PlayState.instance.isGameOverState = true; |
| 110 | + PlayState.instance.shouldSubstatePause = true; |
| 111 | + PlayState.instance.openSubState(gameOverSub); |
| 112 | + } |
| 113 | +
|
| 114 | + // ... |
| 115 | +} |
| 116 | +``` |
| 117 | + |
| 118 | +### Pause/GameOver Sub-State Setup |
| 119 | + |
| 120 | +Then you can create your own custom Sub-State that'll be opened. |
| 121 | + |
| 122 | +```haxe |
| 123 | +package kade.hex.substates; |
| 124 | +
|
| 125 | +import funkin.ui.MusicBeatSubState; |
| 126 | +
|
| 127 | +// ... |
| 128 | +
|
| 129 | +// This can also be HexGameOverMenu |
| 130 | +class HexPauseMenu extends MusicBeatSubState |
| 131 | +{ |
| 132 | + // ... code is surely here |
| 133 | + // also: to close this sub-state, simply calling `close();` will work! |
| 134 | +} |
| 135 | +``` |
| 136 | + |
| 137 | +> Author: [Kade](https://github.com/Kade-github) |
0 commit comments