Nested Array crashing

i have a private member of type:
TArray<TArray<class AActor*> > m_2DArray;
or a 2 dimensional array of AActors…
The game works fine but after a minute or so the game just crashes!
on a different lines but all related to access to m_2DArray…notice the same lines worked before and suddenly not.
i do a null check on an actor in the array so its not an actor.
how ever it seems that the member m_2DArray itself is being garbage collected…
I read somewhere that unreal does not support nested arrays but i can not find an official explanation.
Does someone knows it for sure ?

it always crashes about one minute after begin play on one of the access lines to the member…
here is the code:

void UMicroGridComponnet::Fill()

{

FVector l_Location = m_GridLocation;

l_Location.Z += m_CollisionSize.Z*m_AmountOfTiles*2;

l_Location.Y -= m_GridSize.Y - m_GridSize.Y/m_AmountOfTiles;

for (uint8 Columns = 0; Columns < m_AmountOfTiles; ++Columns)

{

    for (uint8 Rows = 0; Rows < m_AmountOfTiles; ++Rows)

    {

        UE_LOG(LogTemp, Warning, TEXT("Create Tile Row: %d  Column: %d"),Rows,Columns);

        m_TileArray[Rows][Columns] = m_TileFactory->CreateRandomColorTileAtLocation(l_Location);

    }

    l_Location.Y += m_GridSize.Y/m_AmountOfTiles*2 ;

}

}

void UMicroGridComponnet::Discard()

{

for (uint8 Columns = 0; Columns < m_AmountOfTiles; ++Columns)

{

    for (uint8 Rows = 0; Rows < m_AmountOfTiles; ++Rows)

    {

        if( m_TileArray[Rows][Columns] != NULL)

        {

            m_TileArray[Rows][Columns]->Destroy(true,true);

            m_TileArray[Rows][Columns] = NULL;

            UE_LOG(LogTemp, Warning, TEXT("Destroy Row: %d  Column: %d"),Rows,Columns);

        }

    }

}

}

I’ll take a look into this later today (hopefully)

It does not support UPROPERTY for a 2D TArray last I recall, that includes for garbage collection. Workaround I heard was to split it into a separate struct e.g.

USTRUCT()
struct ActorArray2D
{
    GENERATED_BODY()

    UPROPERTY()
    TArray<AActor*> Array;    
};

// some other class
UPROPERTY()
TArray<ActorArray2D> Array;

If you aren’t resizing your array before doing any of that you are going out of bounds doing

m_TileArray[Rows][Columns]

As both would be empty.

P.S. Your loop has the wrong variable names, Rows would be the outer loop not the inner.

Hi Dan,
I resized the arrays in the CTOR, other wise it would not work for a minute before.
Regarding the loop it can work both ways as tile factory will adjust the location automatically if space is occupied by another tile.
I write another class for 2D arrays and overload the subscript operator, it seems it the only solution…
Can i write a templated class in unreal?

Thanks

Yes but I don’t think you can for UCLASS which I assume is the issue with having a nested array(?). A quick search gives me this
https://forums.unrealengine.com/development-discussion/c-gameplay-programming/1750331-template-uclass-or-an-other-way

I tried the USTRUCT solution but game crashes when accessing Tarray ,(note it works fine and after a few moments it just crashes while accessing Tarray ).
I Have Null Checks every where…it crashes in lines after i checked for Null already :frowning:
I double checked i don’t go out of bounds when using the subscriptor… i initialized the array correctly.
I removed TArray and it’s not crashing…??? I trying to understand what am i doing wrong…
So I tried using one dimensional array but still crashes(again it works for a few moments but after about a minute it crashes while accessing Tarray).
So here i was sure somehow Tarray is responsible but i investigated a bit further and found some suggestion online… i wrote this in the h file
UPROPERTY()
TArray<TWeakObjectPtr > m_TileArray;
and this to cpp
if( m_TileArray[Rows + m_AmountOfTiles* Columns].IsValid())
the game is still crashing…LOL i fill like i’m fighting unreal instead of making games…

It sounds like the actors are being garbage collected but I’m not sure why that would be the case. If you want I could take a look at it if you are willing to send your project?

I’m just venting a little bit :laughing:, anyway thanks for hearing me complaining all the time :sweat_smile:
Thanks for the offer how can i send you my project? c++ files wont be enough, i have assets needed for the projects.

File > Package Project > Zip up project

How should i send the zip file? Email?

Wherever, here, PM, email.

Privacy & Terms