[COLOR=“#0000FF”]Code for You SaxonRah![/COLOR]
and others!
I made my own USTRUCT system for having a multi-dimensional UPROPERTY() friendly dynamic array version of your maze data structure.
Here’s that code for you and others to enjoy!
.h
USTRUCT()
struct FMazeGridRow
{
GENERATED_USTRUCT_BODY()
UPROPERTY()
TArray<AStaticMeshActor*> Columns;
void AddNewColumn()
{
Columns.Add(NULL);
}
//default properties
FMazeGridRow()
{
}
};
USTRUCT()
struct FMazeGrid
{
GENERATED_USTRUCT_BODY()
UPROPERTY()
TArray<FMazeGridRow> Rows;
void AddNewRow()
{
Rows.Add(FMazeGridRow());
}
void AddUninitialized(const int32 RowCount, const int32 ColCount)
{
//Add Rows
for(int32 v = 0; v < RowCount; v++)
{
AddNewRow();
}
//Add Columns
for(int32 v = 0; v < RowCount; v++)
{
for(int32 b = 0; b < ColCount; b++)
{
Rows[v].AddNewColumn();
}
}
}
void Clear()
{
if(Rows.Num() <= 0) return;
//~~~~~~~~~~~~~~~
//Destroy any walls
const int32 RowTotal = Rows.Num();
const int32 ColTotal = Rows[0].Columns.Num();
for(int32 v = 0; v < RowTotal; v++)
{
for(int32 b = 0; b < ColTotal; b++)
{
if(Rows[v].Columns** && Rows[v].Columns**->IsValidLowLevel() )
{
Rows[v].Columns**->Destroy();
}
}
}
//Empty
for(int32 v = 0; v < Rows.Num(); v++)
{
Rows[v].Columns.Empty();
}
Rows.Empty();
}
//default properties
FMazeGrid()
{
}
};
**Maze Grid Var Declaration for Persistence and Global Access**
```
//Now you have dynamic array benefits and also UPROPERTY()!
UPROPERTY()
FMazeGrid JoyMazeGrid;
```
.Cpp
```
//Init Maze
JoyMazeGrid.Clear();
JoyMazeGrid.AddUninitialized(tileX, tileY);
//Sample usage
//JoyMazeGrid.Rows[x].Columns[y] = SpawnBP<AStaticMeshActor>(GetWorld(), ...
```
Summary
The final result of all of is that you have
-
dynamic array benefits (easily change the maze size and regenerate during runtime)
-
global access to the MazeGrid Actors, even from other classes
-
GC protection from the UPROPERTY() (if not using actors/objects for some reason)
-
And yet the syntax for 2D array using UPROPERTY() and TArray is still clean and clear and precise
JoyMazeGrid.Rows[x].Columns[y]
Enjoy!
Rama
PS:
I also wrote a function to clear the maze / destroy any actors.
You have to pre-initialize the array to use the intuitive 2D array accessor method.