How to properly loop back to the same node?

Hello !

I created a small function returning an X and Y floats that are in my “arena”.

My arena is a circle with a given radius so I basically just generate two random floats thus giving a point in the encompassing square. This is where the problem is. I then check with Pythagoras theorem whether the point is within the circle or not. If it is not, I want to regenerate new coordinates.

This is what the function looks like :

I cannot loop back the False pin of the branch node back into the branch node input to generate new coordinates. I found that if I put any node in the middle, it does work. In this example I put “Draw Debug Point”. This may work properly but it doesn’t look to me that it would be the way it was intended to be implemented in blueprint. How would you fix this ?

Thanks a lot !

You can use a reroute node, but it looks like you’re trying to get stuck in an infinite loop.

Use the ForLoop macro with a set number of attempts to prevent getting stuck in this function. Either use a Loop with a break, or just return from the function to automatically break out of the flow when you’ve satisfied the condition.

WARNING:
You need to save the random value into a local variable, because it changes every single time a pin pulls from it. Currently once your condition is satisfied, you return values that will re-pull the random number. This is the nature of Pure nodes in BP, they get re-executed on every value request.

Alternate Solution:
A safer way to do this would be to randomly generate an angle and distance from the center of your circle, so you don’t have to loop at all! Use that trigonometry to find the X,Y.

The sqrt of (0-1) is to account for clustering, as suggested by PartlyAtomic.

float angle = Random(0.0, 2.0 * PI)
float distance = Radius * sqrt(Random(0.0, 1.0))
float x = distance * cosine(angle)
float y = distance * sine(angle)
return x, y

WARNING: You need to save the random value into a local variable, because it changes every single time a pin pulls from it. Currently once your condition is satisfied, you return values that will re-pull the random number. This is the nature of Pure nodes in BP, they get re-executed on every value request.

HUGE thanks for this. I had already seen odd behaviours I couldn’t explain and now I know why that was !

I’ll use your suggested implementation which is, I admit, much safer. I don’t know performance wise if it is better but realistically I probably won’t call this function enough for optimization to matter.

Thanks a lot for the various tips ! :smiley: