5
$\begingroup$

I need my game to always save all settings and scores, also always load them when running game. And... I want to also save theese objects, which has disappeared from scenes(collected points, boost ups, etc.) Who can help me, please?

$\endgroup$
4
+50
$\begingroup$

There's probably a much easier way, but you could have variables which change depending on what level your in ect, along with others that toggle the use of boost ups and collected points etc. Then you can save these variables to different .txt files so that they're the same when you start up the game next time.

For Python Variables:

Connect this script to a keyboard sensor, with a status of All Keys, followed by an always sensor. The pytho controller is in module mode. You need a folder next to the .blend called "SaveLoad". And finally add two game properties to you object just so that you can see that the script really works.

S Key to save, L Key to Load and A Key to change the variables, but obviously you can easily modify the code to you needs of automatic execution.

import bge
from bge import logic
path = logic.expandPath("//SaveLoad/")

cont = logic.getCurrentController()
own = cont.owner
keys = cont.sensors["Keyboard"]

var1 = 20
var2 = 60

def save():
    global var1
    global var2
    for key,status in keys.events:
        if status == logic.KX_INPUT_JUST_ACTIVATED:
            string = bge.events.EventToString(key)

            if string == "SKEY":
                info = str(var1)+","+str(var2)
                file = open(path+str(own)+".txt", "w")
                file.write(str(info))

            if string == "LKEY":
                file = open(path+str(own)+".txt", "r")
                line = file.readline().replace("\n","").split(",")
                var1 = int(line[0])
                var2 = int(line[1])

            if string == "AKEY":
                var1 += 1
                var2 += 2

    own["prop"] = var1
    own["prop0"] = var2

For Game Properties

This script needs to be set up the same way as the variable script, but yet again you do have the option to change it to your needs.

from bge import logic
path = logic.expandPath("//SaveLoad/")

cont = logic.getCurrentController()
own = cont.owner
keys = cont.sensors["Keyboard"]

### Use keyboard
for key,status in keys.events:
    if status == logic.KX_INPUT_JUST_ACTIVATED:
        string = bge.events.EventToString(key)

        ### Save "prop" and "prop0" to file
        if string == "SKEY":
            info = str(own["prop"])+","+str(own["prop0"])
            file = open(path+str(own)+".txt", "w")
            file.write(str(info))

        ### Load "prop" and "prop0" from file
        if string == "LKEY":
            file = open(path+str(own)+".txt", "r")
            line = file.readline().replace("\n","").split(",")
            own["prop"] = int(line[0])
            own["prop0"] = int(line[1])

        ### Change values
        if string == "AKEY":
            own["prop"] += 1
            own["prop0"] += 1.3
$\endgroup$
  • $\begingroup$ Will it save in the same folder as .exe when I make game runtime? Will it use .exe file's directory/SaveLoad/? $\endgroup$ – Adrians Netlis Dec 31 '14 at 20:32
  • $\begingroup$ It worked, thank's! Now I will just have to figure out the principals on gamesave. $\endgroup$ – Adrians Netlis Dec 31 '14 at 21:54
  • $\begingroup$ Is it possible to do it infinite like this: info = str(own[])+","+str(own[])+","+str(own[])? $\endgroup$ – Adrians Netlis Dec 31 '14 at 21:57
  • $\begingroup$ file = open(path+str(own)+".txt", "w")- does it save the game as object_on_which_is_added_python_controller_name.txt? $\endgroup$ – Adrians Netlis Dec 31 '14 at 21:59
  • $\begingroup$ Hi! This is important! How do I replace LKEY with always? What should I place in "LKEY" place for always actuator? $\endgroup$ – Adrians Netlis Jan 1 '15 at 13:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.