Hex Grid Generation Sanity Check

Hi, I’m new to Unreal Engine 4. I’ve only been playing around in it for a few days and there are still a multitude of things I don’t know anything about. I’ve worked with some other game editors before, and have worked with things like XNA and managed DirectX when that was still a thing. I’m a software developer by trade, but I develop business software, not games.

For the project I’d like to work on, I need the levels to be constructed of three-dimensional hex tiles. So you basically have a tiled hex grid, but you need to be able to stack hexes to add height and depth to the maps. Here is an example of what I mean.

142ad51fe6c33c3494a63b07b9a6254138c197d5.jpeg

I’ve been thinking of ways this might possibly be accomplished. However, being new to UE4, I’m still not certain what is or isn’t possible, so I’d like a bit of a sanity check on my current thoughts on making levels. My idea of the workflow for a level is currently something like this:

  1. Create a text file describing each hex space on the map, its height, and other important features about it such as terrain type. The first iteration of this won’t allow for negative space in maps, so no overhangs or arches. Therefore a 20x20 map would be described by 400 text entries in some kind of format like this [3,F(orest)]. I might at some point make a tool to write or read these files for easier creation.

  2. Create a blueprint that, in its constructor, uses C++ to read a text file containing a level description as above. It will use the entries in the text file to create a three-dimensional array containing information about each hex space within the level. So you would just drop this blueprint into the world and it would build out the entire hex map.

  3. On the first pass it will just read in information from the level specification file and store it off to an in-memory array.

  4. The second pass over the array will add additional data about each hex in the level, such as whether it needs to be drawn (is it surrounded on all sides by other hexes?), whether it is a walkable tile (is it the top tile in a stack?), and so forth. I did have one thought though. It would be useful to be able to refer to each face of a 3d hex tile separately in code. For example, depending on placement, some tiles might only have two or even one face needing to actually be rendered. So I’m wondering if it’s possible to refer to discrete elements of a single mesh file in code (each face would have a reference), or if my hex tiles need to be a blueprint with 8 different components, for the eight sides and top and bottom. What I’m trying to get at is I need a way to not render extraneous hex tile faces. Also some hexes will need different materials for the top and sides. For example, you might want a hex with grass on top but rock sides for a cliff.

  5. The third pass over the array would create the actual blueprint objects in the game world representing the hex tiles that need to have a visual presence. Hex tiles that are surrounded on all sides by other tiles would of course not be created in the game world at all. I’m hopeful that the end result of this would be that you would have a sort of “shell” made up of hex tile objects with the minimum amount of geometry you actually need to render the level.

  6. Once this is done I could use the other tools in the editor to add things like buildings, enemies, and all the usual stuff.

I would be very grateful if people would chime in with whether this is possible as I have described it, and whether there might be better ways to go about it.

Hello,
your first step would be to see what may have been link in this forum, for example : Map Generator 2.0 - Please Critique! - Blueprint Visual Scripting - Unreal Engine Forums to have an idea of what you can do.

then have a better look on how to do instanced meshes and yes, with unreal engine 4 you can do this kind of procedural level creation.

Actually I’ve already had a look through the forums, and in fact I’m quite familiar with the thread you linked. Mostly what I’m curious about is, can UE4 do this the way I intend to do it, and if so is there a way to improve on the way I intend to do it?

Yes, with instanced stuff you can and with blueprint only. Will you maybe at a point of your creation have to change your original idea ? maybe ^^ But it is doable and not so hard (i am not so great in blueprint and i did some random levels with random props based on thoses tutorials and others like saxonrah’s maze.

Okay, so I got a flat hex grid of arbitrary size generating. I’m currently using InstancedStaticMesh to draw out the field, and each hex will have an invisible blueprint object on top of it for things like collision and keeping information about the underlying tile.

Looking ahead a bit, one of the concerns I have is with being able to have a 3d hex tile have different materials for its sides, bottom, and top. In order to accomplish this, am I going to have to make “fake hexes” instead of using a single mesh? Ie have each tile be visually made up of 8 separate staticmesh faces? This also goes back to being able to remove pieces of hex tiles from the scene that are not visible to the player. For example, if two tiles are adjoining, the faces where they meet need not actually have geometry.

So, hex tile with removable/separately mappable faces. Is there a better way to do this?

Have you seen this thread too ? Generatig a medium-sized hexagonal grid with complex objects - Performance problems - Blueprint Visual Scripting - Unreal Engine Forums : It have a lot of good learns in it.

About your visual maybe you can set an instanced materiel on your instanced mesh which will change considering wich hex is spawned.

I’ve seen that thread, but I haven’t read it thoroughly yet. I’ll do so now!

It is pretty much possible without any troubles - check out this article Driving Gameplay with Data from Excel - Unreal Engine

Look 1st.

Well, just create struct array and fill it with data from your table, no problems here

You simply use information from your array to render hexagons correctly and assign new information to variables when it need to be changed, nothing serious again.

Well, how to construct your 3d hexagons it’s up to you! I ended up using 1 static mesh component and just a lot of meshes with different walls models which I swap when needed. It’s more lightweighted than having a lot of different components. But for building of course I used additional components.

Just setup a behaviour using coordinates from array

Basically - read data from tables and assign data to array of structs, then build 3d grid using transform data from array and customize appearence of created hexagons with additional data from array. Then build layer of logic(building and etc) on top of created grid.