From 407b5128894712525e54ed64f353546aab698e75 Mon Sep 17 00:00:00 2001 From: unfa Date: Sun, 19 Dec 2021 23:11:24 +0100 Subject: [PATCH] Made the game mute itself automatically when loosing focus --- Game/Main.gd | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/Game/Main.gd b/Game/Main.gd index 8719b0a..8958bd5 100644 --- a/Game/Main.gd +++ b/Game/Main.gd @@ -23,6 +23,7 @@ var player_scene = preload("res://Assets/Characters/Player.tscn") var local_player: Node = null var local_player_focus_previous: GameFocus # to store focus that should be set after AWAY is gone +var mute_previous: bool class PlayerInfo: var name: String @@ -79,17 +80,32 @@ var game_score_limit = 10 #15 const destroy_free_player_crash_workaround = true +func get_mute() -> bool: + return AudioServer.is_bus_mute(0) + +func set_mute(mute) -> void: + if mute == null: # toggle + AudioServer.set_bus_mute(0, not get_mute()) + else: + AudioServer.set_bus_mute(0, mute) + # update the HUD icon + hud.get_node("MuteIcon").visible = get_mute() + func _notification(what: int) -> void: match what: NOTIFICATION_APPLICATION_FOCUS_OUT: Engine.target_fps = 5 + mute_previous = get_mute() + set_mute(true) if local_player: local_player_focus_previous = focus focus = GameFocus.AWAY + NOTIFICATION_APPLICATION_FOCUS_IN: # `0` means "unlimited". Engine.target_fps = 0 + set_mute(mute_previous) if local_player: focus = local_player_focus_previous @@ -206,8 +222,7 @@ func _input(_event) -> void: chat.chat_notification("Screenshot taken: " + str(res)) if Input.is_action_just_pressed("mute_audio"): - AudioServer.set_bus_mute(0, not AudioServer.is_bus_mute(0)) - hud.get_node("MuteIcon").visible = AudioServer.is_bus_mute(0) + set_mute(null) @rpc(any_peer, call_local, reliable) func game_over(winner): if local_player: