Purpose of bge.logic.NextFrame()

I’ve been searching for a functionality similar to this, and when I think I’ve finally found it, it doesn’t quite work like I perceived it to. I’ve done searches for it but I can’t find any solid documentation on what it does. Can anyone help me out?

bge.logic.NextFrame() should theoretically bump the render to the next frame, allowing you to put it inside a loop manipulating object location for instance. It would make things so much easier if it allowed the next frame to render while running a script, but it doesn’t seem to work like that.

I am the author of that patch, and its reason is simple: grabbing mainloop control in Python, to support parallel systems of game logic, independent of logic bricks.

It was never meant to run inside a logic brick, this may easily lead to infinite recursion. To prevent this, bge.logic.NextFrame() will do nothing unless mainloop control has been grabbed by Python.

If you want to suspend your Python controller script until after the next frame is rendered, you should do it differently: storing a Python variable (e.g. “progress”) in a global variable or a dict, and reading it back in on the next frame, when the logic brick system activates your Python controller again.
Don’t use a loop for this.

How can Python get the FULL CONTROL?? :slight_smile: (interessing)

At present you create a custom property on the scene (or is it world) called main with a string value of the name of the python file. I’ve written a patch so that you’d subclass bge.types.KX_PythonLogicLoop and define it in the UI, but it needs further work to fix Blender :wink:

how? i never see that was possible add property to the scene, anyway “something happen”

i have add a new property ,
called it -> main
value -> myMod.main

the the script is


#myMod.py

import bge

def main(cont):
    for ob in bge.logic.getCurrentScene().objects:
        i.worldPosition.x += 0.1 


but nothing happen , when press “P” the game not start…

i’m on the right path?

PS: i use blender 2.65.0

You must write the file path with the .py extension. If you want to call the main() function, put main() at the bottom of the file.

the game not start(or better,end immediately) and and the consolle say:

Blender Game Engine Started
Yelding controll to Python script “myMod1.py” …
Exit Python script “myMod1.py”
Blender Game Engine finisched

the “setup” in as in the screenShoot:

Attachments


i tried to add also a line , but not work (still the same error)


import bge


for i in bge.logic.getCurrentScene().objects:
    i.worldPosition.x += 0.1
    
    
bge.logic.NextFrame()

That means it’s working. You’ve given the Python script full control, meaning it tells the game what to do and when (although, in reality it can’t exclude specific components of the nextFrame call, so it’s not fully in control). So, you’ve simply run one frame and then exit. If you want to run more than one frame, use a while loop. However, I’m not sure how that would work in terms of timing or exiting, so give it a try.

umh… continue to run only one frame (from print return correct stuff)


import bge
import random


for i in bge.logic.getCurrentScene().objects:
    i.worldPosition.x += random.random()
    print(i.worldPosition)
    print(i.name)
try:
    bge.timer
except:
    bge.timer = 0
    
print(bge.timer)
    
bge.timer += 1


if bge.timer < 500 :
    bge.logic.NextFrame()
    

how make to run 500 times?

some progress :smiley:
this “work” (ie : is visible a cube that move)


import bge
import random


try:
    bge.timer
except:
    bge.timer = 0
    


while bge.timer < 50000 :
    for i in bge.logic.getCurrentScene().objects:
        i.worldPosition.x += random.random()
    bge.timer += 1
    bge.logic.NextFrame()
    
    

ps: the timing is effectively strange … boh (it take less than one second to come to 50000)

The script is only called once by the engine.

i have got the mechanism, so basically all bge run inside the “while”.

but not clear if it can give also some advantage , maybe is usable for “massive subclassing” ??(ie all obj created get a new function for example , avoiding try/except, if not “init” in own and other annoyng stuff?)

this is the code that work:

press ESC to exit


import bge
import random
import time


t = time.clock()
tMax = t+5.0 


FPS = 60.0
frameTime = 1 / FPS






LOOP = True
while LOOP : 
    #########
    t1 = time.clock()
    #########
    
    if bge.logic.keyboard.events[bge.events.ESCKEY] : 
        LOOP = False
    
    obs = [i for i in bge.logic.getCurrentScene().objects if "prop" in i]
    for i in obs:
        i.worldPosition.x += random.uniform(-1,1)
        i.worldPosition.y += random.uniform(-1,1)
        i.worldPosition.z += random.uniform(-1,1)
    
    #########
    t2 = time.clock()
    #########
    
     #timeForSleep = t2 - t1
     #if timeForSleep > 0.0:
     time.sleep(0.016) #...hem...is calculable...
     bge.logic.NextFrame()

It’s useful for alternative logic systems, like HIVE. But not useful enough in my opinion, hence the patch.