Level Management/Loading
by Brandon Fenty · in Torque 2D Beginner · 04/07/2014 (11:18 pm) · 4 replies
I've created a scene, loaded it, and it looks great and functions. I have a sprite that I've added a control behavior to that allows me to move him around the screen. This is great, for a single level-but as I'm trying to build a framework for an RPG, I want the functionality to walk into houses/buildings/other map areas. I've written a function that, on collision, deletes the current scene window and then reinitializes another one with the same settings (only loading different objects/backgrounds). I'm calling the EXACT same functions to spawn my hero etc. However, when the second 'level' loads, it's got everything there but I'm not able to control my hero at all, as if the controls behavior did not bind correctly. I'm assuming this is because I've destroyed and recreated my scene without cleaning something up properly-or maybe I'm not managing levels in a way that makes sense? Here's relevant code below:
Here's the function to create the scenewindow:
and to spawn the hero:
and to rebuild the scene again (note that it calls the above two functions, exactly what it does to load the original level, only difference is I'm destroying the old scene first here):
Any idea what I'm missing?
Here's the function to create the scenewindow:
function RPGGame::init( %this )
{
// We need a main "Scene" we can use as our game world. The place where sceneObjects play.
// Give it a global name "mainScene" since we may want to access it directly in our scripts.
%scene = new Scene(mainScene);
// Without a system window or "Canvas", we can't see or interact with our scene.
// AppCore initialized the Canvas already
// Now that we have a Canvas, we need a viewport into the scene.
// Give it a global name "mainWindow" since we may want to access it directly in our scripts.
new SceneWindow(mainWindow);
mainWindow.profile = new GuiControlProfile();
Canvas.setContent(mainWindow);
// Finally, connect our scene into the viewport (or sceneWindow).
// Note that a viewport comes with a camera built-in.
mainWindow.setScene(mainScene);
mainWindow.setCameraPosition( 0, 0 );
mainWindow.setCameraSize( 100, 75 );
mainScene.setDebugOn( "aabb" );
//mainWindow.setCameraSize(200, 150);
}and to spawn the hero:
function RPGGame::spawnHero( %this )
{
%anim = "RPGAssets:heroFrontStandAnim";
%size = "5 5";
%hero = new Sprite()
{
Animation = %anim;
class = "Hero";
position = "0 0";
size = %size;
SceneLayer = "15";
SceneGroup = "15";
minSpeed = "5";
maxSpeed = "15";
CollisionCallback = true;
};
%hero.setbodytype(dynamic);
%hero.FixedAngle = true;
%hero.heading = "south";
%hero.createPolygonBoxCollisionShape(%size);
%hero.setCollisionShapeIsSensor(0, false);
%hero.setCollisionGroups( "15");
//Set up Controls
%controls = RPGControlsBehavior.createInstance();
%hero.addBehavior(%controls);
mainScene.add( %hero );
mainWindow.mount(%hero,"0 0", 10, false, 0);
}and to rebuild the scene again (note that it calls the above two functions, exactly what it does to load the original level, only difference is I'm destroying the old scene first here):
function buildLevelTwo()
{
destroySceneWindow();
RPGGame.init(%this);
RPGGame.spawnHero();
addBackdrop( "RPGAssets:houseInterior", %scene );
addAquariumBoundaries(mainScene, 80, 55);
}Any idea what I'm missing?
#2
Ok so I need to delete the scene and not the scene window. Got it-but I was thinking about it after I posted, won't that delete all character stats etc? How would I permanently store data like that? While I'm at it, what about saving the game to load later?
04/08/2014 (6:42 am)
You're exactly right on the bind obj situation. Ok so I need to delete the scene and not the scene window. Got it-but I was thinking about it after I posted, won't that delete all character stats etc? How would I permanently store data like that? While I'm at it, what about saving the game to load later?
#3
garagegames.github.io/Torque2D/TorqueScriptDocs/html/classSimSet.html
For objects like the hero that you wish to carry over between levels, you could simply disable the Sprite, change its position or any other property, then re-enable it when everything in the next level is loaded and ready. No need to delete and recreate.
There's many ways you can go about saving data in T2D, I personally would choose to use the TAML system.
github.com/GarageGames/Torque2D/wiki/Taml-Guide
Through that, you can save individual objects or even entire Scenes to an XML, JSON, or binary file. The guide covers how to write things out and then read them back in.
04/08/2014 (7:09 am)
Well, technically you don't have to delete the entire Scene either. You could store a majority of the level 1 objects that you don't need in level 2 in a SimSet, then delete the contents of the SimSet before loading level 2.garagegames.github.io/Torque2D/TorqueScriptDocs/html/classSimSet.html
For objects like the hero that you wish to carry over between levels, you could simply disable the Sprite, change its position or any other property, then re-enable it when everything in the next level is loaded and ready. No need to delete and recreate.
There's many ways you can go about saving data in T2D, I personally would choose to use the TAML system.
github.com/GarageGames/Torque2D/wiki/Taml-Guide
Through that, you can save individual objects or even entire Scenes to an XML, JSON, or binary file. The guide covers how to write things out and then read them back in.
#4
04/08/2014 (12:49 pm)
Thanks! You've given me some good reading to do.
Associate Mike Lilligreen
Retired T2Der
I know you posted a movement behavior in a previous thread, so I'm guessing you are using bindObj to bind your keys? Perhaps you did not delete the behavior instance so the keybindings are still trying to access the behavior attached to the hero from level 1.
In general, I don't think it is necessary to delete the SceneWindow every time you wish to change a level. It is enough to delete the existing Scene and load a new Scene. The Sandbox is a practical example of this concept.