Array of structs with arrays in them

Hi, is there anyone who would be able to give me an example of how you would setup an array of structs e.g 3 structs that each contain an array, and have an array of those structs. Thanks in advance. I’m essentially just trying to create a multidimensional array using structs.

Just like any other array of structs???



USTRUCT()
struct MyArrayStruct
{
        GENERATE_BODY()
public:

       UPROPERTY()
       TArray ColumnZero;

       UPROPERTY()
       TArray ColumnOne;

       UPROPERTY()
       TArray ColumnTwo;

       MyArrayStruct()
       {
       }
};


Hi I found this on my quest to creating a 2D array, i have yet to try it but you might want to give it a shot.

Joe, you have shown me a struct with some arrays in it, rather than an array of structs that have arrays in them. I’m trying to create a 3d array exposable to blueprint and the way appears to be using an array of structs with arrays in them. Unless I’m losing the plot (which is certainly a possibility lol) your example is just a 2d array.

Sbrouillard, thanks, but it"s a 3d array I’m after.

If you put an array(of whatever you want your 3d array to be) into a struct(because an array can’t be made of arrays in unreal) let’s call it Struct1 then do an array of Struct1 you get a 2d array but if you put that array of struct into a struct let’s call it Struct2 then you should get a 3d array. It just comes down to iterating the steps a number of time equal to the number of dimensions you need. I’m just theorizing here, i haven’t actually tried it.

Sorry, I guess I misread what you were looking for… It’s just embedding arrays. Here is an example of a dynamic 3 dimensional array.




USTRUCT(BlueprintType)
struct FZDimension
{
    GENERATED_BODY()

protected:

    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Example)
    TArray<int32> Depth;

public:
    FZDimension()
    {
    }

    int32 GetValue(int32 Z)
    {
        if (Depth.IsValidIndex(Z))
        {
            return Depth[Z];
        }

        return 0;
    }

    void SetValue(int32 Z, int32 Value)
    {
        if (!Depth.IsValidIndex(Z))
        {
            int32 Needed = (Z - Depth.Num()) + 1;
            Depth.AddZeroed(Needed);
        }
        Depth[Z] = Value;
    }

};

USTRUCT(BlueprintType)
struct FYDimension
{
    GENERATED_BODY()

protected:

    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Example)
    TArray<FZDimension> Rows;

public:
    FYDimension() {}

    int32 GetValue(int32 Y, int32 Z)
    {
        if (Rows.IsValidIndex(Y))
        {
            return Rows[Y].GetValue(Z);
        }

        return 0;
    }

    void SetValue(int32 Y, int32 Z, int32 Value)
    {
        if (!Rows.IsValidIndex(Y))
        {
            int32 Needed = (Y - Rows.Num()) + 1;
            Rows.AddZeroed(Needed);
        }

        Rows[Y].SetValue(Z, Value);
    }

};

USTRUCT(BlueprintType)
struct FThreeDimensionalArray
{
    GENERATED_BODY()

protected:

    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Example)
    TArray<FYDimension> Columns;

public:
    FThreeDimensionalArray() {}

    UFUNCTION(BlueprintCallable)
    int32 GetValue(int32 X, int32 Y, int32 Z)
    {
        if (Columns.IsValidIndex(X))
        {
            return Columns[X].GetValue(Y,Z);
        }

        return 0;
    }

    UFUNCTION(BlueprintCallable)
    void SetValue(int32 X, int32 Y, int32 Z, int32 Value)
    {
        if (!Columns.IsValidIndex(X))
        {
            int32 Needed = (X - Columns.Num()) + 1;
            Columns.AddZeroed(Needed);
        }
        Columns[X].SetValue(Y, Z, Value);
    }
};




Some things of note. It’s ugly to edit this type of data in the editor. So if you really need a structure like this you’re going to want to look at how to build custom property editor. Also note that AddZeroed would need to be fixed up for most types. Finally, if your dimensions are fixed, just use a buffer and calculate the position. Equally bad to change values in a property editor but much nicer code wise.

– Updated to add BlueprintCallable to the two accessors.

Thanks Joe, that’s awesome, just what I need :slight_smile:

Thanks Sbrouillard, that’s definitely some food for thought.

Unforunately I get an error in VS 2017 with that code "Ustructs cannot contain Ufunctions. I do also get E0077 “this declaration has no storage class or type specifier” for Generated_body, although I get the impression from looking elsewhere that this can be ignored. The first error though seems pretty terminal

Ah yea I forgot about that. Trouble with writing code in a forum not VS :). Those two accessors can’t be UFUNCTIONs. You’ll have to provide Blueprint support some other way, like having the object that uses the array provide BP access.

Eek! arrays in Ue4 are clearly a lot more complicated than I thought they would be :frowning: