Removing/Adding scenes problem (2.58 + Python)

When I’m writing scripts for games, I use module scripts ( with defined functions inside. At the top of the script, not in a defined space, I have this code:

from bge import logic
sce = logic.getCurrentScene()
sl = logic.getSceneList()

def firstFunction():
    object = sce.objects['name']

I’m not sure if this is a very “morally acceptable” way of starting a script, so please let me know if there’s a more efficient way of getting these important variables instead of typing them at the start of each function.

The problem is that when I remove an overlayed scene with “scene.end()” and re-add it with “l.addScene(‘name’)”, I receive this error multiple times when the newly overlayed scene runs its script which tries to access those variables defined at the header of the script:

SystemError: value = CList[i], Blender Game Engine data has been freed, cannot use this python variable

What I suspect is happening is the variables defined at the top of the script have been cleared from memory and aren’t being redefined when the script runs again once the scene has been re-added. I run these functions by Module controller block rather than Script.

So why is it that these variables are recognized the first time the scene is overlayed and the script run, but not the second time the scene is overlayed? Is there any way to get around this, or is there something I’m missing here?

Any info would be of use, I’m currently having to move the position of the overlayed scene’s camera far away from the center of attention to make it look like the scene isn’t there.

I want to know that also. I have been having troble with it.

When run as a module, those variables become module level variables and are kept intact from one run of the module to the next. If you want to have those lines re-run, you need to call reload() on the module (I think this is in the imp module now). Debug mode on the module controller would probably also work.

However, calling reload all the time is slow since you’re not taking advantage of any caching. Instead, you could make a module to fetch the information:


from bge import logic

def sce():

def sc():
import g

def my_amazing_function():
    if "my_really_cool_object" in

Thanks! Very helpful. I have a module set up to handle common functions in my game and I’ve fixed the problem earlier. It makes the code a lot more organized as well.

Blender Game Engine data has been freed

It simply means:

  • you are refering to data that does not longer exist.

Mostlikely you are trying to access a gameobject from the removed scene.

sce = logic.getCurrentScene()
sl = logic.getSceneList()

in the initialization part of a module is dangerous as long as you can’t guaranty

  • it is used in one scene only (first line) and
  • the active scenes structure does not change (second line).

Better directly use the functions provided by GameLogic/logic and update your variables at each call to your methods.

Just to let you know