Infinite Loop Detected

I want to check if the randomly picked object is already inside the input array and if it is not in the array I want to use the random object.

It’s super easy to implement this logic in any programming language but I couldn’t create the same logic within UE. Here’s my visual nodes.

How can I get a random object from the components array, and check if it is inside the other array and loop if it is already inside the other array?

from what is shown I only see one random value pull, and it happens before the artificial while loop you created, meaning if the random number was index 5 it would just test index 5 infinitely hence the “Infinite Loop”

I would suggest putting it into an actual loop maybe a For loop with break (right-click and type “for break” though it should be one of the options before you hit the “space”) because there is a chance we go through the entire array without finding a suitable candidate, so we want some way to make sure we can get out, and that we don’t stay in there infinity.

create a temporary count variable of probably an int (because we are not stepping through the array in order we will need to count our limiter somehow) then in the loop body connect up to the cast, and have your circle back go into the loop. then into the break hook up the False of your current if test.

I would also suggest instead of trying to cast each thing that is randomly spit out to make it so that the Array is either already of that type as the input variable, or iterate over the array and remove the indexes are not of that type beforehand, because if say we have 20 things, only index 2, 4, 7 are of the type we actually want then it will be more consistent to remove all the other index before we actually start doing compares, and having to deal with casting on each attempted pull.

another optimization would be to have a temporary reference to the type and after getting it assign it to the temp, and access that temp for the needed steps because the Array->Random() may not always be trustworthy for retention and performance.

Does the random method not being called with each “loop” in there? I’m not very familiar with the visual scripting logic, as far as I know, the random value is pulled from the array, then it is checked if it is inside the “usedArray”. If it is, it goes back and picks a new random element, and the same goes on until the picked element is not inside the usedArray. I know, normally I’d use a simple loop to do this, but again visual scripting gets a bit confusing.

The cast is not the main issue as I can optimize the function later on, this is a prototype for now.

And for random method, I’ve heard it is unreliable, and I might use another method to pick random element, which is not a big problem again.

Main problem is, the pseudo loop not working as intended :smiley:

I would say just have something happen before your cast so that the attempt to pull a random index has the actual potential to be different, and I would strongly suggest some kind of limiter besides just the compare. because it would be better for it to fail gracefully rather then Computer random getting you “randomly” into an infinite loop…

it can also depend on how many things might be in this array on how likely this is to be actually random, computer random does not always have full equal distribution, so for example if the array have 4 elements that are reasonable candidates, and 3 of them would loop again. it should be a 25% chance given equal distribution, but that in Computer random 25% might as well be 5%.

so by just iterating a counter before the pull, and then in you conditional check if that counter is greater then say ?10? break out into some absolute fail-state that still continues execution. where a function call is done synchronously and often on the main thread anything like this could pause the GameLoop and Tick()s that are naturally happening.

1 Like

Alright I’ll add some execution point beforehand so it gets called again and see if it’s the reason.

I’m not THAT concerned about this particular function’s efficiency, because it will be executed one time only during level generation. Of course, optimization should be important.

What I don’t understand is, during the first execution of this code, the “usedArray” is empty, and the randomly picked element is a component of an existing actor. So it picks 1 of 3 components let’s say. It checks if this 1 component has already been used, and it is not, obviously. So it should continue in the function. However, it does not.

Anyways, I should do my testing tomorrow, and see if anything changes, I’m exhausted right now, after meddling with a lot of functions. Thanks for your time as well!

As I mentioned yesterday, I was too exhausted, now I replaced it with a simple loop with a small counter limit, and everything goes smoothly. I also changed the random to integer random so it is a bit more reliable. Thanks for the time as well!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.