I can't access variables on instanced nodes

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

Hello,

I am trying to do something with instanced nodes. The instancing code looks something like

var node = []
var node_prefab = load("res://Node.tscn")

func _ready():
    for i in range(x):
        node.append(node_prefab.instance())
        add_child(node[i])

Then I change the values with something like this:

func _physics_process(delta):
    for i in range(x):
        node[i].variable = a

The variable in question depends on the node type. When the project runs, the code to assign the variables doesn’t work and nothing happens. However, when I check with print(), the correct variables aren displayed. It’s like my code is not correctly applying the change to the actual instanced nodes in the scene. What am I doing wrong?

Edit : I realize that some of the variables work. What doesn’t work is 6DOF physics joints. According to debug, the variables to set the spring force and the damping and link the joint to the body and wheels are getting passed to the joint, but the joint does nothing. It’s supposed to act as a car suspension but it just does nothing.

:Here’s a link to the problem

I don’t understand what’s going wrong. Is it something with joints specifically?

have you checked if the node in the array and the node added as child are actually the same object?

Andrea | 2021-01-02 23:27

I just checked the instanced nodes’ name and place in the hierarchy and it recieves the correct variables. I don’t know what’s happening.

Unityfugee | 2021-01-03 16:31

does it work if, instead of calling the joint from the array, you call it from the scene tree?
like using get_child(x) or get_node("WhateverNodeName")

Andrea | 2021-01-05 13:42

:bust_in_silhouette: Reply From: abelgutierrez99

I’m not sure if this would work:

func _ready():
    var nodeToAdd : Node
    for i in range(x):
        nodeToAdd = node_prefab.instance()
        node.append(nodeToAdd)
        add_child(nodeToAdd)

I had the same problem earlier and I solved adding the nodes to another node (like Spatial in 3D) (which should be a child of the root node) as children, so I can access them using built-in functions, in my code I use for child in scenarioTiles.get_children():

:bust_in_silhouette: Reply From: Unityfugee

I fixed it, I was doing

Joint.set_node_a(NodePath(node_a.name))

I was inputting the path of the node to join from the point of view of the node carrying the scrpt and that’s not how it works. I was supposed to input the path from the joint node, like:

Joint.set_node_a(NodePath("../"+node_a.name))