I have a full code sample for 2D BP-friendly / replicating arrays here:
**New Code Sample Just For You**
To create a UPROPERTY() compatible 2D array you should actually make a struct
Then you can also use RepRetry
Also make sure to make **each member a UPROPERTY(**) in the ustruct or it wont get replicated
Full code sample for you, that I just wrote:
```
//You dont necessarily need this Data Unit struct if your fundamental data type is simple like float or FString.
USTRUCT(BlueprintType)
struct FYourDataUnit
{
GENERATED_USTRUCT_BODY()
//Whatever your core data of the array is, make sure you use UPROPERTY()
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="2D Array")
FVector Location;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="2D Array")
FLinearColor FlowerColor;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="2D Array")
uint8 NumPetals;
FYourDataUnit()
{
//always set defaults for ustruct simple data types.
NumPetals = 5;
Location = FVector::ZeroVector;
FlowerColor = FLinearColor::Red;
}
};
USTRUCT(BlueprintType)
struct FYourDataRow
{
GENERATED_USTRUCT_BODY()
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="2D Array")
TArray<FYourDataUnit> Cols;
//or could be using a simple data type instead of a Data Unit Struct
//UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="2D Array")
//TArray<FString> Cols;
FYourDataRow(){}
};
USTRUCT(BlueprintType)
struct FYourDataArray
{
GENERATED_USTRUCT_BODY()
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="2D Array")
TArray<FYourDataRow> Rows;
FYourDataArray(){}
};
```
Replication
//And you can replicate it like this
UPROPERTY(Replicated,RepRetry)
FYourDataArray Your2DDataArray;
**Access**
```
//then you can access a member like this
Your2DDataArray.Rows[x].Cols[y];
```
Benefits
While this might look a bit cumbersome it results in an entirely BP-friendly 2D array that can also be replicated over the network.
I was told by Epic to use this multi-ustruct arrangement for a BP-friendly 2D array when I asked about 2D arrays during the UE4 Beta.
It’s worked great for me
My test case was large 2D Maze generation and performance was great.
I was just saying your data type is small. Meaning the amount of elements in row or col, is limited. It would be allot different replicating a 2d array of say int64 in rol or col.
You might need to do your own change management implemented with replicated functions.
That is, setting the array members through functions that signal that the member needs to also be replicated. You could either do this with every single change, or batch the changes and send several at once.
Hiya, I realize this is old, but I wanted to add something that is SUPER IMPORTANT that I just ran into.
If you are going to replicate an array you should be VERY VERY careful about how you remove your items. RemoveAtSwap or RemoveAllSwap should always be used in preference to RemoveAt and RemoveAll. If you have a large array where you will be removing elements with any frequency, it’s super important that you are not letting the array dynamically rearrange itself because you will kill your performance in a networked game. One thing you can do simply is to just let your array changes be sent by RPC rather than by replication (probably the best way), but just worth being aware of.