Download

Procedurally add random Mesh in construction script

Hello, everyone!
I have created a blueprint that adds meshes according to the position of a vector variable, very similar to the one in this tutorial:

I made some little alterations so that only the x-value matters at the moment and the meshes also get added correctly when the x-value is negative.
aaf83bfb7863d569b1edc72ad67a6aaa569d1260.jpeg
This works perfectly fine until here.
e044d39746d62b5574b42af8a7662b84647ca8ba.jpeg

However, I wanted to add random meshes instead of just one.
So I changed the loop body to go into a branch first. A random float gets created and if it is <=0.3 one mesh gets added, if it is larger than 0.3 but not larger than 0.6 a different mesh is added instead. If that is also not the case another mesh is added.
e0f071d588d6049d55a4b4eabb86971177bcff08.jpeg

However, this causes a very strange bug.
Some of the meshes are missing. No matter how far I move the EndPoint on the x-axis, the problem always seems to appear only in the first couple of meshes. However, they are not always the same ones.
58e7047b2a6e0e3bb0a2b0e78165cab4b0ef58b0.jpeg
efbcf616abaa6085d1931d33740350f0430e72b9.jpeg
2b6711996d3cf3bbfd16051fc4f0a2ae824e3c98.jpeg

I enabled Simulate Physics in the Add StaticMesh nodes to see if there are some overlapping meshes and it seems like the missing ones are always stacked on the very last one.
I might have some logic mistake in my blueprint but I just can’t figure it out. It’s just so weird that the problem only occures within the first few meshes. I even set the x-value of the EndPoint to 1 Million, adding 1000 Meshes. Still, there only were missing ones among the first few. When the x-value of the EndPoint is rather low there are 0-2 meshes missing but when the value is quite high they seem to be always exactly 2.

Also, my method of choosing between random meshes seems quite dirty. Is there a better way? For example using arrays or simply a node that randomly fires one of its outputs?
And one more question: Does the random float gets randomized both times it is used in my blueprint or only once per loop? Either way, I tried avoiding that by creating a variable that gets set to a random float every time the loop starts and then use the variable instead of the random float in the picture above. But the results were exactly the same.

I looked but i didn’t see anything out of the ordinary of why this would be happening.

Did you make sure the second and third meshes were set ?

EDIT - This is how I do it normally. This is how I do it randomly.

Yes, all the meshes are set correctly.

not sure if you seen but i added some pictures :slight_smile:

Try to set the add static mesh all to the same mesh, to make sure it isn’t a problem there ether being a bad mesh, or having the mesh offset causing a problem with relative location.

Where you multiply the end point by 1,0,0 then get the vector length, you can just use the X value where you break and use the X value instead. As the vector length is only going to be the x value.

Sorry for the late reply, I had lectures today.
I finally got it to work, based on SaxonRah’s blueprint.
Thank you so much!
I don’t quite know what it was but I think the problem had something to do with setting the mesh directly in the ‘Add StatMeshComponent’ node’s details instead of setting it separately with a ‘Set StaticMesh’ node.
Also, I just realised I used three almost identical looking meshes in my post yesterday which made it quite hard to understand. Sorry about that.

I’ll try to get it work on the y-axis as well. I’ll post the whole blueprint here once I get it to work.