For Loop not generating new Meshse

Godot Version

4.4.1

Question

Trying to make a loop in which I use a for loop to create nodes. To ensure the nodes are being created properly, I try to spawn meshes to track their position. However, when running it, I only see a single mesh at the last position, instead of a cube at each node position, regardless of how many nodes I make when I call GenerateGraph.



You are always using the same node by calling “$MapNode”. This means you will always get the first child when calling get_child(0). You would need to call get_child(i).

Also you are always adding the same node to the Nodes array, because of the previous mentioned mistake

1 Like

On posting code:

Put “gd” behind the first set of ticks for GDScript code:
```gd
# GDScript
```

2 Likes

I’d like to ask then how you add a different instance of that node in the Nodes Array, if you wouldn’t mind.

what exactly do you want to put in the nodes-array? Do you want to add the meshes?
if yes then you have to do something like this:

var mesh: MeshInstance3D = MeshInstance3D.new()
newNode.add_child(mesh)
mesh.position = Vector3(i, 0, 1)
mesh.mesh = BoxMesh.new()
nodes.append(mesh)

Not the meshes, but the map node itself, each one carrying its own mesh as a child.

  1. Click and drag your MapNode scene file (probably called map_node.tscn if you followed the recommended guidelines) from your FileSystem into your code. Right before letting go, hold down Ctrl and then let go of the mouse button. (That’ll create a const line.)
  2. Add the following code where you had var newNode = $MapNode
var new_node = MAP_NODE.instantiate()
  1. Profit!!!