# Dynamic 2D Array using TArray

**URL:** <https://forums.unrealengine.com/t/dynamic-2d-array-using-tarray/382330>\
**Category:** C++\
**Tags:** tarray, unreal-engine, CPP, question\
**Created:** [January 19, 2017, 8:18am UTC](https://forums.unrealengine.com/t/dynamic-2d-array-using-tarray/382330 "2017-01-19T08:18:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dlkulp](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@dlkulp](https://forums.unrealengine.com/u/dlkulp)\
**Post date:** [January 19, 2017, 8:18am UTC](https://forums.unrealengine.com/t/dynamic-2d-array-using-tarray/382330/1 "2017-01-19T08:18:40Z")

</div>

Maybe I’m just completely missing the point here and there’s a better way to do this. I’m trying to get a pretty simple dynamically sized 2D array of custom types. [This](https://answers.unrealengine.com/questions/380253/2d-array-in-c.html) is so close, but the response is not dynamic and I haven’t really found anything else that gets at this specific problem. In normal C++ I’d do something like

```
vector<vector<myClass>> myVect;

```

and that would allow me to do what I want. My searching indicates that `TArray` is the most similar to a `vector` but I keep getting errors when I try to do something like this:

```
TArray<TArray<myClass>> myArray;

```

Since this isn’t working, is there a “best practice” in Unreal that I’m not familiar with, or is there something else I’m missing? I also find it weird that Visual Studio isn’t giving me any red squigglies, it’s only when I compile that I’m made aware of the problem.

In case it helps, this is the actual error log:

```
CompilerResultsLog:Error: Error d:\program files\epic games\4.14\engine\source\runtime\core\public\Containers/Array.h(1745) : error C2248: 'ATile::ATile': cannot access private member declared in class 'ATile'
CompilerResultsLog:Error: Error c:\users\user\documents\unreal projects\sneakysnake\source\sneakysnake\public\Tile.h(11) : note: see declaration of 'ATile::ATile'
CompilerResultsLog:Error: Error c:\users\user\documents\unreal projects\sneakysnake\source\sneakysnake\public\Tile.h(9) : note: see declaration of 'ATile'
CompilerResultsLog:Error: Error d:\program files\epic games\4.14\engine\source\runtime\core\public\Containers/Array.h(1771) : note: see reference to function template instantiation 'int32 TArray<ATile,FDefaultAllocator>::Emplace<ATile>(ATile &&)' being compiled
CompilerResultsLog:Error: Error d:\program files\epic games\4.14\engine\source\runtime\core\public\Containers/Array.h(1771) : note: see reference to function template instantiation 'int32 TArray<ATile,FDefaultAllocator>::Emplace<ATile>(ATile &&)' being compiled
CompilerResultsLog:Error: Error d:\program files\epic games\4.14\engine\source\runtime\core\public\Containers/Array.h(1771) : note: while compiling class template member function 'int32 TArray<ATile,FDefaultAllocator>::Add(ATile &&)'
CompilerResultsLog:Error: Error C:\Users\user\Documents\Unreal Projects\SneakySnake\Source\SneakySnake\Private\WorldGen.cpp(24) : note: see reference to function template instantiation 'int32 TArray<ATile,FDefaultAllocator>::Add(ATile &&)' being compiled
CompilerResultsLog:Error: Error C:\Users\user\Documents\Unreal Projects\SneakySnake\Source\SneakySnake\Private\WorldGen.cpp(21) : note: see reference to class template instantiation 'TArray<ATile,FDefaultAllocator>' being compiled

```

all the rest of the codz can be found [here](http://pastebin.com/R508TUZ8).

tl;dr: I want a dynamic 2d array and `TArray` is giving me lots of errors. What’s the correct way to do this?

---

<div class="post-metadata">

**Author:** ![project.gheist](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@project.gheist](https://forums.unrealengine.com/u/project.gheist)\
**Post date:** [January 19, 2017, 10:06am UTC](https://forums.unrealengine.com/t/dynamic-2d-array-using-tarray/382330/2 "2017-01-19T10:06:03Z")

</div>

Hi dlkulp,

I believe I saw this issue before and the solution was to place the secondary `TArray` in a `USTRUCT` like the following;

```
USTRUCT()
struct FEncapsule
{
TArray<MyClass> Foo;
}

TArray<FEncapsule> MyArray;

```

To let it behave more like a default array, you can override the [] operator for the `FEncapsule` struct.

Hope this helps. Cheers,

---

<div class="post-metadata">

**Author:** ![Steve\_Robb](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Steve\_Robb](https://forums.unrealengine.com/u/Steve_Robb)\
**Post date:** [January 19, 2017, 10:28am UTC](https://forums.unrealengine.com/t/dynamic-2d-array-using-tarray/382330/3 "2017-01-19T10:28:32Z")

</div>

I think you want:

```
TArray<TArray<ATile*>> levelMap;

```

You can’t have containers of UObject values, only UObject pointers. However, as mentioned by project.gheist, you probably also want to wrap the inner array in a USTRUCT() and then make levelMap a UPROPERTY(), as this will allow garbage collection to work correctly for your map.

Steve

---

<div class="post-metadata">

**Author:** ![Juice-Tin](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/juice-tin/32/1008648_2.png) [@Juice-Tin](https://forums.unrealengine.com/u/Juice-Tin)\
**Post date:** [January 22, 2018, 9:21pm UTC](https://forums.unrealengine.com/t/dynamic-2d-array-using-tarray/382330/4 "2018-01-22T21:21:34Z")

</div>

Your override was a great idea! I just wanted to add this for other peoples reference:

```
USTRUCT()
struct FRune2DArray{
	GENERATED_BODY()
public:

	TArray<FRune> Ar;

	FRune operator[] (int32 i) {
		return Ar[i];
	}

	void Add(FRune rune) {
		Ar.Add(rune);
	}
};

```

I needed a 2D array for my FRune structs, so by overriding [] and adding an Add function (and any other you want) you can perfectly simulate a real 2D Tarray. Here is the result:

```
	TArray<FRune2DArray> runes;

	runes.Add(FRune2DArray());
	runes.Add(FRune2DArray());

	runes[1].Add(FRune());
	
	runes[1][0]; //contains the FRune I just added
```

---

<div class="post-metadata">

**Author:** ![kungfu\_k3nny04](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@kungfu\_k3nny04](https://forums.unrealengine.com/u/kungfu_k3nny04)\
**Post date:** [March 9, 2022, 6:46am UTC](https://forums.unrealengine.com/t/dynamic-2d-array-using-tarray/382330/5 "2022-03-09T06:46:20Z")

</div>

how would i do this if i was making a 2d for an actor class. it is for an inventory system
