Free all nodes that have the same property and are the same type

func Item_pressed(key):
	data.Items[key] -= 1
	if data.Items[key] == 0:
		var texture_to_check = load("res://assets/items/"+key+".png")
		for button in get_tree().get_nodes_in_group("TextureButtons"):
			if button.texture_normal == texture_to_check:
				button.queue_free()
		data.Items.erase(key)
	$Cast/darken.hide()
	$Castless/Bag.hide()
	disable_btns(false)
	run_items.emit(key)

the buttons do not get killed when the value reaches 0

	data.Items[key] -= 1
	if data.Items[key] == 0:
		var texture_to_check = load("res://assets/items/"+key+".png")
		for button in get_tree().get_nodes_in_group("TextureButtons"):
			if button.texture_normal == texture_to_check:
				button.queue_free()
		data.Items.erase(key)

the problem seems to be here

on a side note as im 172 commits into this project and 36 of those today maybe i should also take a break for the rest of today

It’s probably because when you execute “load” you’re actually creating a new reference for that image. You’re not just “loading the image”, you’re creating a resource in memory which is different than the image you have in your buttons. So that comparison if button.texture_normal == texture_to_check: will always return false.

Maybe find some other way to identify the buttons you have to free.

this makes since let me think on it for a bit kind of like if you make a file with class_name and then reference it in another file your creating a new version of said class file

then how would i do something like checking if it uses the same image path just use a string like

"res://assets/items/"+key+".png"

You could compare the texure_normal.resource_path.

data.Items[key] -= 1
	if data.Items[key] == 0:
		var resource_path: String = "res://assets/items/"+key+".png"

		for button in get_tree().get_nodes_in_group("TextureButtons"):
			if button.texture_normal.resource_path == resource_path:
				button.queue_free()
		data.Items.erase(key)
1 Like

alright im sure that would work but somwthings not working

func update_bag():
	for key in data.Items.keys():
		var new_button : TextureButton = TextureButton.new()
		var new_text : Label = Label.new()
		$Castless/Bag/Container.add_child(new_button)
		new_button.add_child(new_text)
		new_text.position = new_button.position
		new_text.position.y += 30
		new_text.text = str(data.Items[key])
		new_button.texture_normal = load("res://assets/items/"+str(key)+".png")
		new_button.pressed.connect(Item_pressed.bind(key))
func Item_pressed(key):
	data.Items[key] -= 1
	if data.Items[key] == 0:
		var texture_to_check = "res://assets/items/"+key+".png"
		for button in get_tree().get_nodes_in_group("TextureButtons"):
			print(button.texture_normal.resource_path)
			if button.texture_normal.resource_path == texture_to_check:
				button.queue_free()
		data.Items.erase(key)
	$Cast/darken.hide()
	$Castless/Bag.hide()
	disable_btns(false)
	run_items.emit(key)

the print statment never gets called?

@export var Items : Dictionary = {
	"potion": 1,
}
for button in get_tree().get_nodes_in_group("TextureButtons"):

all code after this line doesnt run meaning theres no TextureButtons in the scene?

1 Like


one is there though

Are your Texture buttons marked in a group? It does not automatically collect TextureButton type nodes

this would be the problem

func update_bag():
	for key in data.Items.keys():
		var new_button : TextureButton = TextureButton.new()
		var new_text : Label = Label.new()
		$Castless/Bag/Container.add_child(new_button)
		new_button.add_child(new_text)
		new_text.position = new_button.position
		new_text.position.y += 30
		new_text.text = str(data.Items[key])
		new_button.texture_normal = load("res://assets/items/"+str(key)+".png")
		new_button.pressed.connect(Item_pressed.bind(key))

in here how would i add the button to a group

You can set a better name for the group here too, If you use an instanced scene instead of making the button piece by piece you can set the group in the editor.

new_button.add_to_group("ItemButton")
1 Like

worked like a charm

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.