Download

implement special dynamic 2D TArray

Hi all,

In my Tetris game i need a 2D TArray, whose each element contains a struct that contains 2 1D TArrays

BallManager.h :


#include "Cube.h"
#include "GameFramework/Pawn.h"
#include "CubesManager.generated.h"

struct CubeStruct
{
	TArray<ACube*> Cube;
	TArray<int32> IsACube;
};

UCLASS()
class TETRIS6_API ACubesManager : public APawn
{
	GENERATED_BODY()

public:
	
        ...

	//2D TArray of the static cubes positions and their Cube pointers
	TArray<CubeStruct> StaticCubesMatrix;

};

In CubesManager.cpp constructor, i tried to implement my 2D TArray, but i failed


#include "Tetris6.h"
#include "CubesManager.h"


ACubesManager::ACubesManager()
{
	...

	for (int32 i = 1; i <= NumCubesPerRow; i++) {
		for (int32 j = 1; j <= NumCubesPerColumn; j++) {
			//some tests earlier failed
		}
	}
}

How to do so please ?

Hello guitou80,

as I understand your code, you probably want to use the 2-dimensional array to address every square on the screen? This is totally possible!

Let us start with the header file. You want to be making StaticCubesMatrix a UPROPERTY so that you can view and/or edit the property right in the editor and allow blueprints to read and even access it if you want to. To do so simply add UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = “Tetris”) directly above the array declaration; this will make your array show up and editable in the editor under the category specified and allow blueprints to read and modify its contents. If you only want the array to be visible in the editor replace EditAnywhere with VisibleAnywhere, and if you only want blueprints to read, replace BlueprintReadWrite with BlueprintReadOnly.

Something to node is that when you make your TArray a UPROPERTY its template argument must be an UCLASS, an USTRUCT, an UENUM, a special type, like FVector, or a primitive type such as a int32, float, or bool. So you must make your CubeStruct a USTRUCT. To do this, simply add USTRUCT(BlueprintType) directly above the declaration “struct CubeStruct”; ‘BlueprintType’ makes the struct a valid type for blueprints to reference. Also, you must rename the struct to “FCubeStruct” because of the naming convention used in Unreal software. Also, add GENERATED_BODY() right after the the first “{” parenthesis; this is required by the Unreal Header Tool.

Now, as we want a 2-dimensional array we might be tempted to write something like this: TArray<TArray<FCubeStruct>> StaticCubesMatrix. This seems fine but the Unreal Header Tool will complain about this stating that a tarray of tarrays is not allowed. Luckly, we can ‘trick’ the header tool by defining another USTRUCT which will look like this:


USTRUCT(BlueprintType)
struct FArrayPlaceholder
{
GENERATED_BODY()

    TArray<FCubeStruct> Placeholder;
};

With this type, we declare StaticCubesMatrix as this: TArray StaticCubesMatrix. You just need to adjust your iteration code slightly now: in C++ arrays start at index 0 and the maximum index is its size - 1. So to iterate your matrix, you would use this code:



for(size_t i = 0; i < StaticCubesMatrix.Num(); ++i)
{
    for(size_t j = 0; j < StaticCubesMatrix*.Placeholder.Num(); ++j)
    {
        /* Init a FCubeStruct named Data here */
        StaticCubesMatrix*.Placeholder[j] = Data;
    }
}

I hope I could help.

Thanks for your reply, but it didn’t work.

Instead i simply used 2D static array whose each element is a struct that doesn’t contain any 1D array