Hello everybody! Thanks in advance for putting up with my newborn question…
I am working on a Tychoon style terrain generation system. Obviously it uses a lot of dynamic and static arrays often in multidimensional forms and they’re all over the place. The problem I am having, however, is properly defining them and instantiating them in my class constructor.
In one of my classes, TerrainTile, I have the following declarations in the header:
class MGTYCHOON_API TerrainTile
{
public:
TerrainTile(TStaticArray4<uint32> set_vertices = { 0, 0, 0, 0 });
// ...
private:
bool isRendered;
TStaticArray4<uint32> vertices;
static TStaticArray4< TStaticArray2<uint32>> vertex_xy_coords;
// ...
}
The implementation in TerrainTile.cpp looks like this:
// ...
TerrainTile::TerrainTile(TStaticArray4<uint32> set_vertices)
{
isRendered = true;
vertices = set_vertices;
}
// ...
TStaticArray4< TStaticArray2<uint32>> TerrainTile::vertex_xy_coords = {
{ 0, 0 }, // v0
{ 1, 0 }, // v1
{ 1, 1 }, // v2
{ 0, 1 } // v3
};
// ...
The idea is to crate a TerrainTile object with default ‘vertices’ values of [0, 0, 0, 0] (those are elevation values for each vertex). When a class is instantiated, the idea is that it will have a default elevation of “0” for each vertex while the static array ‘vertex_xy_coords’ contains a reference of (x,y) coordinates (relative to the tile origin).
Obviously I am doing this wrong, because the compiler complains that I don’t have appropriate constructors for my TStaticArray objects:
Error 2 error C2512: 'TStaticArray4<uint32>' : no appropriate default constructor available C:\...]\Unreal Projects\MGTychoon\Source\MGTychoon\TerrainTile.cpp 7 1 MGTychoon
In another file, “Terrain.cpp” I have another class that uses this kind of nested TStaticArray but spits a much more confusing error. Here’s the code:
TStaticArray2< TStaticArray3<uint32>> ATerrain::DefineSurfaceTriangles(bool isRotated)
{
if (isRotated)
{
return {
{ 1, 2, 3 },
{ 3, 0, 1 }
};
}
else
{
return {
{ 0, 1, 2 },
{ 2, 3, 1 }
};
}
}
And the error:
Error 5 error C2512: 'TStaticArray3<uint32>' : no appropriate default constructor available C:\...]\Epic Games\4.4\Engine\Source\Runtime\Core\Public\Containers\StaticArray.h 20 1 MGTychoon
This is more frustrating because the error complains about the library ‘StaticArray.h’ (which I have to assume works fine). I had to sift through the compiler output to find the bad code (seen above).
I am obviously new to C++, so I’m a little out of my element. I can’t pick out what I’m doing wrong, and I can’t find enough information on TStaticArrays to figure out how to do this correctly. Before I started working with VisualStudio and the Unreal Engine I wrote a different version of these two classes where I did stuff like this frequently with ‘std::vector’.
Can anybody tell me the correct way to declare a TStaticArray class member and define its initial values in my class constructor?
Again, any help will be greatly appreciated (I also have an Unreal Engine answer hub question open where I will post any answers).