Load And Saving

(Jaoshua) #1

I’m trying to create a save system in my game i want to use global Dictionary but i cant find away to save the health property or any other property can some one help me

(Monster) #2

I guess you are talking about the BGE.

You need to place the “data to be saved” in the global dict via a custom Python controller. Be aware you need to use a marshallable data type such as string, int, float, boolean, list, dict. The content of the containers are restricted the same way.


import bge

storage = bge.logic.globalDict


def storeHealth():
   owner = getOwner()
   healthValue = owner["health"]
   storage["player's health"] = healthValue

def restoreHealth()
    healthValue = storage["player's health"]
    owner = getOwner()
    owner["health"] = storage["player's health"]

def getOwner():
     return bge.logic.getCurrentController().owner

The final implementation depends on what storage structure you want to use.
E.g. Top level = save point, 2nd level = object name, 3rd level = object elements

    "savepoint 1": {
        "player" : {
            "position": [0.0, 0.0, 0.0],
            "properties": {
                "health": 98.3,
                "armory": 7.5
        "merchant alpha": {
            "position": [130.2, 78.3, 2.8],
            "properties": {
                "gold": 133

As you see this can become quite complex. So you should think about before hand.