Control UPBGE with and exit from mainloop (now it crashes)

Hi! I want to create a project with UPBGE using only a python script and without using any logic brick.
I’m using UPBGE 0.30.0 Alpha (based on Blender 2.92.0 Alpha).

I followed this tutorial:

and I created this script inside blender text editor (the blender file is named test.blend):
#! bpy
import bge
import time
import timeit

# active scene cube
scene = bge.logic.getCurrentScene()
# the object "Cube"
cube = scene.objects["Cube"]

keyboard = bge.logic.keyboard

LOOP = True
FPS = 60  # Frames per Second
last_time = timeit.default_timer()


while LOOP:
    # mainloop
    # wait until next frame
    current_time = timeit.default_timer()
    # sleep time until next frame
    sleep_time = 1 / FPS - (current_time - last_time)
    if sleep_time > 0:
    last_time = current_time

    # change scene
    # read keyboard input
    k_events = bge.logic.KX_INPUT_ACTIVE
    # uparrow key pressed
    if[] == k_events:
        # move up cube .005 Blender-Units
        cube.position[2] += .01
    # downarrow key pressed
    if[] == k_events:
        # move down cube .005 Blender-Units
        cube.position[2] -= .01
    # escape key pressed
    if[] == k_events:
        # Stop mainloop -> exit program
        LOOP = False
    # render next frame

It works correctly and I can move the cube up and down with the keyboard arrows, but when i press Esc blender freezes for about ten seconds and then it crashes with this error:

Debug: Exit Python script ''
Blender Game Engine Finished
Writing: /tmp/test.crash.txt
Segmentation fault (core dumped)

This is the content of /tmp/test.crash.txt:

# Blender 2.92.0, Commit date: 2020-11-25 10:08, Hash acfa07879f0e

# backtrace
./blender(BLI_system_backtrace+0x20) [0x8923020]
./blender() [0xfa494a]
/usr/lib/ [0x7ff7754776a0]
./blender() [0x746d930]
./blender(GPU_viewport_free+0x10) [0x746e5e0]
./blender(_ZN8KX_SceneD1Ev+0xac) [0x1da240c]
./blender(_ZN8KX_SceneD0Ev+0x9) [0x1da29f9]
./blender(_ZN19BL_BlenderConverter11RemoveSceneEP8KX_Scene+0x10b) [0x1d3618b]
./blender(_ZN15KX_KetsjiEngine10StopEngineEv+0x34) [0x1d7a864]
./blender(_ZN11LA_Launcher10ExitEngineEv+0x19) [0x1d33879]
./blender(_ZN18LA_BlenderLauncher10ExitEngineEv+0x9) [0x1d33759]
./blender(StartKetsjiShell+0x3b4) [0x1d32c84]
./blender() [0x1ca8d4d]
./blender() [0x132b508]
./blender() [0x132e464]
./blender() [0x132f70a]
./blender() [0x132fa1c]
./blender(wm_event_do_handlers+0x60a) [0x13303fa]
./blender(WM_main+0x20) [0x1326f20]
./blender(main+0x3e5) [0xebd105]
/usr/lib/ [0x7ff775462152]
./blender() [0xfa11d3]

# Python backtrace

So, my question is: how to stop the “game” and return to blender to work?
It’s very annoying to restart blender every time I want to test the “game”.

How are you stopping your game after stopping the loop?

Pretty sure the Segmentation fault (core dumped) is the problem.

To stop the game I simply press Esc on the keyboard.
If I open a new file in Blender/UPBGE (File > New > General), when I press P the game starts, when I press Esc the game ends and 3D viewport reappears.

Do you mean that this is a UPBGE bug?
What should I do in this case? Open an issue on github?

You might need to stop the script, then stop the game. If that doesn’t work, try the other way around.
To do that, set up a keyboard input sensor which listens to the ESC key. That’ll stop it from terminating the game.

I wouldn’t jump to that just yet… Pretty sure you’re just seeing the symptom from shutting down the game engine in an odd manner. Stopping the game engine isn’t like closing a program - the code and threads keep running. You see it all the time when people start using threads because they don’t automatically shut down when you stop the engine.

Fixed here:


So it was a bug! That’s kind of cool to witness to be honest! : D Especially since it was so quick!


Thank you very much @aWeirdOwl and all the others who contributed to the discussion!
Now i have to wait for a new build to try it out :grinning:

Thanks for fixing the bug youle!

^I second that sentiment!

I have been informed that github commits are raw source-code files, not compiled files.
However, there is a update with the bug fix for your problem atm.

Bro, a commit means a build update - every time there’s a new update there’s a new version available to the public for download.
Download it at or

Yes, today was uploaded a new build and I tested it, now it works!
I download the build from here:

1 Like

There is another little problem.
I exported the game (File > Export > Save as game runtime).
When I launch this executable if I press Esc the game is closing with this output:

Debug: Exit Python script ''
Error: totblock: 2

If I try to close it with Alt+F4 (or with the X button on the window’s titlebar) the game doesn’t stop and I get this output:

Error: Exit code 6: