[CLOSED] Infinite Loop error when using AI MoveTo

Hi all. I am developing a game where you race against some npc’s. The npc walks/runs in a straight line and turns back and does it all over. I have a “round” limiter, but it’s the same if I remove it. The code is a bit messy and long, but you can figure it out. Can you help in removing the error?

The error messages


The code itself. I tried to fit it all in a readable manner



Hi there, the infinite loop error you’re encountering in your NPC movement code can occur when the engine detects a potential infinite loop in your blueprint logic. This is usually caused by a loop that doesn’t have a proper exit condition or runs too many times in a single frame.

See if you can review the following if not you’ll need to start debugging:

1. Inspect the While Loop Condition

Ensure the condition for your While Loop node eventually becomes false. From your first image, it looks like the loop condition is Current Round <= Max Rounds. If Current Round is not incremented properly or Max Rounds is too high, this can cause an infinite loop.

2. Add Safety Checks

Consider adding a safety mechanism to break the loop if it runs too many times, which can help prevent infinite loops. For example, introduce a counter that limits the number of iterations within a single frame.

3. Check Event Bindings and State Transitions

Ensure your state transitions (e.g., from walking to running and vice versa) are correctly set up to avoid re-triggering the same logic multiple times in quick succession.

4. Refactor Long Execution Chains

If the execution chain in your blueprints is too long or complex, break it down into smaller, more manageable functions or events.

5. Debug Logs

Add debug logs to see the values of Current Round and Max Rounds to ensure they change as expected and identify where the logic might be getting stuck.

Review Your Blueprint Code

Image Analysis:

  1. First Image: Event BeginPlay
  • Event BeginPlay triggers a While Loop that checks if Current Round is less than or equal to Max Rounds.
  • Ensure that Current Round is incremented correctly within the loop and that Max Rounds is set to a reasonable value.
  1. Second and Third Images: Movement and Animation Logic
  • Your movement logic involving AI MoveTo and animation playing should be checked for proper completion handling.
  • Make sure that once the NPC reaches the destination or the animation finishes playing, it correctly sets the next state and doesn’t re-enter the While Loop immediately.
  1. Fourth Image: AI MoveTo and Pathfinding
  • Ensure that the AI MoveTo node has proper handling for both success and failure conditions. If the NPC gets stuck, it should not re-trigger the same movement logic endlessly.

Example Debugging Steps:

  1. Event BeginPlay Adjustments:
  • Add a print statement to see the initial values of Current Round and Max Rounds.
  1. While Loop Exit Condition:
  • Add a counter to limit the loop iterations.
  1. Increment Current Round Correctly:
  • Ensure Current Round is incremented inside the loop or after the loop completes a relevant section.
  1. AI MoveTo Success and Failure Handling:
  • Ensure both success and failure outputs of AI MoveTo have proper state-setting logic to avoid re-triggering the same move command immediately.

Hope this helps.

Hey @Marci1209!

It’s definitely use of the WhileLoop!
You don’t have a completed option, nor will it reach a threshold, so it will run this code an infinite amount of times per frame, crashing your game. UE5 is telling you, “Hey, this will make you crash!” Because the WhileLoop has no stopping point.

Try removing that Loop and do a simple branch instead. Then on whatever event makes them lose ammo, upon Current Rounds not equaling Max Rounds, you can have it run a custom event to do another AI moveto or whatever you’d like!

Let us know if we can help further! :slight_smile:

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