How do I run a while-loop without starting a new thread?

I would like to know if anybody knows a way to run a “while” loop in blenders game engine. If I want to run a while-loop in a new thread, the script get F*** up, but without a new thread, blender freezes and crashes :frowning:

I think if you have a while loop that is to complex, then issues can happen in Blender. Blender does not update itself until all game logic has finished, so if you have while loop that is to complex or infinite, then I would recommend using threads.

Here’s an example using a while loop with no threads
simple.blend (60.5 KB)

Here’s an example using an infinite loop with threads
endless.blend (61.1 KB)

I know you didn’t want to use threads, but you might want to take a look at my threaded example.

There are some problems with using threads in the game engine, you will have to detect when the engine has quit and explicitly end the thread or it will continue.

While loops are okay, as long as you know they are definite (such as in pathfinding algorithms), however, using an indefinite while loop will not work. Instead, you will have to run whatever is inside the loop every logic tick, replacing the loop with an if condition, e.g:

#old
while True:
    pass

# new
if IN_LOOP:
    pass
else:
    pass

If you code is complex, you may want to look at using some sort of state machine with the loop being a single state.