while crashes the game?

hi!
please check this script and scroll down for the question

import GameLogicimport bge
from bge import logic


sce = logic.getCurrentScene()
cont = logic.getCurrentController()
soldier = cont.owner
mouse = sorted([i for i in sce.objects if i.name == "mouse_soldier1"],key=lambda o: soldier.getDistanceTo(o) )[0]


anim_walk = cont.actuators[0]
anim_stand = cont.actuators[1]


distance = soldado.getDistanceTo(mouse)


if distance>1:
    cont.activate(anim_walk)
else:
    cont.activate(anim_stand) 

this works fine, but i want to replace this if

if distance>1:
    cont.activate(anim_walk)
else:
    cont.activate(anim_stand) 

for a while(so it loops inifinitely), but when i do

while distance>1:
    cont.activate(anim_walk)
else:
    cont.activate(anim_stand) 

blender crashes!

anim_walk and anim_stand are “replace mesh” actuators
any ideas on what is wrong?

I supose that blender don’t acuealy crash, but it hangs up on the logic tic when the while loop where runned for the first time,
The engine works in the way that it need every script/action to finich in order to advance to the next logic tic, when u set an infinity while loop the script never finich and therfor the engine never goes to the next logic tick and then “frezes”

I might be wrong, but I think this is why it chrashes 4 U

(sry 4 bad spelling, had no spellchek on this computer)

I think you might be misunderstanding how while loops work. While loops are like for loops, but they go on until the condition is satisfied. If distance is greater than 1 when the game starts, then it will continually execute cont.activate(anim_walk) in the current game frame, and so never continue to run the game. Why would you want to replace the if statement with a while statement? Why not just run the script every frame, if you want that?

thanks! that cleared me some things up, i dont want to run the script every frame cause it drains the logic and lags so hard

better avoid to use while because can due this, just you can forget to write one line.

if you have a “undefinied” loop you can also using the normal for , with a big number.
at least in the worse case you make just some useless calculation


L=[]
x=0
while x<10:
    L.append(x)
    x+=1 # if you forget this line the game freeze
    
print(L)








L=[]
x=0
for i in range(100000) :
    L.append(x)
    x+=1
    if x > 10 :
        break


print(L)

just to know, but in this case “if” make the right work as say SolarLune