Issues with Getting Random Item from a Struct Array

This post is for those struggling to get a random item from a struct array within a function, and as I could not find the solution I may as well post it.

TLDR: Make nodes into a function with an integer as an input, then set a random integer outside of the function.

In my case, I had an array of a custom struct containing two values, both Animation Montage types (I required “open” and “close” montages of an animation, one with Auto Blend Out disabled). This array had 4 items.

I had Made a function to select a random Item and set two variables using the selected struct. The below image is that function, and every time a random struct item was selected, the values would be miss-matched with other values from the other items.


The really strange thing is, that getting an item using an index worked as intended with no incorrect values. But even if a random integer was used to select the item within the function, I had got miss-matched values

The Solution I had found was give the function an int input, get an item from that input, then outside the function get a random int and hook it up to the input.


If Somone could explain to me why this solution worked that would be much appreciated.

P.S. This is my first post so apologies if this is the wrong place for this forum.

random is a pure function so you’ve called it twice for each set and getting diffrent values, solution is to save the output of random in a local variable

The solution you described is a common technique for getting a random item from an array of custom structs. The reason this solution works is because when you use an index to select an item from the array, you are directly accessing the memory location of that item. This means that the values of the struct fields are stored in contiguous memory locations, and accessing one item does not affect the values of other items in the array.

On the other hand, when you use a random integer to select an item from the array, you are not directly accessing the memory location of that item. Instead, you are using the random integer to calculate an offset from the beginning of the array, and then accessing that memory location. This means that the values of the struct fields are not stored in contiguous memory locations, and accessing one item can potentially affect the values of other items in the array.

By giving the function an integer input and getting an item from that input, you are effectively creating a separate memory location for each item in the array. This means that accessing one item does not affect the values of other items in the array, and you can use random integers to select items from the array without worrying about incorrect values.

In summary, the solution you described works because it uses a technique called “indexing” to directly access the memory location of each item in the array, while using a random integer to select an item from the array can potentially affect the values of other items in the array.