[How To] transfer a property to a later scene?

Hello all!

Once in a while we get questions like: How can I access the players health from another scene? How can I keep the number of wins/loss? etc.

Rather than explaining it again and again I want to present you some solutions.

What is the problem?

Imagine your scene is a ship. The ship has cargo holds (= game object properties). Each cargo hold has crates with data (= property values).

Now you want to change the ship (set scene). What happens?

The captain, sets the ship on fire and let it sink. You the only passenger you are transferred to a new ship that was build while the old one is sinking.
Unfortunately all the cargo holds and all the crates are lost with the old ship :(.


How to rescue the cargo holds?
You can’t! The cargo holds are mounted to the ship and will sink with it.

How to rescue the crates?
You need to tell someone to do that. What options you have is explained at the next posts.


A very simple option is to send the data via message. You send the messages while giving the command to change the ship.
This means the data is at the post office while the ship is sinking.
The post will deliver the post to the remaining ships which includes the new ship. You just need listeners at the new ship watching for new messages (at scene start-up).

How you use messages to transfer data is explained at the BGE Guide to Messages incl. Healthbar tutorial.

Two steps:
Send -> sends the cargo via message
Receive -> reads the message and stores the cargo accordingly

Benefits:

  • most steps can be done via GUI
  • all steps can be done via Python code

Drawbacks:

  • messages only
  • Python code needed (to restore the message body)
  • no permanent storage
  • transfer can’t be repeated

Another often recommended method is to transfer your data to another ship that travels at the same time at the same see (=game).

You can use messages (see post #2) or Python code to transfer the cargo from one ship to the other. When using Python code both ships must both exist at the same time!

You need two steps:

  • Store - transfer the cargo from the old ship to the wingman ship
  • Restore - transfer the cargo from the wingman ship to the new ship

Store and restore do not need to be at the same time. Store should happen first.

Benefits:

  • transfer can be repeated as long as the wingman lives
  • new wingmans possible
  • supports Messages
  • supports value copy via Python code (is both scenes exist at the same time)

Drawbacks:

  • when the wingman dies the cargo dies with it (if not restored)
  • two steps to transfer the cargo from the old ship to the new ship
  • no permanent storage

Another recommended way is quite simple:
Throw all cargo overboard.

Wait! How to get it back from see? Bind them to buoys.
Then look for the buoys.


This time messages will not help. You have to use Python code. The see allows a lot of things to act as buoy: barrels, planks, shelves. You can even build your own lifeboat. (in terms of the bge => modules, attribute of a module, globalDict)

You need two steps:

  • Store - throw the cargo overboard of the old ship
  • Restore - collect and grab the cargo from see to the new ship

Store and restore do not need to be at the same time. Store should happen first.

Benefits:

  • transfer can be repeated as long as the buoys exist
  • the see will survive any ship

Drawbacks:

  • Python code only
  • two steps to transfer the cargo from the old ship to the new ship
  • no permanent storage

simpleDemo.py


import bge

def verySimpleStore(controller):
  bge.logic.globalDict["buoyA"] = controller.owner["cargoA"]

def verySimpleRestore(controller):
  cargoA = bge.logic.globalDict.get("buoyA")
  if cargoA is None:
    return
  controller.owner["cargoA"] = cargoA

Store:
sensor (measuring the scene change)
–> Python Module: simpleDemo.verySimpleStore
Restore:
sensor (measuring the scene just started [AlwaysActuator])
—> Python Module: simpleDemo.verySimpleRestore

This last solution is an extension of the previous one.

The cargo is stored at the see as before. But the cargo can be stored at a permanent storage: A massive rock (= file).

This action has two steps:

  • Store - throw the cargo overboard of the old ship
  • Save - transfer the cargo to the rock

It is quite nice to know the cargo is save but we need it at the new ship. So we do the transfer into the other direction.
The cargo is thrown from the rock into the see. The new ship grabs the cargo from the see.


This needs two steps as well.

  • Load - throw the cargo from the rock into the see
  • Restore - collect and grab the cargo from see to the new ship

This method does not only allow to transfer data from one scene to another. It also allows to transfer data from one game session to another one.

This means we have 4 steps for all. Luckily we do not always nee all 4 steps. If the game session continues the cargo swimming in the see can be grabbed by the new ship as well (see post #4).

If you look at the complete picture it looks a bit complicated but it is not. Just keep in mind what step you need and look at this step.

Benefits:

  • transfer can be repeated as long as the buoys exist
  • the see will survive any ship
  • the rock will survive the see
  • permanent storage possible

Drawbacks:

  • Python code only
  • two steps to transfer the cargo from the old ship to the new ship
  • four steps to transfer the cargo from the old ship to the new ship on a new see

Hint:
If you use “GameLogic.globalDict” (also known as “bge.logic.globalDict”) as “rock” you can use the SaveActuator and the LoadActuator which is a quite good implementation of the Save and Load steps.
You still need to implement the Store and Restore steps via Python.

Example:
You can use the example from post #4. To save use the SaveActautor to load use the LoadActuator.

reserved post for examples

so ya, you made a post with a script. but iv tried to use it over and over. you may be the only one who can help. could you pleaze make me a .blend example!!! im using 2.69

here it is-
i need to copy a property between scenes

“”"You have many options here, let me find an old post of mine.

Easiest to use bge.logic.globalDict in a python script. Copy this script into a new file in the text window. Call this “properties.py” without the quotes.
On the object to send the property:

Create an always sensor, and set it to True Pulse Enabled (the first …).
Connect the always to a Python Module Controller (Python ->Module) and type ‘properties.set’(without quotes)

On the object to receive the property:

Create an always sensor, and set it to True Pulse Enabled (the first …).
Connect the always to a Python Module Controller (Python ->Module) and type ‘properties.get’ (without quotes)

In the script, change the value of ‘property_name’ to the name of the property to get or set. They can be different, as long as the property_name in get gets the property of that object, and the property_name of set is the same as the property you want to set it to.

import bge

def set():
cont = bge.logic.getCurrentController()
own = cont.owner

#Change this to the name of the property
property_name = ‘property_1’

bge.logic.globalDict[‘property’] = own[property_name]

def get():
cont = bge.logic.getCurrentController()
own = cont.owner

#Change this to the name of the property
property_name = ‘property_1’
own[property_name] = bge.logic.globalDict[‘property’]"""

i need to copy a property between scenes
plz leave a demo.blend

A great summary- thanks Monster!

Could you add an example blend to the last example, please, Monster! I will be really grateful!:slight_smile: i just need thet method for settings(save, load, store, transfer). I hope you will include it a save function, that works even if you close .blend and than run again!:slight_smile:

A save-load system (using the described methods) is the SaveLoader (be aware I’m not sure this is still working). I haven’t developed a new system recently.

You can also do a search on save or load in the discussion forum. This gets requested from time to time.

The “trick” to get the saved game state on game start is to separate this into several operations:

A) on game start: load + restore (if one of them fails … continue with the default settings)
B) on game end: store + save (you might need to override “ESC” for quitting the game)