How can I snap instanced static meshes to the landscape? (spline tool)

Hello,

I am creating a spline tool, with instanced static meshes, where it is possible to randomize the population of different kinds of meshes from a user set array[1]. To make it easier to use, I want to provide the option to snap the meshes to the landscape.

This works great for when there is only have one type of mesh, but when I have multiple types of meshes, this doesn’t seem to work the same way.

How can I snap different instanced static meshes to the landscape?


I will share some screenshots of my blueprint and the problem. So hopefully someone will understand what I mean. The screenshots are in the correct order for when the function runs/gets called.

Where I ‘see’ if the amount of meshes is only 1 or more:

Adding one instanced static mesh component for each type of mesh in the array “Meshes”:

Adding instances and setting the static mesh. When that’s done, I am calling the “SnapMeshesToLandscape” function:

You can find the “SnapMeshesToLandscape” function here:
Snap Instanced Static Mesh Spline To Landscape posted by viimi | blueprintUE | PasteBin For Unreal Engine

Screenshots of the SnapMeshesToLandscape function

“SnapMeshesToLandscape” function; Getting the array of all instanced static meshes I have added:

“SnapMeshesToLandscape” function; updating the transform from where a line trace hit the landscape:


I skipped the line trace screenshots, but you can find the whole function in the link.


Screenshots of meshes snapping to landscape

How it looks like with one type of mesh (how I want it to work):

How it looks like with multiple meshes:


I almost sure I am doing something wrong when I set the InstancedStaticMeshesAdded array, but I don’t know what.


Note: I will make this to an utility widget blueprint later on, but now I just want the functionality to work.


  1. The “user populated array” I am talking about. it actually a Map right now. ↩︎

The problem is here:

You pass in the Instance Index from an array that is actually made up of multiple Instanced SM components. For example, you have 3 components, each with 1 instance. If you use a For Each like in the screenshot, you’ll only find the first instance of the first component. That’s because the second iteration will look for index 1 in the second component, and index 2 in the third component — but all of them only have one instance, at index 0.

Instead of storing your instances like this in an array, which seems error-prone, I’d suggest using built-in functions like Get Instance Count. For example, something like this:

With this nested For Loop you’ll loop through all of your components and all of their instances, without needing to track the numbers yourself.

After posting this, I have come to the conclusion that the Arrays aren’t set correctly.

This solution doesn’t solve the problem with the arrays being incorrect populated. It may make it work better later, but it is not what I currently need.