Resume from a pause menu

I have created a pause test. When I press P, and then enter, the scene will pause. But when I try to use a message sensor to unpause the game and resume the current scene, nothing happens. Even the ESC key does not work. Ive written out my script like this:

# import bge
import bge
import bge.logic as gl
import GameTypes
import Rasterizer as r

# This didn't turn out so well..

exit = gl.getExitKey()
if exit <= 130 or exit == None:
    exit = 130

print(dir(GameTypes))

# get dictionary
dict = gl.globalDict

# get scene list
sceneList = gl.getSceneList()

# get scene
sce = sceneList[0]

# get current scene
scene = gl.getCurrentScene()

# object list
objList = scene.objects

# get controller
cont = gl.getCurrentController()

# get owner
own = cont.owner

# get scene name
sceneName = scene.name

# print scene directory (testing only)
#print(dir(scene))

# print logic directory (test only)
#print(dir(gl))

#print(sceneList)

 paused = {'PAUSED' : sceneList}
 mains = {'LEVEL' : sce.name}
 dict['PAUSED'] = paused
 dict['LEVEL'] = mains

def copyPropertyFromPlayer():

   sens = cont.sensors

   # message sensor
    messageSensor = cont.sensors['recMessage']

    copyProp = cont.actuators['copyProperty']

    objList = sce.objects

    player = own['player']

    if not player == '':
	    name = objList[player].name
	    print(dir(copyProp))  
	    copyProp.value = objList[own['player']]
	    copyProp.propName = player['main']
    else:
	    print('copyPropertyFromPlayer() Error: No players found to copy from')
	    return False

    #print(pause_scene)
    #print(main_scene)

# send pause message function
def doPause():

    invalid = own.invalid

    # get sensor and actuator by property, these must be correct!
    if invalid == False: sen = cont.sensors[own['sen']]
    if invalid == False:   act = cont.actuators[own['act']]
    if invalid == True: sen = cont.sensors['sen']
    if invalid == True: act = cont.actuators['act']

    # show cursor
    r.showMouse(True)

     # print out commands for sensor and actuator, this is testing purposes only
    #print('SENSOR COMMANDS --',dir(sen))
    #print('ACTUATOR COMMANDS --',dir(act))
     if scene.suspended == False:
	    gl.addScene(own['pauseSce'])
	    own['main'] = dict['LEVEL']
	    gl.saveGlobalDict
	    sce.suspend(own['main'])

def doResume():

    invalid = own.invalid

    # message sensor
    messageSensor = cont.sensors['recMessage']  

    # get sensor and actuator by property, these must be correct!
    if invalid == False:   act = cont.actuators[own['act']]
    if invalid == True: act = cont.actuators['act']

    message = messageSensor.bodies[0]
    act.scene = message
    print(act.scene)
    cont.activate(act)

def main():

    # save and load dictionary if not already
    if dict['LEVEL'] == {}:
	    dict['LEVEL'] = own['main']
	    gl.loadGlobalDict
    if dict['PAUSED'] == {}:
	    own['pauseSce'] = dict['PAUSED']
	    dict['PAUSED'] = own['pauseSce']
	    gl.loadGlobalDict
	
main()

Just to clarify, dict means globalDict, and gl means logic.

I am also trying to make it so when the scene is paused, a message named ‘scene’ is sent containing the scene name that is not the pause menu but the main one. The pause screen would then be added upon pressing Enter, and the scene message would then be sent, and the main scene would suspend itself. The main scene could be any name. Next, the code in the pause menu detects if a message called ‘resume’ is sent, and so, removes the pause scene and resumes the main scene.

Explanation:

The copyPropertyFromPlayer() is designed to copy a property from an object within the main scene. It searches the main scene, and stores the property ‘player’ in the actuator named 'copyProperty, which copies the player property from lets say, a cube in the main scene from the pause menu. This in turn, is used to set the ‘main’ property to the main scene.

doPause() tries to add a pause scene and store the main and pause properties in the globalDict to use if the game engine is turned off. These properties are reloaded in the main() function. That way, the pause menu won’t stuff up when the player tries to use it.

doResume() does the opposite and resumes the main scene while removing the pause menu. The scene actuator used to resume the main scene now takes the message called ‘scene’ which contains the players ‘main’ property and therefore resumes the scene by setting the scene as the players main property.

Hope this explanation helps!

Is there a working pause menu? My keyboard is not broken, but suspending the scene means the ESC key does not work. This forces me to exit Blender.

Once a scene is paused, nothing in it will work.

Put your pausing/resuming logic in a different scene to the one that you are pausing. I’d recommend putting the logic inside the same overlay scene that’ll be showing the unpause button.