InstancedStaticMesh Tutorial

So lately I have been looking into a tiled terrain. Been through a lot of trial and error due to there not being a lot of documentation on this so I figured I would share what I figured out so you guys don’t run into the same issues I have.

Part 1

  1. Create a c++ project with basic code and with the starter content.
  2. Go to File->Add Code To Project
  3. Make the class inherit the Actor class. We are going to be placing this in the editor.
  4. Let the editor add the code and load into visual studio. It will probably ask to stop the editor, make sure you do that.

This is what you are going to want your code to look like.

H File

CPP File

When you get your code in, compile it, when it succeeds go into the editor and you should be able to find your actor class in the class explorer and place it in the editor.

In the panel on the right, select your actor and look in the details panel. You should see the mesh instances variable you made in c++.

Expand it and you should see a tab that says “Static Mesh”, expand that tab and select the static mesh you want to instance. I am just choosing a cube.

If all goes right, if you hit play, a mesh will appear in front of you.

Part 2

So you have your mesh, but its plain and has an icky checkered pattern.

In the starter content folder, find a material you want to apply to your mesh and open it to edit it. In the bottom left there is the “Details” panel.
Inside that panel find the “Usage” checkboxes and check “Use With Instanced Static Meshes”. Remember to save the material.

Next if you go to the “Rendering” tab, and hit the + sign next to “Materials” you can choose that material you just modified. I chose just M_Ground_Grass

Now if you click play, you should have a cube with a material. Mine is all grassy.

2 Likes

Hi ,

Thanks for this! I’m actually just about to start doing something like this in the game I’m currently putting together. I do have a question though - I’m wanting to go about spawning platforms for the player to walk on. How do I spawn them so that they all connect to each other (end to end) so the player can continuously walk on them?

Regards,

You will probably want to take a look at this. They do procedural room generation in blueprint.

Thanks for the link! Unfortunately, I’d prefer to do it in C++…

As I’m not a C++ coder, this kind of stuff is invaluable!

Thanks so much!

As some food for thought, would it be possible to do this but with a static mesh that’s generated in a Blueprint? For example say I had a blueprint where I can set a custom roof and a custom building, Would I be able to modify your C++ code in some way that would allow me to use it as one static mesh (Even though it’s a Blueprint?)

Thanks for this tutorial!

How would this be done with only C++?

It is being done in C++. He’s setting the static mesh in the blueprint but you can easily do it in C++, it’s just SetStaticMesh(…)

Steps are :

Create HISM component
Set the static mesh to use
Add instances

For

RootComponent = MeshInstances;

I get…

E0513 a value of type “UInstancedStaticMeshComponent *” cannot be assigned to an entity of type “USceneComponent *”
and
C2440 ‘=’: cannot convert from ‘UInstancedStaticMeshComponent *’ to ‘USceneComponent *’

and for…

MeshInstances->AddInstance(InstanceTransform);

I get…

E0393 pointer to incomplete class type is not allowed
and
C2027 use of undefined type ‘UInstancedStaticMeshComponent’

Essentially it can’t assign because it can’t resolve UStaticMeshComponent type.

Adding…

#include “Components/StaticMeshComponent.h”

Solved the issue for me.