How to find randomly selected integer?

I’m trying to select a specific forced feedback based on which montage is played. How do I find which number was chosen in the random array? The index of the random node will also be random and does not always match the number of the selected montage.

Set an int variable from here

image

and then just use it later.

I tried that and it still gives me a random integer, doesn’t match the integer of the array :confused:

Can you show the code?


I tried setting it before the Play Anim Montage as well, just grabs a random integer from each instead of the same random integer.

Without understanding the rest of your blueprint, it is hard to say why this isn’t working. Is it possible that this Function is called quickly in sequence, causing the random value to change before the Force Feedback section?

One suggestion that might work: Don’t save the array index; instead, save the element to a variable then use that to Find the index of that element in the array immediately before the Select when use that index to do the Select?

That could be, it is playing a montage based off a trace.
I tried that as well and no luck.

Sounds like it probably isn’t going to work then? That’s okay, I can just do one force feedback for all of them - not as fancy as a unique one for each animation but nothing I can’t live without haha

It’s something that Epic added, which may be the source of the problem. But either way it looks like something else is interfering with it. I’ll do some digging and post if I find a solution!

I’ve used that node, it works well :slight_smile: It’s a bit like the way you can now say ‘get actor of class’ instead of that ‘all actors + zero from array’ cludge.

The code above looks fine, it must be something else going on…? ( @unvoicedbee )

All dependant pure nodes are re-evaluated for each exec node.

So simply put :


This works because here the RANDOM node is dependent twice on the same exec node (print string), so RANDOM is executed only once and its results are cached during this specific evaluation.

However :


This doesn’t work because RANDOM is executed once for the first Print String exec, and another time for the second Print String exec (giving a different value).

Solution : as a rule of thumb, avoid wiring multiple outputs (or one output multiple times) of pure nodes if they are not consistent or have side effects. Immediately store the appropriate result in a variable and use that variable. By appropriate I mean, if you don’t need the index it’s perfectly fine to use item directly. If you do need the index, store the index and get the item back from index.

6 Likes

That explains it very well. I still can’t get it to store the index correctly in my project but I was able to get it working in a fresh one, must be something with my code before this that is interfering with it. Thank you!