Adding Objects from different Scenes

I have a feeling this has been asked before and I did do a frantic search o it but im stumped…
How do you add the same exact object (logic, properties, material, and all) to a different Scene.

say: get “Cube” from ‘Scn1’ added to Scn2 like you would with an emty attatched with a [Edit Object: add] actuator?

-All In game (e.g at runtime)

You can’t.

You can use LibLoad to load objects from an external file.

There is usually no need to “copy” objects from one scene to the other. If you need an object you can add it to an invisible layer and use addObject during runtime.

What are you trying to do?

Yeah I tried to do this just a few weeks ago. I don’t really remember why I needed to though. I think I may have been trying to change properties of objects on other scenes and then found that linking objects to different scenes worked for what I needed because it also links properties.

The objects are linked in Blender only. When loading scenes into the BGE the objects become independent instances. So they share teh same design/structure but not the same data.

Well I have different maps set up in each scene, Map1, Map2, Map3, etc… I want to load my character to each scene/map without changing its object name.

Ahh… Use link groups.
This should help:

Basically, groups allow you to have a central object that you can edit and modify as much as you want. Then, all the group objects linked to this central object will update automatically.

This is most likely what you want.

Edit: And about object names, this shouldn’t be a problem with this solution. If something pops up just ask :slight_smile: However; there shouldn’t be a need for specific object names, you can always locate an object with other, more dynamic features (such as properties).

Im familiar with that, I want the same character/object to jump around in different scenes “In game/while playing” while also maintaining all its data/properties but thanks. Im looking into LibLoad @Monster mentioned.

If you want the same state data (properties, transformation, states …) and not just structural data (mesh, material, …) you need to transfer it via Python.

This is the same as with saveLoad or multiplayer network.

Hi all,
How about this particular circumstance:
I create a scene that loads my object dynamically via libLoad. when I need to load another scene using the same object I need to free the file before load it again via libLoad to the other scene (otherwise you get error).

Well although it works, it takes a long time to reload it. So, my question is:

Since the object is already loaded on the first scene, can I import it to my second scene without freeing the blend file and doing libLoad all over again?

The easiest way to transfer properties and data through scenes is using global variables.

If you use CTRL + L you can link objects between multiple scenes, you could either directly link your character into the scene, or link it to a hidden layer and add it as you would any hidden object.

I’m not 100% sure how your character will behave if it’s in two active scenes though.

Well I got something going the way I wanted and going well but now Im facing an error @hyperbatata mentioned. Im getting, “SystemError: val = gameOb[key]: KX_GameObject, Blender Game Engine data has been freed, cannot use this python variable” - in line 41 which is the last code i wrote below
I know it has something to do with restarting the HUD scene or the MAIN scene but how do i fix this??

Here is a brief what’s going on:
I have an empty named “GData” with a python module attached “AsgData.Update” in the HUD scene. (the MAIN scene adds the HUD scene as an overlay"])

Now in the MAIN scene (where the charater is), when the character dies I first remove the HUD scene then restart the MAIN scene. (by default it will add back up the HUD scene as an overlay).

Now the code:
my python modue "AsgData.Update is atached to a ‘tap’ always sensor so once added it loads

{in} - attached to an empty in the HUD scene i mentioned
import GameLogic as g

c = g.getCurrentController()
o = c.owner
objL = g.getCurrentScene().objects

MAIN scene object ------------

sl = g.getSceneList()
for i in sl:
MAINscn = SceneList[“MAIN”]

objLM = MAINscn.objects

PwrAsg = objL[“PwrAsgn”] #this is another empty “PwrAsgn” in the HUD scene that has these property “PowerObt”

Player = objLM[“PlayerBase”]

def Update():
Player[“PowerPlc”] = PwrAsg[“PowerObt”] #I cant pass the property to the player ‘after restarting’ cause of the error

I’v tried several aproaches to this issue:
I’m building a game where the character must load into one scene, then exits and must be loaded on the other scene.
I’ve tried with libLoad() to load the character each time a scene is loaded (but it seems too slow for my taste).
Also I’ve tried the addObject() With the caracter previously linked to a hidden layer. In this case the character loads perfectly on the first scene but I get the ‘Blender Game Engine data has been freed, cannot use this python variable’ ERROR on the second scene.
It seams, by the tests I’ve made, that since the character is linked in both scenes, the file is not freed and I get the error (also, it also seems to be related to the actuators used on the original file).
So, I tried to link the character directly into the active layers and drop the addObject alltogether, but the error persists.

Seems like i need to free the file between scene transitions, but I cant figure out how.

Is it clear?

well, I’ll keep looking.


Please use code tags to post code snippets!

Blender Game Engine data has been freed

This means you are accessing a reference to an object that does not exist anymore. This can be because, you ended the object or the scene it lived in.

Uhm, interesting,
So i just need to work the code around this. TO make sure I call the object after i make a new instance!


I’ll check it out!

I’m pretty sure it is the other way around. You try to access an old object. But it is already dead.

I’m pretty sure it is the other way around. You try to access an old object. But it is already dead.[/QUOTE]
I’m still working on it, but I’m starting to think that is not the case!
I’ve made this example file that simulates the problem i’m having.

You load the mainGame.blend file that controls two linked scenes (map 01 and map 02). Just run into the portal and you are suposed to go to the next scene.

The same character is linked to each one of the scenes and an empty on each scene loads the character from a hidden layer.
exemple.rar.blend (751 KB)[QUOTE=Monster;2282181]

The scripts are simplified versions of the ones I’m working on, just for testing purposes.

I’m running out of ideas, but I think this has something to do with the actuators on the original character file (but I’m not sure).

Well, I’m definitely doing a tutorial on this issue after solving this pickle!

Thanks for your time!

Using classes is, to me, kind of a bad idea with the BGE (or at least, is a fairly advanced mechanic). You’re already working with a class-based system, since all instances of a single created object share logic (i.e. all spawned Player objects share the player’s game logic).

When you swap out a game object’s ‘internal class’ with a custom KX_GameObject-based class instance, you make all previous references to the game object invalid. I think you have to make sure that whenever you spawn a player object and replace the normal KX_GameObject with your custom one, objects only refer to it afterward (otherwise, the reference is invalid). You could also just check to see if the player reference is invalid or not with object.invalid. Your problem is there, though I can’t state specifically where it is. Sorry about that.

Thanks @SolarLune its a good point you raise!
The reason I’m using so many classes is because on the long run I’ll need to call methods from these classes from different objects in different points of the game, so its easier to just make them easily accessible!
But I never thought about KX_GameObject-based class instance issue!

I’ll check if this is the case later (after work) and I’ll reply if this is causing the problem.

Thank you all very much!

As I see it now, the problem is not actually swapping the game’s object internal class ‘KX_GameObject’ rather than the method I’m using to instantiate it.
As pointed out by [B]@SolarLune, When i load the new scene, all the data referenced to the character class is freed from memory but I keep the variables. So when I make a new instance of the object, the game engine have no reference to its controller and hence, cannot operate on the object raising the error message!

I still have no solution to it just yet. I have read at forum that i should make a class initialization block of code that should make it persistent throughout scene swapping. I tried a feel things but for nor success yet.

Well, I’ll keep looking (never surrender!) for a reasonable solution.
maybe with globalDic, or using the same object as the character spawner, I dont know!

Thank you guys, this has been a good practice!