[ad_1]
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It only takes a minute to sign up.
Anybody can ask a question
Anybody can answer
The best answers are voted up and rise to the top
Asked
Viewed
4 times
I’m new to Godot and I tried to write a tool that creates a Posiotion2D array for my chess tiles. By checking InitTilePositions in the inspector, the CreatePositions() method creates 64 Posiotion2D nodes that appear in the scene tree. The problem is that the tilesPositions array stays with empty Posiotion2D nodes even after CreatePositions() was activated. Would love some help!
public class TilesPositions: Node2D
{
#region FIELDS
[Export] public float SpaceBetween { get; set; } = 50;
[Export] public float YCorrection { get; set; }
[Export] public float XCorrection { get; set; }
[Export] public bool InitTilePositions { get; set; }
[Export] Position2D[] tilesPositions = new Position2D[64];
private const int ROWS = 8;
private const int COLUMNS = 8;
#endregion
#region METHODS
#region PUBLIC METHODS & INITIALIZATION
public override void _Process(float delta)
{
if (Engine.EditorHint || GetTree().Paused)
{
if (InitTilePositions)
{
CreatePositions();
InitTilePositions = false;
}
}
}
#endregion
#region PRIVATE METHODS
private void CreatePositions()
{
Vector2 screenCenter = GetViewportRect().Size / 2;
Vector2 boardCenter = screenCenter + new Vector2(SpaceBetween / 2, SpaceBetween / 2);
float offset = SpaceBetween;
for (int row = 0; row < ROWS; row++)
{
for (int col = 0; col < COLUMNS; col++)
{
int index = row * COLUMNS + col;
Vector2 gridPosition = new(col - COLUMNS / 2, (ROWS - 1 - row) - ROWS / 2);
Vector2 tilePosition = boardCenter + gridPosition * offset;
// Reuse or create Position2D nodes
var tilePositionNode = tilesPositions[index];
if (tilePositionNode == null || tilePositionNode.GetParent() != this)
{
GD.Print($"Creating new Position2D node at index");
tilePositionNode = new Position2D
{
Name = $"TilePosition X:{row} Y:{col}"
};
AddChild(tilePositionNode);
tilePositionNode.Owner = GetTree().EditedSceneRoot;
tilesPositions[index] = tilePositionNode;
}
GD.Print($"Setting position of node at index {index} to {tilePosition}");
tilePositionNode.Position = new Vector2(tilePosition.x + XCorrection, tilePosition.y + YCorrection);
}
}
}
#endregion
#endregion
}
\$\endgroup\$
default
[ad_2]