2D Arrays Please! D:

I have a few features that rely HEAVILY on having 2D arrays (actually in my case I have NESTED 2D arrays) and I’m curious as to why these aren’t in UE4 and if there are any plans to add them. I mean, ND Arrays would be great (Multidimensional/3D/4D ect.) just as a “possibility space” thing but those can all be easily worked around using 2D Arrays. I can’t seem to make 2D arrays from 1D arrays :confused: And even if I figure out how it’s quite odd that they aren’t supported in UE4 nonetheless. They’re kind of a staple imo.

Well, I guess it is like that because the “Arrays” in UE are more like lists than traditional arrays.

This is quite simple. The address conversion is :

I = Row * ColCount + Col

So you basically multiply the row number with the column width (giving you the complete rows), plus the cells of the incomplete column.
(Works only with zero-based lists/arrays).

Example:
Consider an 4x4 “array”, storted in a list of 16 items (0 to 15).

  • The cell at [2,3] has the list index 3 * 4 + 2 = 14
  • The cell at [0,0] has the list index 0 * 4 + 0 = 0
  • The cell at [3,3] has the list index 3 * 4 + 3 = 15

The reverse can be calculated similarly with division and modulo

1 Like

I’m sorry but I found that really confusing D:

Let me just run through one of my super basic 2D arrays (one that I don’t need to use but is really simple to use for this question)

it’s a 2D array named enemy. It’s assigned like this: Enemy[EnemyID,Stat]=Value ; This is used for the HUD.
On spawn each enemy is given a unique identifier to use as their EnemyID ; this is just a number that increments based on spawn order. They then set their max and current health and the rest are set to 0.

So in the HUD I have a for loop that looks like this: (just rough remembering)
{
var MaxHealth = Enemy[0,MaxHealthEnum];
var CurrentHealth = Enemy[0,CurrentHealthEnum];

draw instructions.
}

I tried doing this in 1D arrays by saying:
EnemyID[0] = EnemyID of the 1st spawned enemy
EnemyStat[0]… and then it all fell apart as I realized I couldn’t store multiple variables for that enemy. I tried understanding what you said but it’s all over my head D:

Edit give me a sec just had a revelation lol.

Edit Continued: So it just occurred to me that the EnemyID array can just point to another array with the stats of each enemy. But it sounds really contrived.

GetEnemyStatArray[ID] = 1D array named EnemyStat(somehow insert ID here) that I would have to make and then maintain after assigning it there.

This is breaking me xD it feels so simple but it’s just not connecting.

Edit: Came back to this a few hours later (as in just now) and it makes sense to me now. Completely understand it :slight_smile:

But I’d still like 2D arrays. xD Epic pls.

Options:
1 - Create multiple 1D Arrays. Looks like you’ve already tried that. A bit contrived, yeah, but it works for simple situations.
2 - Use C++. Kind of a pain even if you’re fluent in C++ because you’ll have to write custom getters and setters if you want to use them with Blueprint.
3 - Use an array of structs. This will require a slight redesign of your system, but it’s probably the easiest option, especially if you are using Blueprint only. This way you can keep all of the information together.
4 - Use an array of arrays. A multidimensional array is basically an array of arrays and doing this is possible in blueprint. I feel this can get confusing pretty quickly however.

No problem :slight_smile:

here is what I mean:

Lets say I want to make a 2D array of floats.
I declare just a “normal” float array. Then I create a function “GetElement” that handles the address/index translation.
The only thing we need to supply as well is the number of columns in the array.

in this example, we would access the element at (0/2)

40804aa36e627d91086afbae32b1c2b22e11fcc1.jpeg

The GetElement just gets the item from the array, the index of which is calculated in the GetLinearIndex function.
(You might as well include its code in the GetElement function. That is just a matter of style).

f5835079513d0fd3dd94f22ee1afcc851cb8f509.jpeg

Ant the getLinearIndex function is a simple as can be:

Arrays3.JPG

Hope that clears up some things :smiley:

2D arrays don’t always have the same count on the second dimension, though.

If you wanted to accomplish this, my best idea would be to create a wrapper blueprint class which has an array of your data type, and then create an array of that class’s references.

Alternatively, if you do just have 2 fixed sizes in mind, the method mentioned above of indexing like pixels on an image would work.

Strictly speaking, they do. Otherwise they are not considered arrays.

It would also work in higher dimensions as well. For 3D you would just have to add the third dimension value with the product of the fist two dimension limits.

How come this is still not a thing?

Isn’t multi dimension arrays one of the most basic things in programming languages? How come it hasn’t been translated into Blueprints yet?

ROFL just ROFL…