I have several instances with buttons. New commands only seem to affect the last instance created. How to tell Godot to do something with an instance that has been generated several steps before? I know they have specific names. I store these names in a file. But how do I know what instance with what name I have just used to click on a button?
return name of instance clicked
It is not quite clear what you are asking, perhaps some source code would help.
As to guess to get name information:
node.name node.get_path() node.get_filename()
What I need to check from what intance (with name) a signal has been send. So send a instance specific signal.
This post would in fact be helpfull. But I can't get it to work. It uses a Area2D and collistionshape2d to have something that can be clicked. If I add the code to the area2d (in the scene that needs to be instanced) and click on the instance, the word Clicked is not being printed...
https://reddit.com/r/godot/comments/4hy5it/how_to_get_a_node_by_mouse_click/
- Edited
To explain further and show you the code:
I have scene A with this code (there is a lot more, but this I think is relevant):
var clone
func kloon_maken(): #function to create the instance
clone = v_a_scene.instance()
func kloon_v_a_toevoegen(): #function to add an instance to the tree
node_to_spawn_at.add_child(clone)
clone.rect_min_size = Vector2(600,180)
# Connect the signals from the newly instanced scene to the correct functions:
clone.connect("plus_ingedrukt", self, "plus_ingedrukt_ontvangen"); #connects button 1
clone.connect("minus_ingedrukt", self, "minus_ingedrukt_ontvangen"); #connects button 2
clone.connect("edit_ingedrukt", self, "edit_ingedrukt_ontvangen"); #connects button 3
func plus_ingedrukt_ontvangen(): #do this if button 1 is pressed
Some functions
func minus_ingedrukt_ontvangen(): #do this if button 2 is pressed
some functions
func edit_ingedrukt_ontvangen(): #do this if button 3is pressed
some functions and print("edit pressed")
I have scene B with this code:
signal minus_ingedrukt
signal plus_ingedrukt
signal edit_ingedrukt
func _ready():
# Connect the signals here!
# This will make it where when the buttons are pressed, the correct function is called.
get_node("VBoxContainer/HBoxContainer2/HBoxContainer_plus/TextureButton_plus").connect("pressed", self, "_on_TextureButton_plus_pressed"); #connect button 1
get_node("VBoxContainer/HBoxContainer2/HBoxContainer_minus/TextureButton_minus").connect("pressed", self, "_on_TextureButton_minus_pressed"); #connect button 2
get_node("VBoxContainer/HBoxContainer2/HBoxContainer_edit/TextureButton_edit").connect("pressed", self, "_on_TextureButton_edit_pressed"); #connect button 3
func _on_TextureButton_plus_pressed():
# When the button is pressed, emit the signal from button 1!
emit_signal("plus_ingedrukt")
func on_TextureButton_edit_pressed():
# When the button is pressed, emit the signal from button 2!
emit_signal("edit_ingedrukt")
func _on_TextureButton_minus_pressed():
# When the button is pressed, emit the signal from button 3!
emit_signal("minus_ingedrukt")
Yet if I click on button 3 of a previously created instance, I get an error that the _on_TextureButton_edit_pressed method is not found. So I need a way to tell Godot witch instance it is I am clicking on.
Looking at the code, the issue might be your indentation. The function definitions need to be at the same indentation level as _ready
to be recognized as a function. Though I think the compiler would complain if the indentation is wrong, so I don't think that is necessarily the issue.
As for figuring out which instance is pressed, you could do something like this with the signals:
extends Node
signal custom_signal
func _ready():
emit_signal("custom_signal", self.name)
That is just an example, but the idea is more or less the same. You can pass a variable with the signal, so you can pass the name of the instance that the signal came from if that is what you are looking for.
Hopefully this helps!
- Edited
OK, I just edited the code, so the indents are gone (it was just bad copy/pasting). Sorry for that.
The emit_signal sounds interesting, but I have no clue how to connect it with scene A.
This is what I tried: Scene A
func _process(delta):
if clone!= null and Input.is_action_just_pressed("click_tap"):
clone.connect("naam_instance", self, "naam_instance_vergelijken")
elif clone == null and Input.is_action_just_pressed("click_tap"):
return
func naam_instance_vergelijken():
print(clone.name)
Scene B
signal minus_ingedrukt
signal plus_ingedrukt
signal edit_ingedrukt
var vraag_kloon
var antwoord_kloon
var vraag_edit_kloon
var antwoord_edit_kloon
signal naam_instance
func _ready():
get_node("VBoxContainer/HBoxContainer2/HBoxContainer_plus/TextureButton_plus").connect("pressed", self, "_on_TextureButton_plus_pressed");
get_node("VBoxContainer/HBoxContainer2/HBoxContainer_minus/TextureButton_minus").connect("pressed", self, "_on_TextureButton_minus_pressed");
get_node("VBoxContainer/HBoxContainer2/HBoxContainer_edit/TextureButton_edit").connect("pressed", self, "_on_TextureButton_edit_pressed");
get_node(".").connect("ready", self, "_on_instance_ready")
func _on_TextureButton_plus_pressed():
emit_signal("plus_ingedrukt")
func on_TextureButton_edit_pressed():
emit_signal("edit_ingedrukt")
func _on_TextureButton_minus_pressed():
emit_signal("minus_ingedrukt")
func _on_instance_ready():
emit_signal("naam_instance", self.name)
In the debugger I get Signal 'naam_instance' is already connected to given method 'naam_instance_vergelijken' in that object. And nothing is printed.
I just got the comment that
func on_TextureButton_edit_pressed():
emit_signal("edit_ingedrukt")
Should be:
func _on_TextureButton_edit_pressed():
emit_signal("edit_ingedrukt")
But that does not affect the emit_signal("naam_instance", self.name) And that still does not work.
- Edited
Solution If I use this code in scene B
signal custom_signal
func _ready():
emit_signal("custom_signal", self.name)
Then I can fetch that name (specific name of the instance) in scene A with this code:
#inside the function you want
general_name_of_instance.connect("custom_signal", self, "some_function");
func some_function(name): # name in between ()
print(name) #or do whatever you like with name
Changeed the topic to question so you can mark any relevant comments as answers if you'd like.