Values from struct are not respected when looping over items

I’m trying to make a Blueprint that that will automatically generate a bunch of Instanced Static Meshes. I want to be able to pick a static mesh as a base and then have a bunch of Instanced Static Meshes spawn on it. Corals on top of a rock, in my case.

I made a struct that takes a Static Mesh and a single float value to be used as scale:

An array of these structs is used to spawn random Instanced Static Meshes. In the Construction Script I do the following:

  • Get the bounding box of the base mesh (rock)
  • Generate a bunch of random points in it
  • For each of the points, start a line trace 200 units above the point, straight down
  • Now I have a point on the rock where I can spawn something

  • Pick a random struct from the list
  • Add an Instanced Static Mesh with the Static Mesh of the struct
  • Place it on the hit location of the ray
  • Set the rotation based on the normal of the surface
  • Set the scale to the value that was set in the struct


Here’s the problem that I’m having: everything works great when I add a single item to the list:

However, when I add more items, the scale value doesn’t seem to be respected. In this example I would expect the yellow corals to always have a scale value of 0.3, while only the blue corals would have a scale value of 1. As you can see in the picture, both the yellow and blue corals get a scale value of either 0.3 or 1.

Does anyone have an idea why it’s picking both scale values for both items?

It’s random on every call. You have 2 calls:


You can create local struct variable to keep both mesh and size.

3 Likes

Hi!

The problem is how you use node “Random”.

Firstly you use it to get random coral and set static mesh:
image

Secondly, you use it to get size of random coral:

Problem is, there are two different calls of “Random” node. So coral you get when setting static mesh and coral you pick to get it’s size are two random corals, not the same one.

I recommend use “Random” node once, get all of it’s values (coral itself and it’s size) into variables and use them.

2 Likes

Yup, you’re right, thanks for pointing that out :+1: This is so very apparent when looking at code, but not so much in Blueprints.