0 votes

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.

Godot version 3.2.3
in Engine by (15 points)

2 Answers

+1 vote
Best answer

I think I've made something like that.
The process is a bit long for this format but It kinda works like this:
1. 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


2. 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()

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)

by (334 points)
selected by

exactly what I was searching for, thank you

+1 vote

I think this might be helpful:

As for adding button nodes to array, you might want to save button instances as objects and save them to the array.

by (23 points)

ok thanks, I will have a look

not exactly what I needed, but thanks

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.