November 5, 2015 by jonmillymiles
I’ve encountered a problem, I can make the random rooms, and I can make corridors – they’re just a random room that is a single square wide – but I’ve tried to get them to join up and I cant.
At present, everything is spawned as a separate object… and I can’t get them to read their coordinates from each other properly. I could read the x and y world coordinates from the room object and get the width and length and multiply them out but testing in this nature for each square to see how long a corridor should be is going to be time consuming.
What I need to do is to go back to basics and make a map. Way back in the mists of time most games used a two dimensional (2D) array (X and Y) to hold data about how the world was constructed. This was used to generate the landscape using a tile set and could be referenced by the game at any time to see if the player location would trigger an event, such as death.
Why will this help me? Well by having a common reference for my dungeon I will be able to map effectively from one place to another as all of the data, usually integers, will be held in a single entity. Therefore there will be less data looked up and passed between objects. There will be less referencing required for simple functions and most importantly the build process should be easier and, hopefully, quicker.
Games that used this are classics such as Super Mario Bros., Sonic, and Turrican for those who are fortunate enough to remember the C64. They are easily identifiable as they have repetitive graphic elements throughout the scenery. What is striking is that this reuse of assets, essentially modular terrain, is what is featured throughout most modern games in one way or another. Modular terrain was my topic of choice last year for one of my modules and is discussed in my blog post Blueprints of Life.
There is a problem with Unreal Engine 4 though, it does not have a 2D array – it only has a single dimension array, essentially just a list of elements and so the first thing that I need to do is to work out how to make them.
After doing some research I decided to have a go at making my own.
There are two ways of doing this, and they both form a grid. The first is to create a new structure, called a “Row” which is simply an array of integers. We can then create a new variable, the “Map” which holds an array of “Row” therefore giving us this 2D referencing system. Something like this:
In Unreal Engine 4 the setup looks like this and the logic below is how to read an element from the 2D array. You may also notice that I am encapsulating all of my code into functions… this makes it so much easier to work with and, when a function works correctly, it is far less likely to develop bugs.
But before we can read from the map we need to generate the content.
To do that we need to generate Y rows with X number of elements and populate each of these with a value. I am going to use a random number generator to place a ‘1’ in the element on a 1 in 5 chance.
In practice we would not populate the tiles on creation, we would just generate the array; but we also need to test the process.
So… what I need to do now is to cycle through the map (X and Y), read each value and if it is ‘1’ then we will make a tile…
So here goes…
… and it didn’t!!
Logically it should work – I even found the Resize node to resize the arrays immediately without having to use the Add node repeatedly. But after two hours of testing the problem seems to be consistent throughout the piece – I cannot access the items in the sub-arrays (X). I cannot write to them at least, or alter them at all.
I need a plan B, luckily I said earlier that there are two ways of doing this and the second involves far more maths 🙂