You'll probably want to store the tiles in a two dimensional list or similar and then use a for loop to initialize the tiles:
public class Map : Spatial
{
private PackedScene _tile = GD.Load<PackedScene>("res://Tile.tscn");
private int _numRow = 10;
private int _numCol = 10;
# the two dimensional list
# you may need to import system.collections
private List<List<Node>> tiles = new List<List<Node>>();
public override void _Ready()
{
for (int x = 0; x < _numRow; x++)
{
List<Node> row = new List<Node>();
for (int y = 0; y < _numCol; y++)
{
Node clone = _tile.Instance();
AddChild(clone);
row.Append(clone);
}
tiles.Append(row);
}
# then to access each element (for example)
GD.Print(tiles[2][2].name);
}
}
If you need to access the Spatial properties of the node, you will want to cast the node. The documentation shows a few different ways to do this, but I generally use the as
keyword so I can check for null
values:
Spatial example = tiles[2][2] as Spatial;
if (example != null):
GD.Print(example.global_transform.origin)