Last active
December 30, 2018 09:44
-
-
Save ZodmanPerth/7dbb7b45fb103dafe6c3154c46901ba1 to your computer and use it in GitHub Desktop.
Pause Menu and navigation system for Godot. Blog post at http://www.redperegrine.net/2018/12/30/building-a-pause-menu-part-1/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extends "res://game/gameStateNodeBase.gd" | |
var _homeSceneInstance; | |
var _activeSceneInstance | |
func Initialise(homeScenePath): | |
_homeSceneInstance = load(homeScenePath).instance() | |
Home() | |
func SetActiveScene(args): | |
var nodeInstance | |
if (args.has("sceneInstance")): | |
nodeInstance = args.sceneInstance | |
elif (args.has("scenePath")): | |
nodeInstance = load(args.scenePath).instance() | |
else: | |
return | |
if (args.has("sceneName")): | |
print(["Playing Scene", args.sceneName]) | |
RemoveActiveScene() | |
_activeSceneInstance = nodeInstance | |
_activeSceneInstance.connect("SetActiveScene", self, "SetActiveScene") | |
self.add_child(nodeInstance) | |
func RemoveActiveScene(): | |
if (_activeSceneInstance == null): | |
return | |
_activeSceneInstance.disconnect("SetActiveScene", self, "SetActiveScene") | |
remove_child(_activeSceneInstance) | |
_activeSceneInstance = null | |
func Home(): | |
SetActiveScene({ sceneInstance = _homeSceneInstance, sceneName = "Home"}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extends Node | |
func _ready(): | |
# Set up game environment | |
Input.set_mouse_mode(Input.MOUSE_MODE_HIDDEN) | |
get_tree().set_auto_accept_quit(false) # Must be false to allow pause menu to work on Android | |
get_tree().set_quit_on_go_back(false) # Must be false to allow pause menu to work on Android | |
$gameStateManager.Initialise("res://game/menu/Main.tscn", "res://pauseMenu/PauseMenu.tscn") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extends "res://game/gameStateNodeBase.gd" | |
var _pauseSceneInstance; | |
var _currentGameState = gameState.Initialising | |
func Initialise(homeScenePath, pauseScenePath): | |
$activeSceneManager.Initialise(homeScenePath) | |
$pauseSceneManager.Initialise(pauseScenePath) | |
SetGameState(gameState.PlayingScene) | |
print([["Pause",gameCommand.PauseGame], ["Home",gameCommand.GoHome], ["Quit",gameCommand.QuitApp], ["Continue",gameCommand.ContinueGame]]) | |
func ExecuteGameCommand(command): | |
print(["ExecuteGameCommand", command]) | |
match command: | |
gameCommand.GoHome: | |
SetGameState(gameState.PlayingScene) | |
$activeSceneManager.Home(); | |
$pauseSceneManager.Hide() | |
gameCommand.PauseGame: | |
SetGameState(gameState.GamePaused) | |
$pauseSceneManager.Show() | |
gameCommand.ContinueGame: | |
SetGameState(gameState.PlayingScene) | |
$pauseSceneManager.Hide() | |
gameCommand.QuitApp: | |
get_tree().quit() | |
func SetGameState(state): | |
match state: | |
gameState.GamePaused: | |
get_tree().paused = true | |
gameState.PlayingScene: | |
get_tree().paused = false | |
_currentGameState = state | |
func _notification(what): | |
print(["Notification", what]) | |
match (what): | |
MainLoop.NOTIFICATION_WM_FOCUS_OUT: | |
ExecuteGameCommand(gameCommand.PauseGame) | |
MainLoop.NOTIFICATION_WM_QUIT_REQUEST: | |
ExecuteGameCommand(gameCommand.QuitApp) | |
MainLoop.NOTIFICATION_WM_GO_BACK_REQUEST: | |
ExecuteGameCommand(gameCommand.PauseGame) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extends Node | |
enum gameState { Initialising, PlayingScene, GamePaused } | |
enum gameCommand { PauseGame, GoHome, QuitApp, ContinueGame } | |
# var setActiveSceneArgs = { sceneInstance = "", scenePath = "", sceneName = "" } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
naming file |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extends "res://game/gameStateNodeBase.gd" | |
var _pauseSceneInstance | |
func Initialise(pauseScenePath): | |
var gameStateManager = self.get_parent() | |
_pauseSceneInstance = load(pauseScenePath).instance() | |
_pauseSceneInstance.connect("MenuSelected", gameStateManager, "ExecuteGameCommand", [ gameCommand.GoHome ]) | |
_pauseSceneInstance.connect("QuitSelected", gameStateManager, "ExecuteGameCommand", [ gameCommand.QuitApp ]) | |
_pauseSceneInstance.connect("PlaySelected", gameStateManager, "ExecuteGameCommand", [ gameCommand.ContinueGame ]) | |
_pauseSceneInstance.hide() | |
self.add_child(_pauseSceneInstance) | |
func Hide(): | |
_pauseSceneInstance.hide() | |
func Show(): | |
_pauseSceneInstance.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment