Game Development Community

Loading a new Level

by Brian Wilmeth · in Torque Game Builder · 02/20/2006 (11:28 am) · 6 replies

Ok I'm getting the hang of this torque thing. The next step? LOADING NEW LEVELS..

I have an exit setup in my first level but what I need to know is..

When I reach my exit

HOW DO I DELETE THE OLD LEVEL AND LOAD UP A NEW ONE. Such that there are no memory leaks..

Simple simple simple yes :)

Anyone out there no how to handle this?

#1
02/23/2006 (7:58 am)
There are many way to handel this, one way is to make a script object which will manage all the objects in the scene here is an example

new scriptObject(gameLevel){ class = "levelManager";};

function levelManager::onAdd(%this){
 //this is called when an object of class levelManager is created
//I would make all the level objects and have them get tied to this object
 %this.SpriteSet = new Simset();
//make a sprite and add to the set
//I like to make sprites a scriptobject too 
%sprite = new scriptObject() { class = "scriptedSprite");
// I put the actual 2td sprite object as a property of the scripted sprite
%sprite.sprite = new t2dStaticSprite(){};
%this.SpriteSet.add(%sprite);
}

function levelManager::onRemove(%this){
//this is called when gameLevel.delete(); is called
//thing one can do would be to remove all the object in the sets and the set them selves
//for instance make a method to handel cleaning up the sprites
 %this.removeSprites(%this.Spriteset);
//then remove the set
%this.Spriteset.delete();
}

function levelManager::removeSprites(%this, %set){
    for(%i = 0; %i < %set.getCount(); %i++ ){
                 %obj = %set.getObject(%i);
                 %obj.sprite.safeDelete();
    }
}

I hope this helps
#2
02/26/2006 (5:24 am)
I can see why you might selectively delete all the objects in a set, or all the objects in a given layer, while preserving you player sprite, for example. But I'm curious why you would keep a separate list of everything in your level, if they're already contained by a sceneGraph?

Related to this, how much can you do using a sceneGraph as the container for all your level data? Why would a levelmanager be needed at all if you can just put everything into a sceneGraph, and use - for example - clearScene to clear the scene?

Isn't that how the Level Editor works? Doesn't it just let you build and then save out a scenegraph?

I'm especially curious, because for our game - which will have its own level editor - I was planning to try and encapsulate everything in the scenegraph and just use saveScene/loadScene.
#3
02/26/2006 (5:47 am)
Scott,

I guess an answer would be that the "t2dSceneGraph" is designed to contain only t2dSceneObject-based objects and not other non-scene resources such as ScriptObjects, sounds, FileObjects etc.

Of course, it's actually quite a neat idea to have an extension to the t2dSceneGraph that allows you to add any SimObject to it and when you issue a "clearScene()" it removes the objects. If you pass "true" (which indicates to destroy the objects as well) it destroys them.

- Melv.
#4
02/26/2006 (8:31 am)
Thanks for the detailed explanation, I'll probably use something like that when my game gets more complicated..

For now I have the function

function createLevel()
{
$gravity = 700;
$spawnPoint = "-20 -30";

%tileMap = new t2dTileMap()
{
scenegraph = t2dscene;
};

%tileMap.loadTileMap("~/data/tilemaps/areaFirst.tile");
%layer = %tileMap.getTileLayer(0);
%layer.setCollisionActive(false, true);
%layer.setGraphGroup($platformGroup);
%layer.setLayer($platformLayer);
%layer.setPosition("0 0");

//$camera.setLimits(%layer.getArea());

resetPlayer($spawnPoint);
}

I simply want to know how to delete this level when the time comes.
Do I have to make %tileMap a global and do %tileMap.delete().. I have a feeling this isn't the best way.
#5
02/26/2006 (8:44 am)
$tileMap.delete()
#6
03/19/2006 (8:51 pm)
Thanks, Melv.

Hmm, but if I understand correctly, you could make a sceneObject which is contained in the scene only as a place for data storage, and the behavior you're describing would apply to it.

In other words, if this sceneObject has a property assigned to it which is a SimSet, will saving the scene save the SimSet, and will it be restored on load? It seems like it should be possible, unless such objects are only stored as reference numbers... or names.

As I get into the work of loading and saving levels versus games it seems like a bad idea to store the actual game state in an object in the scene. After all, nobody wants the score to be reset every time they load a level! On the other hand, it's no sweat to store this data globally in-game, copy it into a sceneObject just before saving the scene... and copy it back after loading the savegame file.

Then the only difference between load/save level and load/save game is whether or not this extra object is used.