Hey !
I would like to know if there’s any two dimensional array in Blueprint scripting. I looked all over the official documentation and I only found that Array is one dimension only.
Thanks in advance !
Hey !
I would like to know if there’s any two dimensional array in Blueprint scripting. I looked all over the official documentation and I only found that Array is one dimension only.
Thanks in advance !
UE4 reflection system does not support array in array (even in C++, regardless of fact that C++ allows to do so), so it’s impossible to make multi dimensional array. But there very simple workarounds which makes single dimension array to work like two dimensional array, by playing allocation numbers. If one dimension has static size, you can multiply that size in allocator by number of y postions, like this:
x+(x_size*y)
Ofcorse you need to watch out to x not being higher or same then x_size or else it will read or set other y row. You can make functions to make it a lot cleaner and simpler
That’s weird I thought of doing it in C++ but if you say that it’s the same problem than in Blueprint then I’ll go for the one dimension array. Thank you for the tips
Do you know if this will be added in the future ?
Just as small heads up.
You can use an array of structs containing another array of another variable.
So two dimensional is very much possible. I’m not quite sure how it handles more dimensions than that though as I’ve never tested it but I can’t imagine that it’d just break.
Cheers
Erasio solution seems better, i forgot you could do that ;]
i think he got result already, but for people who may find this, solution is really simple:
here’s screenshots:
filling such 2D array
get 2D array elements like [index1][index2] and printing values
see how printing look in game
what i didn’t try:
set value in 2d array at already exist index (actually we didn’t need it for our purpose, because generation of heigh map doesn’t require changing after first fill), but i will try use “set array item” to see is i can set value of already exist 2d array element like [row1][column2]
It reminds me of hack used back in UnrealScript but yes, it’s viable
There’s a much simpler way to do this with Vector 2D array variables.
Make a Vector 2D array variable and then in a nested for loop, write the i and j values. This way you can even make it a function so that all you have to do is input an x and y and then it will spit out a 2d array
https://i.imgur.com/nX59Ocz.png – function creation
https://i.imgur.com/UasK6Ss.png – function calling and then reading the outputs
https://i.imgur.com/mlVFuuH.png – outputs
I dug this thread up from the grave cause i was looking for an answer, but it gave me an idea. Not sure why you need a 2D array so idk if this would work but I just thought of a way to address a linear array in a way that acts 2D. Have one int for x position one for y position, one for X length and one for Y length. Linear array’s total length should be (x length)*(Y length). Say your position is X3 and Y1, the theoretical grid size is 4 (0-3) on X and 4 (0-3) on Y. And assume the linear array starts at zero on the “lower left” corner. And the highest index represents the “top right” corner, with the array going “horizontally” along the theoretical grid. And advancing up a row when it reaches the right edge. So in this way to get an item at X column, Y row take ((PosY * SizeX) + PosX) to get the index in the array. In the above example (X3, Y1) it’d give you 7, for the index. Or the right most position on the second row from the bottom.
I also dug this thread up from the grave to just say that it’s 2022 now… why does each user have to re-invent the wheel to manipulate 2D arrays in Unreal? Why aren’t there already dozens of convenience functions built-in for this? (Niagara apparently has one or two, but that’s it from what I could see.)
Easy way to sort strings in an array for BP would be great too, basic functions.
Why on Earth would there be no support for 2D arrays in a game engine?? This makes zero sense to me. I don’t want to use some obscure hacks just to use a basic programming technique.
Super… SUPER odd. There has to be some reasoning behind this… What is the “recommended” way for devs to handle the need for this? It’s a rather fundamental concept in programming so I refuse to believe there isn’t some canonical way to achieve this that doesn’t feel like a hack (the nested structs).
Wow, I am having the same problem and it is really strange that don’t have a good and clean way to do this. I am working in a dungeon rooms with the position indexes access. The solution of a Array of structs and the struct with array do the job, but I am having issues with updating data in the structs, it seems that I have to build all struct again and then I can set the struct in the root Array.
Other solution that I am thinking is use a Map with the key the union of the two index. Something like this:
key: 0-0 value: “A”
key: 0-1 value: “B”
.
.
key: 1-0 value: “A”
Is this a cool solution? Not really.
to update a struct in an array you need to grab a Ref not a Copy
Umm, its not that you cant nest arrays in unreal, you can do whatever you want through structs, its that they dont allow to nest their Templated arrays, like TArray and TMap and other Unreal templated arrays.
I just wanted to create a two dimensional array to store an id and strings, like this:
TMap<FName,TArray<FString>> Test;
But the HeaderTool doesnt allow that, so I will probably use a different solution completely or I will make a struct, like:
USTRUCT(BlueprintType)
struct FStringList
{
GENERATED_BODY()
UPROPERTY(EditAnywhere, BlueprintReadOnly)
TArray<FString> Items;
};
//then the variable:
TMap<FName,FStringList> Test;