Game Development Community

Reference Object on loadScene

by Aditya Kulkarni · in Torque Game Builder · 12/08/2009 (6:30 am) · 17 replies

saves a scene:
level.saveScene("~/data/levels/testScene9.scn");


loads a scene:
level.loadScene("~/data/levels/testScene9.scn");

how am i supposed to reference the objects after i load the scene again? the objectid it was using previously doesn't exist on loadScene...

also, this doesn't save schedules, nor the values of textObjects...

#1
12/08/2009 (6:45 am)
If you give the objects unique names (as in, "new t2dStaticSprite(PlayerCar);"), you'll be able to access them by that name.

But as for schedules, random objects, and values, you'll have to write your own save/load routine.
#2
12/08/2009 (7:06 am)
cool...

can i still access those using '%this'?
#3
12/08/2009 (7:12 am)
i ask this because all my onMouseDown events fail to work after loading the .scn file...
#4
12/08/2009 (9:32 am)
just can't get this to work...

i set a name for every object in the level editor...
now, i echoed out all the objects in the scenegraph after 'loadScene' is called and guess what? none of the object have names!

#5
12/09/2009 (4:06 am)
I notice in TGB/tools/levelEditor/scripts/levelManagement.ed.cs, in the function "T2DProject::doSave", that they save the level with the following code:

// Save the Level
if( isWriteableFileName( %this.currentLevelFile ) )
{
   %fo = new FileObject();
   if( %fo.openForWrite(%this.currentLevelFile) )
   {
      %fo.writeObject(%scenegraph, "%levelContent = ");
      %fo.close();
   }
   %fo.delete();
}

It might be something to try next.
#6
12/18/2009 (1:00 am)
awright this worked...i saved everything as level0.scn

loading the contents as follows:

exec("level0.scn");
SceneWindow2D.setSceneGraph(%levelContent);

thanks william!
#7
12/18/2009 (3:06 am)
Great to hear!
#8
08/06/2010 (10:45 am)
Hi Aditya kulkarni,lately i get a problem like yours too.

i want to savescene for all the obj in my scene,but when i loadscene,i found all the dynamic fields r gone. idk whether i havent done something necessary before savescene or something else.pls tell me how to solve the problem.



#9
08/06/2010 (10:59 am)
Make sure canSaveDynamicFields = 1 for your objects.

E.g.
new t2dAnimatedSprite(%animationName) {
    imageMap = "animation1";
    frame = "0";
    canSaveDynamicFields = "1";
    Position = "0 0";
    size = "4 4";
    mountID = "3";
    class = waterbody;
  };
#10
08/06/2010 (12:39 pm)
yes,I check that" canSaveDynamicFields =1 ",but still I reload my scene and the DynamicFields all gone

#11
08/06/2010 (12:49 pm)
obvious i know, but did you 'saveScene' again before loading it?
#12
08/06/2010 (1:13 pm)
yeah i saved before loading
#13
08/06/2010 (1:40 pm)
Aditya,i see this thread u r using this method , is this neccesary? if yes ,how to use it?

"%this.currentLevelFile" what is "%this", T2Dproject object? how can i get the handle?

1. // Save the Level
2. if( isWriteableFileName( %this.currentLevelFile ) )
3. {
4. %fo = new FileObject();
5. if( %fo.openForWrite(%this.currentLevelFile) )
6. {
7. %fo.writeObject(%scenegraph, "%levelContent = ");
8. %fo.close();
9. }
10. %fo.delete();
11. }
#14
08/06/2010 (1:51 pm)
in the function William found, T2DProject...i use

%path = expandFilename("~/data/levels/saves/level.scn");
isWriteableFileName(%path);
#15
08/08/2010 (10:40 am)
oh thx man finally i get it work

BTW william's way to save the scn,actually is a".cs" file.

this method is better than the original"save/loadscene"

cool thx again! :)
#16
08/08/2011 (3:03 pm)
My turn to have trouble with this now. :P

I just did a "t2dSceneGraph.saveScene" and got a binary file. I can't figure out where to call "t2dSceneGraph.loadScene" without crashing. I think this is because it is trying to delete the current scene from within itself.

So if I follow this thread, these are the takeaways, yes?

* do not use the saveScene/loadScene functions
* roll your own scene saving function and use myFileObject.writeObject(mySceneGraph, "%myScene = ")
* using writeObject will write a cs file which is text based
* load it back in with exec(mySceneFile) and then
* set your window2D.setScenegraph(%myScene);

Finally, you will have to write more code and save off schedules or global variables.

Does that sound correct?
#17
08/08/2011 (6:17 pm)
While I assume I won't be using saveScene and loadScene, I wanted to record this here. For the record, I just did a test like so. I named my scene graph "theArena":

%fileName = "~/hello.cs";
   theArena.saveScene(%fileName);
   theArena.schedule(0, clearScene, true);
   %longFileName = expandFilename(%fileName);
   theArena.schedule(0, loadScene, %longFileName);

Note that I could save the file with the shorthand "~/hello.cs" but I needed the expanded file name to load it. Also, I scheduled the "destructive" calls clearScene and loadScene because I was calling them from a mouse event and it would crash if the object I clicked on was in the callstack when the clearing and loading happened.

The result was that the scene did load back in, but the objects where all zombies. They were all there and they moved the way they were moving when I saved, but no collision detections, no mouse events, nothin'. (Of course, no schedules as I expected.) It seems they were also missing their names, since I attempted to echo a few fields with no success (simplerShooter.getID()).

Hmmm.