• News

• Industries

• Learning & Support

• Community

• Marketplace

# n x n array

Hi

I was wondering if it’s possible to make an array with both rows and columns. Now i imagine arrays as a single row of entries, whereas i can imagine a lot of uses for an array with row and column entries.

Let’s for simplicity say i want to create a 3x3 tiled map of some sort, and the middle room contains an up/down hall. Then i could loop thorugh the array, and it would find the room at (x,y)=(2,2) and place connecting walls at (x,y-1) = (2,1) and (x,y+1) = (2,3). Or how do people go about that kind of business?

Kind regards

Use a struct array with 2 variables.

Narg

Yep I’ve come up with a simple equation that lets you do this, here’s an equation that finds the index from the x and y:

index = x + (y*highest height)

I use this in my Game for a grid of blocks with 1 array (see the link in my signature)

Here is a picture representation.

If you wanted to have a 3D tiled map, 3x3x3, its almost the same equation:

*index = x + (y*highest height) + ((Layer - 1) NumBlocksPerLayer)

So in this 3x3x3 situation:

*index = x+ (y*3) +(( Layer - 1) 9)

Note that the 1st layer needs to be 0.

This is all really quite easy, maybe I complicated it. If you have any questions just ask, I will be happy to answer.

It’s called a 2D array, or 3D if you are going another step on each element too. In blueprints alone with an array UE4 doesn’t support those. You will want to create a struct with array objects for it’s fields to achieve this. I populate mine through dataset’s and then you can load them in as regular array variables and add or remove as necessary during runtime (To the local array variable, not the dataset)

Jamendxman3: It was also something like that i had in mind, when converting from 1D to 2D, however a bit cumbersome to do if you change the grid, and want to look the entry above and below, up.

ZoltanJr and Narg: A struct seems like the best alternative to do this with, where you effectively can make a (x,y) structure

Thank you for the replies

Well let me resentence is in a short manner:

when you add items to the array do this:

insert item at index: y + (x*gridHeight)

and when you want to retrieve items, you do the same thing.
To flip the grid to go from right to left or vice versa, you just swap the x and the y in the equation. Those are the only variables you’d need to change.