while loop overload, cant exit form BGE

Hello, im stick in a problem, seems very basic…and someone can help me

I try to use the whole loop in BGE but when i press p and after cant extit (esc) from BGE.

create an always sensor (TRUE button not active)

while 1:
print “hello”

blend file:


i.e. in python console interactive mode i can brake this loop with ctrl+c (keyinterup)

The BGE doesn’t like “while”, try to find a way to use “if” instead.

Euh …

1e Why would you use an endless while-loop in the first place.

2e You might create a keyboard sensor too and link it to a gameactuator - end this game

Hope this helps


When Blender executes the script and does nothing else until it comes to the end. So a while loop can’t be depending on anything changing outside the script. They are at the moment pretty useless compared to if loops. If the bge gets a more script only alternative to logic brick maybe…

Basically, a while loop goes and goes and goes until the script tells it to stop. You might use a while loop to do something like

while i>0:
[tab]print "hello"

(I can’t indent in BA, so you’ll have to add the 2 tabs manually to see it in action)
this would print “hello” 10 times.

I have yet to actually try a while loop in blender, so I might be wrong about all this, but that’s how it is in most programming languages I’ve used.


I think the while 1: loop behave very similar like Always sensor in TRUE mode (infinite do the script), and should work Esc (escape from runtime) end exit from BGE without overloading/crash instead need kill or end process of the blender.
With while loop i can controlling this infinite loop more flexibility, for i.e. within a infiniteDefinition() (do every frame something until this definition return something).

when i call the infinteDefininton() may return a variable , None, or False etc.

infininteDefinition():     # if this true
        print "do something"     # do this

If you use a while loop you need a break out condition so it can exit the loop… so you say while x=10 do this… else: do some other shit…

basically, the difference between a loop and an if statement is an’ if’ runs only once in a frame, then waits for the next frame to run again. A loop runs again and again in one single frame until it is told to stop- unfortunately since blender only does one thing at a time, the part that looks for the ‘esc’ key doesn’t run while the loop is- if the loop runs infinitely, blender freezes.

What people are calling an “if loop” is not actually a loop, because it breaks after it runs for the first time, then starts over next frame.

I agree with the Captain,

By using an endless-loop you stall Blender.

A loop should always have a condition that ends the loop (In fact your own program shows that best).

If-then-else-statements are no loops, so you can use them doing what you want to do,they always have an ‘open end’.


Thanks for helps :yes:

I thinking how can i control the while 1: infinite loop speed:

i tested my theory in pygame and it is work as i suspected but in BGE not work.

pygame code (this code run from an python editor not in BGE):

import  pygame 
from pygame.locals import * 
steppingtime = 60 
counter = 0 
clock = pygame.time.Clock() 
while 1: 
    clock.tick(60) # here can i contorlling the while loop speed in pygame 
    counter +=1 
    print counter 
    if counter > steppingtime: 
        print "one step" 
        counter =0

BGE code:

cont = GameLogic.getCurrentController() 
own = cont.getOwner() 
sens = cont.getSensor('mySensor') 
steppingtime = 60 
counter = 0 
while 1: 
    GameLogic.setLogicTicRate(60) # cant controlling with this
    counter +=1 
    print counter 
    if counter > steppingtime: 
        print "one step" 
        counter =0

can i controlling the while loop speed in BGE as in pygame?

seem this code is fine for me, put this code here ,may usable for others.

with while loop print infinite haliho. with time set, this way u can contorlling a loop within a script, and dont crash blender

cont = GameLogic.getCurrentController() 
own = cont.getOwner() 
sens = cont.getSensor('mySensor') 
  #need create a timer property
t =  int(own.time) 
while t == 2: 
    print "haliho" 
    own.time = 0 

The way you have it set up, you may as well use an if instead of a while- then you don’t need the break, which at the moment just ensures that the while only runs once.

Thanks that is simpler than before :slight_smile:


condition = 1
# This statement is equivalent
while condition:
        print "Condition is True."
# to this statement.
if condition:
        print "Condition is True"

Unless you want blender to stop completely, it’s a good idea to avoid while statements all together.

Want to kill your framerate just for kicks!? Attach this to an always sensor!

import time

The one huge difference between an always pulsing sensor and a while loop is… The pulsing sensor pulses every frame, whereas the while loop will be processed once in one frame. In other words, if your while loop never ends then you won’t move on to the next frame and Blender will look like its crashed. Thus pushing the esc key won’t work because it would process that for the next frame.