0
$\begingroup$

I made a game with my pupils in our geography lessons this year. The task is to travel around the World and collect points. The single levels work very great but we can´t show the collected points at the end. The reason I guess is that we had to split up the levels to different blend-files. We had to do this because of some pythonscripts (mini-map, mousecursor) and the loading time. The structure looks like this:

                         ->australia.blend ->show collected points (works not)

menue    ->     spain.blend  

                         -> show collected points (works)

(Sorry the arrows should be more rotated...)

So is there any possibility to send a message or something like that from one blendfile to another?

Greetings from Hamburg

$\endgroup$
1
$\begingroup$

You can't send anything from a blend file. A blend file is just a container for data. It gets read and written by Blender. The BGE can read them (but does not write to it).

Game Sessions

As you are talking about blend files I guess you mean game session (you are using the game actuator to switch levels). A game session is when you start your game and lives until it ends (quit). When you start again, you get a new session without any relationship to a previous one. Game sessions can run parallel. They still have no relationship to each other.

What you need is to transfer game status between game sessions.

Parallel

When two game sessions are running at the same time they can communicate via network (Python -> socket). In this case I suggest to search for question/answer regarding network communication. Be aware this is no topic for beginners.

Sequential

When two game sessions are not running at the same time you need to persist the game status. Persistence between game sessions can be in the file system (or a database) -> It must survive the end of the game session.

Therefore I suggest you specifically look for a question/answer dealing with Save/Load. This requires a bit of Python knowledge, but there are quite a few tutorials out there.

Levels as Scenes

When your levels are different scenes within the same game session, you could transfer game data via Messages or via Python. This is the case when you use the scene actuator.

Sorry but I can't give you a better answer with the information I currently have.

I hope it helps anyway

$\endgroup$
0
$\begingroup$

You could write the points to an external file, and then read them at the end.

https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files

$\endgroup$
0
$\begingroup$

If all of the files are in the same directory, you can use Python scripting to save the points in all of them.

FILENAME = "points.txt"

def addPoints(points):
    with open(FILENAME, "a+") as f:
        f.seek(0)
        data = f.read()
        if data:
            data = int(data.strip())
        else:
            data = 0
        f.truncate(0)
        f.write(str(data + points))

def setPoints(points):
    with open(FILENAME, "a+") as f:
        f.seek(0)
        f.truncate(0)
        f.write(str(points))

def readPoints():
    # Returns 0 if the file doesn't exist
    try:
        with open(FILENAME, "r") as f:
            return int(f.read().strip())
    except:
        return 0

You can use these functions as follows:

addPoints(10)
setPoints(0)
points = readPoints()

If points.txt does not exist when addPoints or setPoints are called, the functions will create it. If readPoints is called and points.txt doesn't exist, it will simply return 0.

Copy all of this into your game files and use them where necessary. If you're making a game, I'm fairly sure that you know how Python scripting works, although if you need more help just write a comment.

$\endgroup$

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.