Multiple Button instance for the same function

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By jehandante

I’m a noob, so the question might sound dumb. Let’s go anyway.

I want to do a very simple program with n TextEdit modules, and n buttons each “attached” to it’s own TextEdit. And when you click on one particular button, it copies the content of the TextEdit it is attached to.

I created the script and it works very well. Here it is:


func _on_Button1_pressed():
	get_node("text1/TextEdit1").copy()

func _on_Button2_pressed():
	get_node("text2/TextEdit2").copy()

func _on_Button3_pressed():
	get_node("text3/TextEdit3").copy()

Of course, if I want to do a hundred of those, that’s going to be fastidious. I’m not a programer (I wouldn’t ask that question if I was), but I’m pretty sure there is a way to make a global function so every time I add a new TextEdit and a Button, given that I attach them in the right way, I would have the same result without defining a new function every time.

My idea would be something like: every TextEdit would be the child of one Button. And the general idea of the function would be:
“when a button is pressed, get his TextEdit child node and copy”.

And I don’t know how to do that :slight_smile:
Could someone explain it to me?

:bust_in_silhouette: Reply From: estebanmolca

I do not know if I understood correctly, English is not my native language, but from what I see you have to create a new scene, add the textedit and the button as children, they can be children of a Control or a Node or the same textedit may be the root. Add a script to the parent node and configure the desired behavior including the button signals to the script function. Save your scene. Now you can use this saved scene to instantiate it as many times as you want, each instance behaves independently (This is the main reason why I like godot so much!) Now you can access a specific script of some instance from your main project , since all the instances have a different name and all the instances have a copy of the script that I created in the original scene. The godot manual explains the topic of instances and communication between scripts. Any questions or if it is something else you want to do leave me your comment.

Example:
Escene save as TextEditEscene.tscn:

  • TextEditScene (Parent node, type: Node2D, whit attached script)
    ----- TextEdit (children node, type: TextEdit)
    ----- Button (children node, type: Button, whit signal to the script in the parent node)

The script:

extends Node2D
func _ready():
	pass

func _on_Button_pressed(): 
	$TextEdit.copy()

-------------------end textedit scene----------------------------------------------------

In your main Escene script:

var text_node=preload("res://TextEditEscene.tscn")
func _ready():
	for i in range(6): #example instantiate 6 times
		var temp=text_node.instance()
		temp.set_position(Vector2(10 , i * 100)) #use i to change y position
		temp.name= "panel"+str(i) #custom name, ej: panel1,panel2, etc
		add_child(temp) 

estebanmolca | 2020-05-28 04:22

Sorry for the late reply. This is very nice.
My first try was that method but I didn’t know how to do it for it to work.

I don’t fully understand the script in the main scene, but I believe I can make that work, with a little bit of digging in the godot docs.

Thank you very much for your help.

jehandante | 2020-06-07 04:07

Yes, I wanted to show how to dynamically create multiple instances with a loop but it is unnecessary. The simplest would be:

var text_node = preload ("res: //TextEditEscene.tscn")
func _ready ():
         var temp = text_node.instance ()
         temp.set_position (Vector2 (10, 10))
         add_child (temp)

You can create as many instances as you want with the same method, you would only have to change their position.

estebanmolca | 2020-06-07 04:36