I have a Mesh that should be change a several times via counter.

cont = bge.logic.getCurrentController()
scene = bge.logic.getCurrentScene()
own = cont.owner

if own['build'] >= 0:
if own['build'] >= 25:
if own['build'] >= 50:
if own['build'] >= 100:

stage = 0

for stage in range(0,100):
    stage += 1    

The problem is that the counter (game property) is increasing too fast. How can I make it slower? For example, make a delay in 5 tics after each increasing of variable "stage". "Skip" of Always sensor doesn't help. And I want to realize it through python (for loop).


Found my own solution.

stage = own['build']
stage += 0.5

if stage > 100:
    stage = 100  
own['build'] = stage 

But I'm interested to solve this with "for loop"


BGE has it's own event loop:

  1. Render Frame
  2. Run Logic
  3. Calculate Physics
  4. Repeat

So all of your logic/scripts will run inside a single frame. To make things run over multiple frames, they must be split into some way to make them run incrementally. So as you have discovered, you have to add 0.5 each frame rather than having it in a for loop (as this will run inside of a single frame). In many cases you have to store things between frames, and this can be accomplished using game properties, the global dictionary, or global variables in module mode.

Other ways to have things run not in a single frame are:

  • Threading
  • Sockets to external application (eg networking)

It is worth noting that python can take over the events loop, in which case you can use bge.logic.NextFrame(). However you still wouldn't want a specific function/loop to take control and force a frame refresh.


I found more useful solution!

I have created 4 game properties: -test (int) -delay -start (float) -break (float)

Next I change a value of break property and wrote a small script for testing:

import bge


if own["delay"] >= (own["start"] + own["break"]):
    own['test'] += 1
    print (own['test'])   

And yes! Now I can easily change a speed of executing!


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.