# Replicating a 2D dynamic array

**URL:** <https://forums.unrealengine.com/t/replicating-a-2d-dynamic-array/22174>\
**Category:** C++\
**Tags:** unreal-engine\
**Created:** [February 28, 2015, 5:42pm UTC](https://forums.unrealengine.com/t/replicating-a-2d-dynamic-array/22174 "2015-02-28T17:42:23Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![envenger](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@envenger](https://forums.unrealengine.com/u/envenger)\
**Post date:** [February 28, 2015, 5:42pm UTC](https://forums.unrealengine.com/t/replicating-a-2d-dynamic-array/22174/1 "2015-02-28T17:42:23Z")

</div>

Hello everyone…

I have a 2d dynamic array of structures.

What is the best way to replicate it? I went through this [https://docs.unrealengine.com/latest/INT/Gameplay/Networking/Replication/Implementation/DynamicArrays/index.html#implementation](https://docs.unrealengine.com/latest/INT/Gameplay/Networking/Replication/Implementation/DynamicArrays/index.html#implementation) but this doesn’t say anything specifically about 2D arrays.

Another thing I am thinking about is using a Client RPC function call to update the value of each slot.

The data the structure contains is really small(3-4 ints at max). So any one knows how does 2D dynamic arrays and replication work together?

---

<div class="post-metadata">

**Author:** ![Rama](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/rama/32/882413_2.png) [@Rama](https://forums.unrealengine.com/u/Rama)\
**Post date:** [March 1, 2015, 1:07pm UTC](https://forums.unrealengine.com/t/replicating-a-2d-dynamic-array/22174/2 "2015-03-01T13:07:36Z")

</div>

How to Make BP Friendly 2D Arrays That Replicate

Dear Envenger,

**I have a full code sample for 2D BP-friendly / replicating arrays here:**

> **[SaxonRahs Tutorial Thread - Random Maze Generation & Solving](https://forums.unrealengine.com/showthread.php?47-SaxonRahs-Tutorial-Thread-Random-Maze-Generation-amp-Solving&p=27139&viewfull=1#post27139)**
>
> Welcome to my tutorial thread. This maze generator is dedicated to Rama; as in I’m providing it for free. Since he’s helped me so much. \<3 Thanks mate. Here’s some videos of what were going to do. Random Maze...

````auto

**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**

```auto

//And you can replicate it like this
UPROPERTY(Replicated,RepRetry)
FYourDataArray Your2DDataArray;

```

````auto

**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.

Rama

---

<div class="post-metadata">

**Author:** ![envenger](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@envenger](https://forums.unrealengine.com/u/envenger)\
**Post date:** [March 1, 2015, 5:17pm UTC](https://forums.unrealengine.com/t/replicating-a-2d-dynamic-array/22174/3 "2015-03-01T17:17:46Z")

</div>

Hello Rama, thanks for the reply 🙂

I have done something exactly similar.

```auto

USTRUCT(BlueprintType)
struct FStorageUnit
{
	GENERATED_USTRUCT_BODY()

	UPROPERTY()
	FItemStruct ItemStruct;

	FStorageUnit()
	{

	}

	FStorageUnit(const FItemStruct NewItemStruct)
	{
		ItemStruct = NewItemStruct;
	}
};

USTRUCT(BlueprintType)
struct FStorageRow
{
	GENERATED_USTRUCT_BODY()

	UPROPERTY()
	TArray<FStorageUnit> Columns;

	void AddNewColumn()
	{
		Columns.Add(FStorageUnit());
	}
	//default properties
	FStorageRow(){}
};

USTRUCT(BlueprintType)
struct FStorageArray
{
	GENERATED_USTRUCT_BODY()

        UPROPERTY()
	TArray<FStorageRow> Rows;

	void AddNewRow()
	{
		Rows.Add(FStorageRow());
	}

	void AddUninitialized(const int8 RowCount, const int8 ColCount)
	{
		//Add Rows
		for (int32 i = 0; i < RowCount; i++)
		{
			AddNewRow();
		}
		//Add Columns
		for (int8 i = 0; i < RowCount; i++)
		{
			for (int8 j = 0; j < ColCount; j++)
			{
				Rows*.AddNewColumn();
			}
		}
	}
	FStorageArray()
	{}
};

/** A simple 2d int32 structure */
USTRUCT()
struct FStorageLoc
{
	GENERATED_USTRUCT_BODY()

	UPROPERTY()
	int8 RowNum;

	UPROPERTY()
	int8 ColNum;

	FStorageLoc()
	{
		RowNum = -1;
		ColNum = -1;
	}

	FStorageLoc(int8 Row, int8 Col)
	{
		RowNum = Row;
		ColNum = Col;
	}
};

```

But I think when replicating a structure, the whole structure is replicated. So wont it be a bit less efficient network wise?

---

<div class="post-metadata">

**Author:** ![SaxonRah](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@SaxonRah](https://forums.unrealengine.com/u/SaxonRah)\
**Post date:** [March 1, 2015, 5:22pm UTC](https://forums.unrealengine.com/t/replicating-a-2d-dynamic-array/22174/4 "2015-03-01T17:22:11Z")

</div>

> [@envenger;232674](#):
>
> Hello Rama, thanks for the reply 🙂
> 
> I have done something exactly similar.
> 
> But I think when replicating a structure, the whole structure is replicated. So wont it be a bit less efficient network wise?

I’m not entirely sure, but I think yes. though since it’s only int8’s you’ll probably be fine ? Idk exactly. I’d also like to know for sure.

---

<div class="post-metadata">

**Author:** ![envenger](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@envenger](https://forums.unrealengine.com/u/envenger)\
**Post date:** [March 1, 2015, 5:50pm UTC](https://forums.unrealengine.com/t/replicating-a-2d-dynamic-array/22174/5 "2015-03-01T17:50:07Z")

</div>

Well not int8s… The whole Array will be replicated each time a element in the array is changed. That’s something i want to avoid.

---

<div class="post-metadata">

**Author:** ![SaxonRah](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@SaxonRah](https://forums.unrealengine.com/u/SaxonRah)\
**Post date:** [March 2, 2015, 7:46pm UTC](https://forums.unrealengine.com/t/replicating-a-2d-dynamic-array/22174/6 "2015-03-02T19:46:06Z")

</div>

> [@envenger;232688](#):
>
> Well not int8s… The whole Array will be replicated each time a element in the array is changed. That’s something i want to avoid.

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.

---

<div class="post-metadata">

**Author:** ![Inverness](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Inverness](https://forums.unrealengine.com/u/Inverness)\
**Post date:** [March 2, 2015, 8:16pm UTC](https://forums.unrealengine.com/t/replicating-a-2d-dynamic-array/22174/7 "2015-03-02T20:16:55Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_33e74471](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_33e74471](https://forums.unrealengine.com/u/anonymous_user_33e74471)\
**Post date:** [July 16, 2015, 4:49pm UTC](https://forums.unrealengine.com/t/replicating-a-2d-dynamic-array/22174/8 "2015-07-16T16:49:31Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![envenger](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@envenger](https://forums.unrealengine.com/u/envenger)\
**Post date:** [July 16, 2015, 5:54pm UTC](https://forums.unrealengine.com/t/replicating-a-2d-dynamic-array/22174/9 "2015-07-16T17:54:32Z")

</div>

> [@mrooney;333944](#):
>
> 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.

My array size doesn’t change. Only the elements present inside it change so it quite good…

This helped me to optimize my TArray code a bit.

> **[Optimizing TArray Usage for Performance](https://www.unrealengine.com/de/blog/optimizing-tarray-usage-for-performance)**
>
> In Unreal Engine, TArray is a dynamically sized array of typed elements. TArrays are very convenient to the programmer, and they are used \*a lot\* in our codebase. However there can be some subtle performance issues that can arise, and...
