Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Enquest |
Could somebody tell me how you handle this code wise without repeating yourself.
I load json that has data of all my levels. I print out buttons one for one in a grid according levels defined in json.
I could make the button in a separate scene and load the instance for each loop. But that creates extra scene extra naming, extra files to manage. So I rather use duplicate(). However for the first $levelButton I have to assign things and then assign duplicate the rest. How do I avoid this inefficient code.
func _ready():
load_json()
for item in level_data:
if not $levelButton.level:
$levelButton.level = item
$levelButton.connect("pressed", self, "_start_level")
else:
var l = $levelButton.duplicate()
l.level = item
l.connect("pressed", self, "_start_level")
add_child(l)
found the solution
func _ready():
load_json()
for item in level_data:
var l = $levelButton.duplicate()
l.level = level_data[item]
l.connect("pressed", self, "_start_level")
add_child(l)
$levelButton.queue_free()
Enquest | 2018-04-21 15:43