My Python Script Crashes Blender :|

I made a simple Body Clean up script so that it removes all of the dead bodies in the map every 10 seconds. But Blender crashes whenever I run the script. Im pretty sure it has something to do with importing Time into blender.

from time import sleep

cont = GameLogic.getCurrentController()
own = cont.getOwner()

remove = cont.getActuator(“remove”)

while True:





Any ideas of what to do. Maybe an alternate way of going about this. Maybe one of those Timer/ Interval property things will work, I dont knwo how those works and this seemed simpler.

Thanks to all those who help :S

Just use a timer property linked to 2 actuators: one that resets the timer (via a property actuator) and one that sends out the “remove” message.

In this overly complex blend file (I wanted to make something fun) you can see what I mean… though I’ve used an int property instead of a timer property because I wanted to have it represent text, and timers have like a gazillion decimal places. Just use the arrow keys to control, hit the blue boxes to make them “dead” and then all the dead ones will be removed.



removedead.blend (259 KB)

# That will freeze the game for ten seconds.

while 1:
        print "This will freeze the game completely!"

lol - What chaser is trying to say to you is that the expression for your while loop always evaluates to true, meaning that your While clause will continue indefinitely -never actually finishing it’s loop.

you should put in a variable called something like ‘continue’… so: while continue == true: … blah… blah…

and somewhere inside the loop you should then put: continue = false - meaning the while loop will be escaped.

Someone once told me to never use “while” in BGE scripts. Use “if” instead.

Do you know why ?

the next frame is not rendered until all scripts finish, if your script takes 10 seconds to finish, each frame will take 10 seconds xD. the only way to do such a task is to use threads (i heard that is buggy), a timer property, or use a property to count frames

Someone once told me to never use “while” in BGE scripts. Use “if” instead.
Sometimes it is necessary to use a “while” rather than a “if” statement. A “while” statement is a loop (similar to “for”). Of course, like cyborg_ar said, BGE refreshes after each script is finished. “While” statements loop around many different times. The speed of a “While” statement depends on how much code is in the statement and how many times it loops around. For example:

num = 1
while num < 3:
num += 1
With this code, it loops around twice and it only has one line of code in the statement, so 2 lines of code are excluded. Now take a look at this:

num = 1
while num < 101:
num += 1
pos = own.getPosition()

In this code, it loops around 100 times and it has 3 lines of code, so the total amount of code excluded is 300 lines of code. Normally, python scripts are not hunderds or thousands of lines of code. That’s why “while” loops appear to be slow.

One way to patch this is to have the script excluded once out of every 3-5 frames. Therefor, it would only lag on the 3-5 frame making it more pleasant to the eye.

I guess the if statement will work anyway because the script is always looping.

Thanks been alot of help.