How to connect buttons in button array to pressed signal?

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

This question might be stupid, but this is the first game I’m making and I really need some help.
I’m trying to make a puzzle game for connecting pipes. I put all of the needed buttons in array and when I run this program I get this on Debugger: Invalid get index ‘0’ (on base: ‘null instance’)
And on errors: get_node: (Node not found: “arg” (relative to “/root/Node2D/Button”).)

extends Button

var i=0
var arg=

func _ready():
arg.resize(76)
while(i < 76):
var button = Button.new()
arg[i]=button
add_child(arg[i])
$arg[i].connect(“pressed”, self, “myrotate”)
func myrotate():
arg[i].rotation_degrees+=90;

:bust_in_silhouette: Reply From: Wakatta

No need for the $
And you forget to update iwhich is why for is better for this kinda thing

extends Button

var arg=[]

func ready():
    arg.resize(76)
    for i in range(arg.size()):
        var button = Button.new()
        add_child(button)
        button.connect("pressed", self, "myrotate", [i])
        arg.insert(i, button)

func myrotate(i):
    arg[i].rotation_degrees+=90

Thank you so much. I’ve been working on this problem for 2 days and it finally works.

dontknowanything | 2022-11-14 13:03