Topic was automatically imported from the old Question2Answer platform.
Asked By
SSG2710
I’m trying to create some button nodes dynamically, depending in an array, also I want to give those buttons a script (or something to make them functional).
I haven’t found anything that works for me in other posts.
thanks four your help in advance.
I think I’ve made something like that.
The process is a bit long for this format but It kinda works like this:
Create button instance/scene and add a script to it. Say (res://button.tscn) with script like:
extends Button
var something setget set_something
func set_something(value):
something = value
_on_button_up():
get_tree().change_scene(something)
Go back to wherever scene you want to dynamically create buttons and on your script. Create the buttons dynamically:
var button_pck = preload("res://button.tscn")
var array = [ "res://chapter1.tscn","res://chapter2.tscn",.....]
for n in array:
var button = button_pck.instance()
button.set_something(n)
add_child(button)
This way, I could just keep adding to the array and buttons should be dynamically created and each button event would be unique(based on the array contents or whatever I passed to it anyways)