Repeat a piece of code

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

I want to instance a random number of nodes that is stored in a var, but I don’t know how to do that. I think I have to use a loop keyword to do that.

:bust_in_silhouette: Reply From: kidscancode

It sounds like you are unfamiliar with basic programming structures. The construction you’re looking for is called a for loop.

I’m not sure what you mean by “stored in a var”. To store multiple instances, you’d need an array. However, these are just going to be references to your nodes, which need to be added to the tree if you’re going to use them. At that point, you may not need a separate array to reference them.

If you’re new to programming as well as Godot, I highly recommend you start with a beginner programming tutorial. Most of the Godot docs assume you have at least a basic familiarity with general programming concepts. I recommend Python, as there are a great deal of good beginner videos/tutorials, and its syntax is very close to GDScript so it will seem very familiar to you.

At the very least, you should go through the “Step-by-step” Godot tutorial, which you can find here: