I’m making a turn-based strategy game and I have units. I have a button attached to all these units and when I right click them, I would like to overlay a HUD on the game. However, that is where my problems begin, I don’t exactly know how to tell the script to overlay the HUD when I interact with the button on the unit. If I could get any tips that would be greatly appreciated
I presume your button is connected to a signal. Something like:
func _on_my_button_pressed() -> void:
# do something now button is clicked
Lets say your UI node is called UI, then you would do something like:
func _on_my_button_pressed() -> void:
UI.show_hud()
# or if you want to use signals
show_hud.emit()
Now that script has to know where your UI is, and how you do this depends on your game set up and node structures etc. You have choices here. You can hard code it as a node reference, set it as an export variable, or set it in code, or emit a signal your HUD collects.
# hard code it
var UI = $UI
# use an export variable and add it in the editor (it might be a ControlNode or a CanvasLayer)
@export var UI: ControlNode
# set it in code
var UI: ControlNode
func set_ui_reference(ui_node: ControlNode) -> void:
UI = ControlNode
# Use a signal
signal show_hud()
Then finally in your UI node, you will either collect that signal or have the called function ready etc.
If you give us more details about your actual set up (scene tree, which script is collecting the button signal etc) perhaps we can be more exact for you.
Where is the button script? ie where does the signal for the button connect to? And what function in the HUD are you wanting to run on the button click?
Supposing your Interact button connects to the UnitStorage. And you want to run the HUD function “show_swordsman_interaction”. In UnitStorage, since HUD is a child of it use a direct reference. So in your UnitStorage script would be something like:
right now, I have the button connected to swordsman. My initial solution was to emit a signal every time the button was interacted with. However, my problem is that I don’t know how to get the HUD script to detect when the button is interacted with.
Ok, so you still have many ways to do this. Is the swordsman something that is instantiated or is this tree structure as part of your UnitStorage a permanent fixture?
It would be easier to connect the button to the UnitStorage but I will presume you have your reasons. So ok, lets use a signal. You don’t have to here because your structure is very simple, but lets do it anyway.
As a general rule use signals to go up the tree and references to go down the tree.
Swordsman script:
signal show_hud()
_on_interact_btn_pressed() -> void:
show_hud.emit()
So this will emit the signal show_hud to anything listening for it. To listen for it you need to connect to that signal from somewhere. I would do it in the UnitStorage (which in your simple case seems overkill but it is good practice)
Unit Storage script:
First set up some variables to hold the node references for your HUD and Swordsman
@onready var HUD = $HUD
@onready var Swordsman = $Swordsman
Then in your ready function connect to the signal to listen for it:
Now finally, in your HUD script, deal with that function
@onready var Word = Word
func show_hud() -> void:
# Do whatever here
Word.visible = true
# Or tween it or animate it or whatever you are planning to do
There are other ways to do this though. Your button signal could connect directly to the HUD script if you wanted it to.
I just connected the button to the Swordsmans because it was the first thing that came to mind. If you could show me why it would be easier to connect the UnitStorage that would be great.
It just sounded like this whole unit was a single unit, ie, permanent in your tree or at least always existing. As if it were a self contained scene. If that is the case then there is no reason not to connect the button directly to the HUD.
However if your swordsman is one of many, or will later be a separated scene and perhaps you load in an axeman or a horseman for instance, then don’t do this. Signal up the tree and use references down the tree.