# Values from struct are not respected when looping over items

**URL:** <https://forums.unrealengine.com/t/values-from-struct-are-not-respected-when-looping-over-items/1250339>\
**Category:** Programming & Scripting\
**Tags:** Struct, randomize-array, unreal-engine, Blueprint\
**Created:** [August 4, 2023, 7:14am UTC](https://forums.unrealengine.com/t/values-from-struct-are-not-respected-when-looping-over-items/1250339 "2023-08-04T07:14:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![applepiefrost](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@applepiefrost](https://forums.unrealengine.com/u/applepiefrost)\
**Post date:** [August 4, 2023, 7:14am UTC](https://forums.unrealengine.com/t/values-from-struct-are-not-respected-when-looping-over-items/1250339/1 "2023-08-04T07:14:04Z")

</div>

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.

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/f/0/1/f01542693ee2bfbe37b81abbd9b41cfe28b8f9ab.png)

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

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/5/b/8/5b8547a962db17be72ea4dc5e24aee21f861dc7e.png)

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

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/2/9/2294474967f94176ca5ee49a17c67dcc201f5c3d.jpeg)

- 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

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/d/3/0/d303120295738f04598d4425ec2108707ea80708.jpeg)  
 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/f/a/7fab9168aa93f12de0480bc54f5b8383b174f432.jpeg)

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

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/0/2/a/02a241e3b138aae8377e645f4be18f7705b86ffc.jpeg)

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`.

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/6/3/4/63450fa59b00e03f5ed0eda99db34840edc6dbb6.jpeg)

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

---

<div class="post-metadata">

**Author:** ![Shunitzo](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/shunitzo/32/1194547_2.png) [@Shunitzo](https://forums.unrealengine.com/u/Shunitzo)\
**Post date:** [August 4, 2023, 7:25am UTC](https://forums.unrealengine.com/t/values-from-struct-are-not-respected-when-looping-over-items/1250339/2 "2023-08-04T07:25:21Z")

</div>

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

 ![7fab9168aa93f12de0480bc54f5b8383b174f432](https://d3kjluh73b9h9o.cloudfront.net/original/4X/6/8/7/68791641bcc54a61192dda31f4097266a08f7863.jpeg)  
You can create local struct variable to keep both mesh and size.

---

<div class="post-metadata">

**Author:** ![nikita.aksenov](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@nikita.aksenov](https://forums.unrealengine.com/u/nikita.aksenov)\
**Post date:** [August 4, 2023, 7:26am UTC](https://forums.unrealengine.com/t/values-from-struct-are-not-respected-when-looping-over-items/1250339/3 "2023-08-04T07:26:18Z")

</div>

Hi!

The problem is how you use node _“Random”_.

Firstly you use it to get random coral and set static mesh:  
 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/9/4/2943738f90b2fe4e99b5466ff44c3c653d441519.jpeg)

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

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/1/6/21680e75e4e32b9a946125ab64d2fe76b12e65f2.jpeg)

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.

---

<div class="post-metadata">

**Author:** ![applepiefrost](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@applepiefrost](https://forums.unrealengine.com/u/applepiefrost)\
**Post date:** [August 4, 2023, 9:18am UTC](https://forums.unrealengine.com/t/values-from-struct-are-not-respected-when-looping-over-items/1250339/4 "2023-08-04T09:18:58Z")

</div>

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