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…
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).
i.e.
when i call the infinteDefininton() may return a variable , None, or False etc.
infininteDefinition(): # if this true
print "do something" # do this
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 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')
#GameLogic.setLogicTicRate(60)
steppingtime = 60
counter = 0
while 1:
GameLogic.setLogicTicRate(60) # cant controlling with this
#sens.setFrequency(60)
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
break
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.
condition = 1
# This statement is equivalent
while condition:
print "Condition is True."
break
# 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!
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.