I am in the process of making a 2d runner game in Godot. I am a beginner and I am struggling with setting up the HUD properly. The image below shows my level 1 progress and the points add up correctly when the coin and pies are eaten. The hud is a node/canvaslayer tscn that is autoloaded to all pages, and that's exactly the problem.
The HUD is showing up in pages where its not meant to, like the options page. how do I deal with this.
MainHud script:
extends Node2D
var score = 0 setget set_score
onready var lives = 3 setget set_lives
var pies = 0 setget set_pies
func set_score(value):
score = value
$HUD/Score.set_text("SCORE: "+str(score))
func set_pies(value):
pies = value
$HUD/Pies.set_text("PIES: "+str(pies))
func set_lives(value):
lives = value
$HUD/Lives.set_text("LIVES: "+str(lives))
if lives <= 0:
#change scene to dead scene
print("hello")
pass
The functionality is alright in this but if I want to make a points summary page when the user runs out of lives, or remove the icons from unneeded pages, how do I do that?
To sort of make sense of it all, I decided to make a global variable script, but now I don't know how to connect them together.
extends Node
onready var lives setget set_lives, get_lives
onready var score setget set_score, get_score
onready var pies setget set_pies, get_pies
onready var nextscene setget set_pies, get_pies
func _ready():
lives = 3
score = 0
pies = 0
func set_lives(value):
lives = value
func get_lives():
return lives
func set_score(value):
score = value
func get_score():
return score
func set_pies(value):
pies = value
func get_pies():
return pies
func set_nextscene(value):
nextscene = value
func get_nextscene():
return nextscene
Any help would be greatly appreciated.
One way is to just call hide()
on any UI elements you wish to hide. Call show()
to make it visible again.
Example:
main node's script:
func set_lives(value):
lives = value
$HUD/Lives.set_text("LIVES: "+str(lives))
if lives <= 0:
#change scene to dead scene
#Call setting function when you want to hide gameplay elements, like here
set_gameplay_ui_visibility(false)
print("hello")
pass
#Function for setting any gameplay UI elements visibility
func set_gameplay_ui_visibility(value):
if value:
$HUD/Score.show()
$HUD/Pies.show()
$HUD/Lives.show()
else:
$HUD/Score.hide()
$HUD/Pies.hide()
$HUD/Lives.hide()
You can find more example from official docs here: https://docs.godotengine.org/en/stable/getting_started/first_2d_game/06.heads_up_display.html