How to suspend a scene using Python?

Hi,

I’d like to be able to suspend & resume a particular scene without using logic bricks, however the only documentation that I’ve been able to find anywhere on how to do this isn’t particularly useful (to me) since is has no usage examples and seems to be written in some kind of esoteric shorthand:

https://docs.blender.org/api/blender_python_api_2_78_release/bge.types.KX_SceneActuator.html#bge.types.KX_SceneActuator

I think that:

bge.logic.KX_SCENE_SUSPEND

…looks like what I’m looking for, but how would I implement this into my code below?

    if ESCKEY and 'paused' in logic.globalDict:
        if not logic.globalDict['paused']:

            #Pause code here

        else:

            #Resume code here

I’d be very grateful to anyone that can help me with this… thanks!

Have you checked this one out yet? https://docs.blender.org/api/blender_python_api_2_78_release/bge.types.KX_Scene.html#bge.types.KX_Scene.suspend


scene = bge.logic.getCurrentScene()

if Condition:
   scene.suspend()
else:
   scene.resume()

By the way, what is the purpose of the dict entry? I’m kinda confused by it. You can check if a scene is paused with scene.suspended – it returns true if it’s paused, false if it’s not. Might come in handy.

1 Like

The GD is there for easy acces trough out the blend file, and if needed to save it.

Here is one with the use of a GD


scene = bge.logic.getCurrentScene()
GD = logic.globalDict['paused']  


if not GD:
    print('no GD[paused] found')
    return
          
if ESCKEY and GD:      
   scene.suspend()
else:
   scene.resume()

Ah yes, that’s exactly what I was looking for. Thanks!

dont use the GD. what if you open a new blend while the game is paused? you will have to keep track of the variable yourself, instead of the garbage collector.

i personally think this GD method is cleaner.

ESCKEY = bge.logic.keyboard.events[bge.events.ESCKEY]

if ESCKEY == 1:  # only process pause on key press
    paused = GD.get("Paused", False)
    if paused ==True:
        scene.resume()
        GD["Paused"] = False
    else:
        scene.suspend()
        GD["Paused"] = True

lots of ways to do it, anyway you don’t have to double check the true/false or set state


if paused ==True:



it will be enought to do


if paused: #for True or if it is set

if not paused: # for false or not set

Also be careful because suspending a scene from inside itself stops further logic inside the suspended scene, so you will be able to pause it, but not resume it.

Unless you add a second scene and trigger logic from there.

exactly…typical usage is in a UI scene…in case anyone might be swinging by and wondering how it can be done.