I’m having an issue with my Blueprint in Unreal Engine 5.5 and could use some help:
In my project, I’m generating a building with a variable number of floors. To do this, I’m using the Random Integer in Range node, with the Min value set to 10 and the Max value set to 100. The result is used as the Last Index in a For Loop to create the floors.
Problem:
Although the Random Integer in Range node outputs correct values between 10 and 100 (confirmed via Print String), the For Loop never executes more than 40 iterations. Even when I print the current index inside the loop, it always stops at 40, even if the Last Index is set to 100.
Steps I’ve tried:
Tested Random Integer in Range Node: Works perfectly and outputs values up to 100 as expected.
Checked Last Index before the For Loop: The value is correctly passed from the Random Integer in Range node.
Manually Set Last Index: When I manually set the Last Index to 100, the loop runs as expected and creates 100 floors.
Printed Index Inside the Loop: The loop only iterates up to 40, even when the Last Index is greater.
Additional Details:
The First Index of the loop is set to 1, and the Last Index comes directly from the Random Integer in Range node.
There are no visible clamps, branches, or conditions limiting the value of the Last Index.
I’m completely stumped as to why the loop seems to cap at 40 iterations, despite the Last Index being correctly set. Has anyone encountered a similar issue or have an idea of what could be causing this?
I tested what happens when the maximum random value is below 40. For example:
If I set the Random Integer in Range to Min = 1 and Max = 10, I get random values between 1 and 10 as expected, and the loop runs up to those values.
However, when the Max is set to 100, the loop still never goes beyond 40, even though the random value is correctly generated (confirmed with Print String).
I’ve attached the following screenshots:
[Screenshot of the Blueprint]
[Output Log showing the Print String output directly from the Random Integer in Range]
3.[Output Log showing the Print String output from the For Each Loop]
Additionally, I tried using an Array containing numbers from 1 to 100 instead of the Random Integer in Range node. Unfortunately, the For Loop still caps at 40 iterations, even when accessing values beyond 40 from the array.
Interestingly, if I manually input the Last Index as a fixed value (e.g., 100), it works perfectly and creates the correct number of iterations.
Do not connect pure functions to the loop macros, as they will get called on every loop
That means that the random int will get called on every loop, so on every loop a new max will be generated
You should cache the result of the random to a local var and use that as the max in your loop