Download

Locking a tile to a hexagonal grid, and performance question.

So I have some ideas I am playing around with and basically building a hexagonal grid I will have to raycast to each tile and identify what kind of tile it is. I am using it as an actor base class and spawning it as a physicsless actor. This appears to be having performance issues. When I get to around 300 or so I get major frame dips. This may seem like a lot but when all those tiles are all tiled next to each other it only covers a small area. I did a bit of testing and something odd stuck out: I made a stack of them 300 tall and I looked up at the stack I had a decent framerate, but if I looked at the base of the stack I dipped to 20fps.

So my two questions.
What is a good, performance friendly way, to have something that is built with tiles?
How can I snap an object to a hexagonal grid?

Blueprint preferred but I am experienced in C++ if needed.

I would do a search for Zuestiak’s map projects, he fully documents many issues he has run into while generating hex grids. Even had a dev following and talking to him often as well.

Hello,
As Hakabane said Zeustiak will be really helpfull. He did some feedback in this thread which may be usefull to you too :Help with Hex grid generator - Blueprint Visual Scripting - Unreal Engine Forums

Yeah read through my first map generator thread. I was having performance issues when I first started but Ian Shadden helped out quite a bit.

Basically you will want to use InstancedStaticMeshes for anything over a few hundred hexes. Also, I would suggest storing all the info you need in properly indexed arrays. I only trace when I need to click on a tile in game. Everything else is referenced by the tile’s index and by the type of data I am trying to pull from it(Elevation, Precipitation, etc).

So I tried using instanced meshes and I am still seeing a big performance decrease. Not only that but when I hit escape to stop playing the editor crashes. Am I doing this right?

Just to make implementation the same, try these steps:

Make the default mesh for the AddInstanceStaticMeshComponent node the mesh you want to use.

Unhook the set mesh node.

Select Manual Attachment on the AddInstanceStaticMeshComponent node.

Unhook the Hex Actor Variable.

Plug the Return Value into a Variable to store the InstancedStatic mesh in a variable.

Go into the mesh, the material, etc, and ensure they are usable with Instanced Meshes in their details panels.

Maybe disable collision just to ensure that isn’t causing problems somehow.

Double check to make sure you are only getting 1 mesh per click.

That is all I can think of right now, let me know if anything works.

Heya Mojobojo,
You only need to create a single Instanced Static Mesh component per mesh you want to use (I think that’s what Zeustiak gets you to under his instructions). In your example BP there, you are creating an Instanced Static Mesh component every time you click (or potentially “on tick” while you hold the mouse button) which is basically just as bad as creating a bunch of Static Mesh components (possibly worse as it looks like it’s crashing on you).

Also, in ~40 mins (at 3:30 EST) I’ll be on the Training Stream talking about some of this.

Long time no see Ian! I will have to catch up on that stream. :slight_smile:

Thanks for that training stream, it was very helpful. I have instanced meshes working and understand them now. Is there a way I can keep the texture that is applied to the mesh? For some reason when another instance is added the material that is applied to the mesh gets replaced with the world grid material.

EDIT: I seem to have figured it out. I have to add an instance in the editor first and let the material compile, then it works.

I’m also getting [Line: 833] #Failed to destroy physics state (InstancedStaticMeshComponent /Game/Maps/UED as soon at I have any collisions other than NoCollision set on AddInstancedMeshComponent.
Every time I hit play my editor crashes.