# Large Hexagonal Grid

Hello !
I Want to make a cell-based strategy game which involves an hexagonal grid.
The size i want would be 64x48 or 72x48 or even dynamic.
I Target Mobiles phones first, but if needed i’ll go for PC.

The cell should be able to be bought by the player or attacked by neighbor cells, they all have their own blueprint (i’ll try bp first then translate that into C++ after)
It’s a bit like Risk to be honest.

Anyone has an idea to manage that system?

http://www-cs-students.stanford.edu/~amitp/game-programming/grids/

And for more clarification, what do you mean by managing that system?

For instance, if the player select a cell and click on Attack, the cell should return its closest neighbor who are on the player’s team and determine if an attack is possible, stuff like that
If the player owns a cell, it should raise a bit over the others and change its color to the player’s one.
Also, they would need to update following the server data (their color should change and maybe their position)

So the generation algorithm should pop actors every K*(X_coordinate) where K is the size of the Hexagon and when the Y coord is 1,3,5… add a X offset of K/2 and a Y offset of Y*(4/3) ?
Should i directly make that in C++ or the Blueprint will be fine for a Mobile game ?
If each cell is an actor, will that be too much for the engine to handle?
(There could be like 3k to 5k cells at the same time)

I Need to have a generation as fast as possible, that’s what scares me on mobile phone, i don’t want a game at 0.5fps that takes 30 minutes to generate the map

From my readings around in the posts from Zeustiak, you should be using instanced static meshes to generate your tiles. From there you can then generate a single collider that encompasses the entire game world. Using the math from Amitp you be able tomap the mouse click coordinate (X, Y, Z) to a coordinate (X,Y) in the hex grid. Then using some math you should be able to map this to an index an in array.

I’m going to example with grids because I have not had much practice with hexes, but I do love grids.

Assume we have a map such as this, where each ‘#’ symbol is a square that is 100x100 units large.

When we click anywhere in the game (assuming the click collides with the collision mesh we added) we get a coordinate (X, Y, Z), for example possibly (138.32, 213.32, 0).

Knowing that we put our grid bottom left corner at (0, 0, 0) and the top right corner at (100, 100, 0) we know we can divide the mouse coordinates to get the X,Y coordinate in the grid.

138.32 / 100 = 1.38
213.32 / 100 = 2.13
Rounding this will give us (1, 2) as our coordinate. (Keep in mind our coordinates for our grid start at 0, 0 and go up to 2,2)

Mapping this to an index in an array is as simple as doing some math:
Something along the lines of X * Width + Z (it all depends on how you choose to index into the array, just keep consistent!)

It sounds like, from Zeustiak’s tinkering, that he was able to spawn something close to 16k cells at one time and still have a functioning system.

You can also buy the grid on the marketplace. it has quite a bit of the functionality you are looking for. it supports both hex and squares

So for performance reason i should instantiate the static mesh instead of an actor then perform my operation in a “MapManager” bp ?
It doesnt work if i use an actor or it would be very slow?

If i need to store each cell in an array, does 2D array exists in ue4?

I’m not sure if you can do an array of arrays in UE4.

It’s not overly difficult to map a 2D coordinate into a single dimensional array, just go through the same function call every time.

I would do a instanced meshes in a single blueprint that would be in charge of your world, once you are done spawning instanced meshes you can just create a collision component on this blueprint and be good to go.

Yep, it’s possible.

Eh, this whole thing gives me headaches, i don’t think i’ll be able to make my game idea… why do i have ideas that can’t be made by me, alone

My game would look a bit like Civilisation (but less complex,easier to understand), is there another approach to make such a game?
here is my “requirements”
-> The player can attack or buy cells to other players
-> The map is updated using the server data (so i need a function that could update the map)
-> The score is calculated by the player-owned cells
-> RTS Style

If the game isnt procedurally generated that wont bother me tbh,

That’s perfectly achieve/reasonable to do even for a beginner in my opinion.

It’s not overly difficult to generate a simple grid through blueprints using instanced meshes. After that it’s also super simple to add a collision component.

