Loop until boolean is true?

Hello! I’m trying to advance on an AI asset I bought. I found an issue where the AI would flee, even when (with my own code saying) it was supposed to approach the player. So I have made a check/a boolean named “Allowed to flee”.

The variable is set to false when the AI is ready to approach the player, and set to true when the AI is allowed to flee. The AI is supposed to be able to flee if the character suddenly spooks them, by for example standing up/or moving.

The issue I have is that the event that checks when an AI can flee, is by checking if the player is within a certain radius. Inside that code (a function), I start the function off with a branch and the “Allowed to Flee” variable attached to it. On true, its allowed to flee. On false, its not supposed to do anything. This code however, only runs once, when the player is within the radius, and never again.

So I’d like to try and loop it until the branch runs true. But I’ve tried to use “whileloop” and it always gives an infinite loop. Or I’m not using it properly.

Therefore, I wonder how I can loop it? I cant use the normal delay node because its within a collapsed graph inside another function, and I don’t think hooking it up to event tick would be very performant?

I don’t know how to make the loop not infinite, and still check regularly if the player has moved or not so I can set it to flee if the player has.

Try to use Timer.

My Products

3 Likes

Hi! Is there not another way to loop check?

I can’t use custom events since its inside a function and I’d rather not call the loop on something like begin play so it loops from the start.

Sorry if I misunderstood! I’m still quite new, but I appreciate the help! :smiley:

You already said :smiley:
“Loop until boolean is true?”
image

1 Like

Hi! I also said I’ve tried it and it only ends up giving me an infinite loop :slight_smile:

The timer cannot cause an infinite loop. Could you show your graph?

Hi!

Sorry for not being clear. I meant to reply to the comment above.

Using While loop is what causes the infinite loop.

However I cant find out how to use your method since the set up is in a function and therefore I cant use custom events.

This is my “While Loop”:

image

But I seem to have find a solution using the timer event either way. I just called the custom event on a branch of the “allowed to flee” and on the false pin. And then copied the code in the functions at the “true” in the timer event

Then my friend, if you got an infinite loop there is something wrong :smiley:

Just as a help for your further project, as to WHY you got your infinite loop error :slight_smile:

You have to keep in mind, that all your code will run in one tick, or per frame - even a loop. That means, a loop will be looping a hundred or a thousand times per tick (if its triggered by EventTick, then that also translates to every frame, since that one fires every frame), until the condition to complete the loop is met.
And that is hard with a while loop, that waits for your bool to change ^.^
Because to get out of your “while loop” and to avoid getting an infinite loop, you would have to set your condition to true >somewhere in your loop code< which you probably didn´t do :slight_smile:

Your “Allowed to Flee” variable is most likely set somewhere else outside of your loop, so it would never be changed during your loop, and that means, your loop has to run over and over again, until you trigger the “infinite loop” warning. How often was that by default, looping for 100000 or 1000000 times per tick?

What you wanted, is a code, that just checks once every tick, if the condition is met, and then wait for the next tick/frame, and check again. That is what timers do best - they trigger the linked event every time, if you set them to loop.
A timer set to f.e. 1 second and looping, will fire once every second, therefore allow you to check your code every second, until you clear, stop or pause that timer.