Game Development Community

Simple workaround for SceneLoader Load/Unload bug

by Tom Ogburn · in Torque X 2D · 04/03/2008 (9:10 pm) · 8 replies

If you have multiple txscene objects and try to Load and Unload them, when you try to load the second it won't show up. There have been other threads that discuss editing the code to recreate the camera, etc when you try to load the second scene. I started to play around with that and decided to see if there was another way. An easier way, and one that doesn't need source access, is to load an empty scene first that you don't unload. Then Load/Unload the other scenes on top of it.

I just created a blank txscene file and deleted everything I thought I could out of it, all the materials and objects. I left the SceneData and camera.

Then in BeginRun you load that one
Game.Instance.SceneLoader.Load(@"data\levels\blank.txscene");

Then Load/Unload the remaining ones as usual during gameplay.
Game.Instance.SceneLoader.Load(@"data\levels\level1.txscene");
//...code here
            Game.Instance.SceneLoader.Unload(@"data\levels\level1.txscene");
//...code here
            Game.Instance.SceneLoader.Load(@"data\levels\level2.txscene");
//...code here
            Game.Instance.SceneLoader.Unload(@"data\levels\level2.txscene");


"blank.txscene" sample
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<!--Torque X Builder - http://www.garagegames.com-->
<!--Type: Scene-->
<!--Target: TorqueX-->
<!--Version: 2-->
<!--Creator: Torque X Builder-->
<TorqueSceneData>
    <Version>1.0</Version>
    <SceneData>
        <ObjectTypeDeclaration>
            <LockTypes>true</LockTypes>
            <ObjectTypes>
            </ObjectTypes>
        </ObjectTypeDeclaration>
        <T2DSceneGraph name="DefaultSceneGraph">
            <T2DSceneContainer>
                <BinSize>20</BinSize>
                <BinCount>256</BinCount>
            </T2DSceneContainer>
        </T2DSceneGraph>
        <Camera2D name="Camera">
            <CenterPosition>
                <X>0</X>
                <Y>0</Y>
            </CenterPosition>
            <Extent>
                <X>100</X>
                <Y>75</Y>
            </Extent>
            <UseCameraWorldLimits>false</UseCameraWorldLimits>
        </Camera2D>
    </SceneData>
    <Materials>
    </Materials>
    <Objects>
    </Objects>
</TorqueSceneData>

--Tom
Starlit Sky Games

#1
10/08/2008 (6:54 pm)
I've implemented it in my code and it indeed brought my next screen up, it did however present another problem.

I have code that moves the camera with the player as the playable area is not viewable at once in my game. This method of loading the scenes actually broke it.

One thing that I've found (it was found in a tutorial) is to just put the below code after the Sceneloader.load() method as shown below. It totally emliminates the issue.


Game.Instance.SceneLoader.Unload(@"data\levels\titleScreen.txscene");

Game.Instance.SceneLoader.Load(@"data\levels\levelData.txscene");


//create a renderable canvas for the scene
GUIStyle stylePlayGui = new GUIStyle();
GUISceneview playGui = new GUISceneview();
playGui.Style = stylePlayGui;

//switch over to the new canvas
GUICanvas.Instance.SetContentControl(playGui);
#2
09/18/2010 (3:31 pm)
Thanks for the workaround -

I just want to mention also something that I encountered which prevented it from working properly the first time. Just for reference, I am using TorqueX version 3.1.5, but this probably isn't version-dependent:

It is absolutely essential that the blank scene "[YourBlankScene'sFilename].txscene" contains the asset definitions, such as materials within the editor's panes, in it, even though it is blank in the sense that it does not place any of those assets in the actual layout area (the grid, the scene world).

The first time I tried this I removed all of the asset definitions, including the materials in the editor's panes, from the blank scene's panes in addition to the actual layout area scene itself. The result was that the C# code would still compile but it would not give the expected run-time results. It resulted in a blank empty screen, no matter how many levels I tried to load, and regardless of whether or not I tried to first unload any scene.

