# \[CLOSED\] Infinite Loop error when using AI MoveTo

**URL:** <https://forums.unrealengine.com/t/closed-infinite-loop-error-when-using-ai-moveto/1854149>\
**Category:** Programming & Scripting\
**Tags:** infinite-loop, unreal-engine, crash, Blueprint, question\
**Created:** [May 13, 2024, 3:50pm UTC](https://forums.unrealengine.com/t/closed-infinite-loop-error-when-using-ai-moveto/1854149 "2024-05-13T15:50:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marci1209](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Marci1209](https://forums.unrealengine.com/u/Marci1209)\
**Post date:** [May 13, 2024, 3:50pm UTC](https://forums.unrealengine.com/t/closed-infinite-loop-error-when-using-ai-moveto/1854149/1 "2024-05-13T15:50:19Z")

</div>

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

 ![SCR-20240513-pomt](https://d3kjluh73b9h9o.cloudfront.net/original/4X/0/0/3/0035d0fd5bffcdd9336d0012516257175641b3d1.png)  
 ![SCR-20240513-popb](https://d3kjluh73b9h9o.cloudfront.net/original/4X/3/a/7/3a7b99064f823599af8fcf364dc6fe0f765d7f97.png)

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

 ![SCR-20240513-potb](https://d3kjluh73b9h9o.cloudfront.net/original/4X/a/a/d/aad420d78f955c8d662ef2c22980d55bee63be84.png)  
 ![SCR-20240513-poxm](https://d3kjluh73b9h9o.cloudfront.net/original/4X/c/d/6/cd6ae75c6cac6e0c0efa9c76039b7c3ead7d25c4.png)  
 ![SCR-20240513-ppag](https://d3kjluh73b9h9o.cloudfront.net/original/4X/9/e/4/9e447e14dd9a533288a33f5b9618cc9b3a8b90df.png)

---

<div class="post-metadata">

**Author:** ![SpudMcKenzie](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/spudmckenzie/32/937130_2.png) [@SpudMcKenzie](https://forums.unrealengine.com/u/SpudMcKenzie)\
**Post date:** [May 13, 2024, 9:07pm UTC](https://forums.unrealengine.com/t/closed-infinite-loop-error-when-using-ai-moveto/1854149/2 "2024-05-13T21:07:39Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Mind-Brain](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/mind-brain/32/906573_2.png) [@Mind-Brain](https://forums.unrealengine.com/u/Mind-Brain)\
**Post date:** [May 13, 2024, 9:13pm UTC](https://forums.unrealengine.com/t/closed-infinite-loop-error-when-using-ai-moveto/1854149/3 "2024-05-13T21:13:18Z")

</div>

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! 🙂

---

<div class="post-metadata">

**Author:** ![system](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/1/3/713c9d3f58553f0de89543d76a8a3a2779dc9fa4.png) [@system](https://forums.unrealengine.com/u/system)\
**Post date:** [June 12, 2024, 9:13pm UTC](https://forums.unrealengine.com/t/closed-infinite-loop-error-when-using-ai-moveto/1854149/4 "2024-06-12T21:13:54Z")

</div>

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