Download

Making 2d arrays accessible by blueprint

I’m trying to make a core struct to hold my (tile-based) map, thus far it’s just a 2d array with a simple struct:


USTRUCT(BlueprintType)
struct FTileData {

	GENERATED_BODY()

		int32 height;
};

USTRUCT(BlueprintType)
struct FMapStruct{

	GENERATED_BODY()

		UPROPERTY(BlueprintReadWrite)
		TArray<FTileData> heightmap[5][5];
};

It compiles and works fine if I remove the UPROPERTY macro but with it I get a compiler error, “static arrays of containers are not allowed”. Is there a specific workflow for implementing BP-readable multi-dimensional arrays that I’m not aware of?

Still new to unreal engine - but wouldn’t you use TArray< TArray<FTileData> > ?

That is a multi-dimensional array, you could typedef to make it a bit cleaner;

template <typename T>
typedef TArray< TArray<T> > TArray2D;

EDIT: I don’t know if TArray type supports ] lookup, but you could always overload the operator to give you the ]] it seems you are chasing.

Oh, is that how C++ likes to do multidimensional arrays? My purpose in structuring it this way was to create a single table of tile data that you could look up by giving it the cartesian coordinates of that tile… maybe I should be doing something simpler, like a TMap<Vector2,FTileData>? It would allow the same type of lookup without requiring the nested arrays.

@HInoue I don’t think UE4 supports native arrays as uproperties (heightmap[5][5]) You have to use TArray. UProperties also don’t support nested arrays last time I checked

You can have like this


UPROPERTY(EditAnywhere, BlueprintReadWrite)
TArray<FTileData> heightmap;

A single dimension array with an index that you calculate yourself in the BP


index = y * 5 + x

You also need to make the FTileData::height field a uproperty and blueprint readwrite if you want to access

Edit: format

TMap is a bit heavy… if you know the size of the array before-hand (static) you could use a multi-array with TStaticArray

TStaticArray< TStaticArray<FTileData, 5>, 5> array;

You can then access array[0][0].height , given that it is a standard container - I assume it will work with UPROPERTY

EDIT: Seems UPROPERTY doesn’t support nested arrays :frowning:

Yea, that’s the weirdness that’s giving me so much trouble; I could write a subsystem to convert between my data and blueprint, but that’s a ton of extra iteration. Right now I’m thinking of maybe vastly simplifying how it’s stored, maybe by storing an array of FTileRow, where each struct is just an array of tiles, so accessing rowArray[3]->tileList[3] would be the same thing as asking for row 3, column 3, but that seems just as clunky and ill-advised.

Ooh, that is really smart! To heck with the clunky ideas I proposed earlier, I’m going to try that… thank you! I know DA uses grids extensively, is that how you do it?