You could start by modeling a hexagon the way you want and stick a bunch of them into the scene and see what happens. If it doesn’t perform awfully you might be able to do it without instanced meshes.

I Don’t know,it seems really hard to do this in a way that i can run on mobile phones. Plus the multiplayer part :x

Yeah, if you don’t want to use much or any procedural generation then spawning a simple hex map is pretty easy.

Your problem is manageable. You probably just need to take it in bitesized chunks.

First thing you need to do is create a data structure that holds your tile vectors. If you look in my threads at the vector field generator, as I do the math for the tile vectors I am specifically filling an array in a way that is manageable to my brain. You can also look at the spreadsheet I posted to see how those indexes line up to form the map. Indexes 0 through 9 are the first row, 10 through 19 the second, etc. Index 0 is the lower right hand corner. Index 199 is the upper left corner.

You really only need a 1d array to hold your data structure, but you could make a struct with an array inside, and then make an array of that struct. My data structure has been built entirely on top of a 1d array and I am fine with that. I use 2d arays for other special needs such as holding an array of water bodies, rivers, etc.

In the early days I spawned invisible hex collision volumes so when you clicked the map it would hit one and get the index for the underlying tile. This is an easy solution if you don’t want to get into the math. The only reason I use a single collision sphere now is because I have projected the map into a sphere and can’t use individual collision tiles.

Once you have your data structure and know how to interact with it, how much more complicated you make generation is up to you. But at some point you are going to have to deal with pathfinding and AI, and those topics can be extremely complicated. Even those are manageable though if you take it one step at a time.

It wont need pathfinding or Ai, so i’m ok with that, the hard part will be multiplayer.

Should i write my code in C++ directly or it’s ok with blueprint?
Also,i need an actor for each tile, is it possible or is it too much?

What i need, for instance, is if the player click on a tile,it should display a menu of what to do to the tile : Attack,buy, maybe sell,etc…

If you select a unit and want a visible path laid out between it and wherever your mouse is hovering over you will need at least a little bit of pathfinding. But if you aren’t doing a full AI then it should be relatively simple.

Blueprint can handle everything you need. You could get extra performance out of C++, but if you aren’t doing AI or procedural generation you won’t need it. Though, you did mention RTS. It might depend on how many tiles/cities/buildings/etc are generating resources and other things at any given time, as well whether you want to target super low spec phones or something.

Prototype in blueprint. If you have to move to C++ later it should be easy enough.

You could do an actor for each tile, but I would recommend just using arrays that are aligned to your data structure to hold all the pertinent info. Like at index 0 in the array, it would hold terrain type, unit presence, who controls it, etc, etc. I have a base terrain struct that has 30+ variables stored in it. I fill a map sized array with that struct and I have the data I need for each tile.

For each tile instance you spawn you can spawn an additional invisible hexagon with collision on top of it. When you trace to it, you can get it’s index which will set up to match your data structure. Use that index to get whatever info you need about that specific tile in the grid.

Basically just get real comfortable with arrays. Start with an array of vectors and use them to spawn each tile in the world, say 10x20 to start. Then make other arrays that match that structure. Once you have the arrays filled it is just a matter of accessing the data and using it for whatever.

Thanks i’ll try that,but i need actor for something simple : If the player upgrade a tile, it SM should change and the menu should be different,
So what you advise me to do is :
Make an array of Vectors holding every coordinate of each tile then just make a for loop that will spawn the tiles one by one?

It doesn’t have to be an actor to respawn it. You can destroy an instance and spawn a new one in it’s place. Either way works really. Using actors could have limits though. On a decently powerful PC I can spawn a few hundred thousand actors before the engine starts stuttering, but I don’t know what that limit is for phone/tablet. It depends on how much data is stored in each actor and how strong the device is.

Here is how I start things:

So the first ForLoop is X, and the second two are Y. So if X is 10, then Y is 20. Data structure looks like:

Every other map array just mirrors that data structure, and you can figure out the formula to get the neighbor hexagons using only the indexes.

http://prntscr.com/8y7jwk I DID IT ! In 2D.
I Want to have my camera view a bit like in a game table, like this