Just starting out. My goal is to have a game board that automatically generates a multitude of cells as child nodes under it. This is working well.
The issue is that these child nodes called “Square.cs” are not showing up despite having a sprite attached, being in the scene and having CanvasItem set to visible.
Here is a photo of the scene view showing that the squares have no textures attached but the node type does:
You need to instantiate the whole scene, instead of just newing the script class. The latter will just create a new single node with the script attached.
Thanks for replying so quickly! That code did help me get the square to appear but now it is only letting me create one before it stops. Even though its in a nested “for” loop. Also I can no longer access the childs position like above.
public void PopulateBoard()
{
Vector2 boardPosition = GlobalPosition;
int xOffset = 0;
int yOffset = 0;
GD.Print("Starting position: " + boardPosition);
Sprite2D sprite = new Sprite2D();
Texture2D texture = (Texture2D)GD.Load("res://Assets/TileSheet.png");
sprite.Texture = texture;
sprite.RegionEnabled = true;
for (int x = 0; x < gameBoardHeight; x++)
{
for (int y = 0; y < gameBoardWidth; y++)
{
xOffset = x * squareSize;
yOffset = y * squareSize;
var scene = ResourceLoader.Load<PackedScene>("res://Square.tscn").Instantiate();
AddChild(scene);
gameBoard[x, y] = (Square)scene;
}
}
ViewChildren();
I did see an error. The constructor for the square had a parameter for Vector2 position. I removed this constructor overload and it created all of them. Now, how can I access the position of the children?