Skip to content

Instantly share code, notes, and snippets.

@ZodmanPerth
Last active December 30, 2018 09:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ZodmanPerth/7dbb7b45fb103dafe6c3154c46901ba1 to your computer and use it in GitHub Desktop.
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/
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"})
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")
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)
extends Node
enum gameState { Initialising, PlayingScene, GamePaused }
enum gameCommand { PauseGame, GoHome, QuitApp, ContinueGame }
# var setActiveSceneArgs = { sceneInstance = "", scenePath = "", sceneName = "" }
extends Node
signal SetActiveScene(args)
onready var SceneButtonGrid = $menuContainer/VBoxContainer/MarginContainer/SceneButtonGrid
var Apps = [
{
name = "Touch Manipulation",
thumbnail = "res://game/menu/thumbnails/ManipulationTest.png",
path = "res://touchInput/touchInput.tscn",
},
{
name = "Accelerometer",
thumbnail = "res://game/menu/thumbnails/AccelerometerTest.png",
path = "res://sensorInput/sensorInput.tscn",
},
]
func _ready():
for app in Apps:
var sceneButton = load("res://game/menu/SceneButton.tscn").instance()
sceneButton.Initialise(app.name, app.thumbnail, app.path)
sceneButton.connect("pressed", self, "OnButtonPressed")
SceneButtonGrid.add_child(sceneButton)
func OnButtonPressed(scenePath, sceneName):
emit_signal("SetActiveScene", { scenePath = scenePath, sceneName = sceneName })
extends Control
signal MenuSelected()
signal QuitSelected()
signal PlaySelected()
onready var buttonContainer = $CenterContainer/Buttons
func _ready():
for button in buttonContainer.get_children():
button.connect("pressed", self, "OnButtonPressed")
func OnButtonPressed(id):
print(str("Clicked ",id))
match id:
"Menu":
emit_signal("MenuSelected")
"Quit":
emit_signal("QuitSelected")
"Play":
emit_signal("PlaySelected")
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