` Hello there. I am new, so sorry for the stupid question.
I have a scene called “bout” which is a UI element that I have to instantiate a certain number of times in the main scene with a ‘for loop’.
In “bout” I have a colorRect with 2 spinbox as a child.
Now for every “bout” copy I instantiate in the main scene, I want to save in different variables the value entered for every spinbox created. How can I do this? `
The structure of the code is like this and i tried saving the variables with different names with a dictionary but don’t know how to properly do it
@onready var testLabel = $"../Elenco assalti/Label"
...
var pointsDict : Dictionary = {}
var key
func _ready():
numberArrayElements = Global.arrayGroup1.size()
for t in range(0, numberArrayElements):
var bout = mynode.instantiate()
GridContainer1.add_child(bout)
...
var Spinbox1 = bout.get_node("SpinBox1") as SpinBox
var Spinbox2 = bout.get_node("SpinBox2") as SpinBox
...
key = "points" + str(t+1)
pointsDict[key] = Spinbox1.value
Spinbox1.value_changed.connect(_on_value_changed)
func _on_value_changed(value):
pointsDict[key] = value
testLabel.text = str(pointsDict["points1"])
How about inside bout.gd, you have a method called init_spinbox(value1, value2).
After you instantiated and added the bouts to your scene, you can call init_spinbox and give the values as parameters.
Thank you for answering. In the main scene script i wrote something like this
var spinboxValue1
var spinboxValue2
var mynode = preload("res://scenes/bout.tscn")
func _ready():
for i in range(0, numberArrayElements):
var bout = mynode.instantiate()
GridContainer1.add_child(bout)
var Spinbox1 = bout.get_node("SpinBox1") as SpinBox
var Spinbox2 = bout.get_node("SpinBox2") as SpinBox
Spinbox1.value_changed.connect(_on_value_changed1)
Spinbox2.value_changed.connect(_on_value_changed2)
func _on_value_changed1(value):
spinboxValue1 = value
func _on_value_changed2(value):
spinboxValue2 = value
And in the ‘bout.gd’ i wrote the func init_spinbox(spinboxValue1, spinboxValue2): but i can’t understand what i need to write in that function to save the values of every spinbox created in the scene in different variables and how to call it in the main scene
Thanks a lot for your patience, you’re really helping me. I tried to adapt a bit your code but when I write key in root.pointsDict[key]["spinBox1"] = value, Is it not as if you take the last value t? So “points” + (numberArrayElements +1) instead of the t of the spinbox whose value was changed?
the magic happens inside your for loop as we adjust the t value, which then is computed into the key variable.
Because we add the key value as a parameter into each bout instance’s init_spinbox() method, they each have their own specific key value.
Also because everyone knows about the root node’s pointsDict dictionary, they can write their spin box values into the dictionary.
What I wrote is untested pseudocode but I believe it should somewhat do what you require.
If you run the game, you are free to open up the remote tab and see all the different bout nodes inside your SceneTree. If my code is correct, each bout’s key member value should have a unique value.
I was wrong, this code works, I didn’t notice that it was overlaying everything in the GridContainer because I was importing a ColorRect without minimum size. Thank you a lot for everything!