https://sites.google.com/site/webspaceforforumphotos/page1/TorqueQuirkSceneLoadWorkaround.jpg
There is one caveat or disadvantage of using this method, even though it is very fast and almost effortless to implement. If I understand correctly, the blank scene must contain the sum total of all the materials which the subsequent scenes use. So if one subsequent level uses a subset of the assets, then its run-time memory requirement is nonetheless the sum total of all the assets for the entire game. This is potentially unacceptable if the game being created has many assets and insufficient run-time RAM / memory available on the target machine.
#3
09/18/2010 (3:41 pm)
@Neil

I'm not sure if this is still an issue, I have been loading and unloading scenes without any problems.
#4
09/18/2010 (3:46 pm)
@Aaron,

Hi, thanks, but for some reason it was still an issue even with Torque X 3.1.5. I did find a workaround (in post #2 which I edited after you gave your comment in post #3) though.

Are you using the user-modified CEV version of Torque X by any chance? Maybe the CEV version has fixed this issue?

Thanks for your comment.
#5
09/18/2010 (3:57 pm)
Hi Neil, I am using the CEV version.
I will ask Pino and see if this is in fact fixed in the CEV.
#6
09/18/2010 (7:38 pm)
In te CEV there are many fixes not inluded in the official version but I don't recall this one. Actually I miss the point of creating an empty scenefile and honestly I don't really know how that will affect the engine, maybe it's an edge case issue, stull I don't understand why such a scenefile should be loaded.

There are different way to manage the content unloading to avoid memory issues, allof them related to a proper usage of the ContentManager.
#7
09/18/2010 (8:26 pm)
re: post#6

Hi Pino, thanks for your post. I'm not quite sure what you mean by "edge case issue", but basically what happens is that when you try to load a new scene and first unload the previous (currently loaded) scene, then the loaded new scene does not show up, but instead a blank scene is all that shows.

Another strange thing occurs if a person instead skips the process of unloading the first scene before loading the second scene. In that case, the second scene will be loaded, but it will be loaded on top of or intermingled with the first scene which was already loaded. The second scene will not replace the first scene, but rather it becomes superimposed onto the first scene.

https://sites.google.com/site/webspaceforforumphotos/page1/TorqueQuirkDemonstrated.jpg
So that's why loading a blank scene as the initial, first scene is effective, because essentially the second scene is loaded over (not replacing, but rather more like superimposing, intermingling or combining) the first scene. Then then second scene may be safely unloaded, without affecting the assets and materials (which the first scene contains and the second scene also contains). Then a third, fourth, and so scene may be loaded and superimposed onto the first scene.

So the first scene (including its assets) never itself is actually unloaded. For some reason unknown to me, the subsequent scenes load and unload without any problems as long as you never unload the very first scene and as long as that first scene contains the assets which the subsequent scenes use. I guess that maybe the scene assets are somehow loaded by Torque X separately from the scene load function itself. Or perhaps the unload function removes the scene assets and fails to unregister them within the Torque X engine, and then the load function in turn fails to load the assets because it is misinformed that the assets are already loaded when in fact they have been unloaded. If that's the case, then it's a bug in the Torque X engine somewhere.

Regarding managing content, I'm not yet familiar with the ContentManager because I'm just learning Torque X and largely XNA for the first time. But I will keep that in mind for the future to reduce run-time memory requirements. Thanks for the pointer regarding ContentManager!
#8
09/18/2010 (11:23 pm)
In the Community Version of the engine there are changes that make scenes load/unload in a more useful way. In general though, if you are looking to load multiple scenes as overlays or things like that then you should look into the scenegraph, guisceneview, and torque folder classes. Although if you are new to Torque X you will probably just want to keep things simple initially. Perhaps use a small framework such as the gamekit as a starting point.