Hi, I’m implementing a shop and I was wondering if it’s possible to queue_free()/set_visible(false) a specific button. So, if the player presses Rifle, then presses buy, the Rifle button disappears.
Below is the image of the set up (Ignore the signal by Town button). Below the image is the code.
extends Node2D
@onready var item_name: Label = $Control/ColorRect/ItemName
@onready var price: Label = $Control/ColorRect/Price
func _on_rifle_pressed() -> void:
$Control/Items.set_visible(false)
$Control/ColorRect.set_visible(true)
item_name.text = $Control/Items/Weapon/Rifle.text
price.text = 6
func _on_cancel_pressed() -> void:
$Control/ColorRect.set_visible(false)
$Control/Items.set_visible(true)
func _on_buy_pressed() -> void:
if can_buy(int(price.text)):
# Note, this works if the player has more coins
$Control/ColorRect.set_visible(false)
$Control/Items.set_visible(true)
# I think this is where I want to queue_free()/set_visible(false) for the specific button
Global.coins -= int(price.text)
else:
# This also works
print("Can't buy")
func can_buy(amount : int) -> bool:
if Global.coins > amount:
return true
return false
The only thing I can think of is having a bunch of @onready for each of the buttons, but I feel that might lead to a bunch of if else since I do want to add more weapons
A discovery I made is that buttons work with “”. So: $Control/Items/Weapon/”Rifle”. Not sure where to go with that.
You could store which item button you clicked last and then set it whenever you click one of them.
So in script wide scope: var last_pressed: NodePath
Then in the functions, like _on_rifle_pressed(), you just set it. last_pressed = $Control/Items/Weapon/Rifle
And then use that nodepath whenever you buy something successfully, where you have commented you think is the right place. get_node(last_pressed).queue_free()
There are other ways to approach this too. Main idea is to store the relevant buttons as last pressed to know which one you need to queue free.