Reloading/restarting a level
by Mr Zurkon · in iTorque 2D · 09/07/2011 (7:50 pm) · 27 replies
I'm trying to have the game/level restart when the player dies, but I've ran into some problems. When I reload the level (using loadLevel), the loop adding the objects no longer finds them:
It's the line:
Are there any articles/tutorials about how to properly reload a game? How do I stop all the loops I have running and ensure a "clean" restart? I have several loops running in game.cs and I need to make sure they stop at restart.
game/scripts/game.cs (1094): Unable to find object: '1170' attempting to call function 'addToScene'
It's the line:
$MySceneGraph.addToScene(%object);that fails.
Are there any articles/tutorials about how to properly reload a game? How do I stop all the loops I have running and ensure a "clean" restart? I have several loops running in game.cs and I need to make sure they stop at restart.
About the author
#2
It would look something like this...
function endLevelNow()
{
splashWindow2D.loadLevel(splash.t2d);
///the splashLevel2D, would need to be a whole new gui beyong your mainScreen.gui, but exactly the same minus name.
///this splash would load, essentially a visual "block/curtain" to protect against seeing the blackecning and reloading of the game scene.
sceneWindow2D.endlevel();
%this.reloadLevel();
}
function reloadLevel()
{
sceneWindow2D.loadLevel(level1.t2d);
splashWindow2d.endLevel();
///so you see, here we reload the level., and then end the splash level.
}
this should give you a clean, seamless look and allow you to reload a scene.
09/07/2011 (8:38 pm)
One suggestion is to have a splash scene... the way I would do it is to have it as a completely separate window/gui controller from your sceneWindow2D. Upon needing to reload your level, first load splash... then endLevel of the sceneWindow2D(), then loadLevel of the sceneWindow2D.It would look something like this...
function endLevelNow()
{
splashWindow2D.loadLevel(splash.t2d);
///the splashLevel2D, would need to be a whole new gui beyong your mainScreen.gui, but exactly the same minus name.
///this splash would load, essentially a visual "block/curtain" to protect against seeing the blackecning and reloading of the game scene.
sceneWindow2D.endlevel();
%this.reloadLevel();
}
function reloadLevel()
{
sceneWindow2D.loadLevel(level1.t2d);
splashWindow2d.endLevel();
///so you see, here we reload the level., and then end the splash level.
}
this should give you a clean, seamless look and allow you to reload a scene.
#3
What's this "blacking of and reloading of the game scene"? Does it mean you get a black screen between scenegraphs?
09/07/2011 (8:48 pm)
@RennieWhat's this "blacking of and reloading of the game scene"? Does it mean you get a black screen between scenegraphs?
#4
before learning about adding new gui controllers, I would simply run everything thru the one sceneWindow2D. By adding in new gui controllers, I can pop in a splash gui. this allows me to switch levels in discretion. With out this, if I simply...
sceneWindow2D.endLevel();
///then
sceneWindow2D.loadLevel()
Yes, I do get a split second of blackness.
09/07/2011 (8:53 pm)
well, before learning about adding new gui controllers, I would simply run everything thru the one sceneWindow2D. By adding in new gui controllers, I can pop in a splash gui. this allows me to switch levels in discretion. With out this, if I simply...
sceneWindow2D.endLevel();
///then
sceneWindow2D.loadLevel()
Yes, I do get a split second of blackness.
#5
this should not happen when changing levels. I just updated this resource where the functions above are used
Torque Minimal Template -- Part 5. Levels, sound and ML text
you might want to give it a try with your code (there is a download link) just to see if you get the same
To simulate loading many objects to a level I changed this function from
to
it takes more time, but no black screen; the previous scene just stays idle.
09/08/2011 (1:54 pm)
@rennieQuote:Yes, I do get a split second of blackness.
this should not happen when changing levels. I just updated this resource where the functions above are used
Torque Minimal Template -- Part 5. Levels, sound and ML text
you might want to give it a try with your code (there is a download link) just to see if you get the same
To simulate loading many objects to a level I changed this function from
function SceneGraphLevel_01::OnAdd(%this)
{
CreateBackground( %this );
CreateButton( %this, "ButtonHome", "ButtonHomeImageMap", 0, 170, 128, 128);
makeTextObject( %this, "I am in Level 1");
}to
for ( %i = 0; %i < 10000; %i++)
{
CreateBackground( %this );
}it takes more time, but no black screen; the previous scene just stays idle.
#6
what exactly are you doing here? I cant really tell with out analyzing it, and I am too tired right now. Anyhow, beyond that, I like using an extra gui, splash. For me it does two things. One, it prevents the blackness, of same gui end and load level, but also, creates a time barrier allowing the playable scene to not only load visually, but gives it time to get everything up and running. Often I find that if my level has a nice size of scripts running, that even if it is loaded visually, it takes a few seconds before gameplay could be played with out any slowness. I can not detect any slowing down of the engine using a second or third gui so I like it as such.
09/08/2011 (4:59 pm)
Hmm, what exactly are you doing here? I cant really tell with out analyzing it, and I am too tired right now. Anyhow, beyond that, I like using an extra gui, splash. For me it does two things. One, it prevents the blackness, of same gui end and load level, but also, creates a time barrier allowing the playable scene to not only load visually, but gives it time to get everything up and running. Often I find that if my level has a nice size of scripts running, that even if it is loaded visually, it takes a few seconds before gameplay could be played with out any slowness. I can not detect any slowing down of the engine using a second or third gui so I like it as such.
#7
sceneWindow2D.endLevel();
sceneWindow2D.loadLevel();
do but without the "levels" being the typical "t2d" files.
09/08/2011 (5:12 pm)
that code above in #1 basically does what sceneWindow2D.endLevel();
sceneWindow2D.loadLevel();
do but without the "levels" being the typical "t2d" files.
#8
09/08/2011 (6:24 pm)
Tried the code in #1 and ended up with a black screen with the same errors as before (can't find any objects). When ending a level and reloading it, is there a way to reload everything in game.cs? I have some loops I need to restart. How can I do that?
#10
If you look at the very first line of every .t2d file is always
so basically an instantiation of a t2dSceneGraph class, which is exactly what you see above in #1.
an example of use is
where MySceneGraph is a class where you put all our scene objects
The advantage of this is that I can define this MySceneGraph anywhere, and changing levels is *very* easy, just a function call that deletes the previous scene graph instance and associated objects. It also defines he camera *only* here and it is prepared for Universal/Retina.
If you look at the default iTorque template all this is done in a file called levelManagement.cs in a slightly more complex way.
@Mr. Zurkon
The code in #1 is intended to be used *only* as described here. All this is explained here
09/08/2011 (7:53 pm)
@rennieIf you look at the very first line of every .t2d file is always
%levelContent = new t2dSceneGraph() {so basically an instantiation of a t2dSceneGraph class, which is exactly what you see above in #1.
%scenegraph = new t2dSceneGraph( %scenegraph_name )
an example of use is
%scenegraph = new t2dSceneGraph( MySceneGraph );
where MySceneGraph is a class where you put all our scene objects
function MySceneGraph::OnAdd(%this)
{
//create objects here
} The advantage of this is that I can define this MySceneGraph anywhere, and changing levels is *very* easy, just a function call that deletes the previous scene graph instance and associated objects. It also defines he camera *only* here and it is prepared for Universal/Retina.
If you look at the default iTorque template all this is done in a file called levelManagement.cs in a slightly more complex way.
@Mr. Zurkon
The code in #1 is intended to be used *only* as described here. All this is explained here
#11
wow, that is impressive. your retina resource is something i am going to need to go over in the coming months. Just as a heads up tho, retina would involve using 2 images of the same image. one for retina friendly devices and one for non? also, what resolutions (and or other settings) should i be importing my images into the itgb project if i want them to be retina?
09/09/2011 (8:23 am)
@pedro,wow, that is impressive. your retina resource is something i am going to need to go over in the coming months. Just as a heads up tho, retina would involve using 2 images of the same image. one for retina friendly devices and one for non? also, what resolutions (and or other settings) should i be importing my images into the itgb project if i want them to be retina?
#12
That depends. There are 2 different cases:
1) the "Default.png" splash screen, yes, you have to do 3 versions: 320x480, 640x960 and 768x1004 for iPad.
2) all the others, not necessarily. So far, I was able to get away just by doing 1 size with good results.
I have several buttons for interface that I want to display as 64x64 "units" (the resolution of the iPhone screen is in points, not pixels, it's always 320x480 points). So, the images I use are 128x128 pixels.
In the drawing code I call them as 64x64
this assumes that your camera size if always 320x480, which is what the LoadSceneGraph function does.
Supporting High-Resolution Screens
Custom Icon and Image Creation Guidelines
09/09/2011 (9:09 am)
@rennieQuote:retina would involve using 2 images of the same image
That depends. There are 2 different cases:
1) the "Default.png" splash screen, yes, you have to do 3 versions: 320x480, 640x960 and 768x1004 for iPad.
2) all the others, not necessarily. So far, I was able to get away just by doing 1 size with good results.
I have several buttons for interface that I want to display as 64x64 "units" (the resolution of the iPhone screen is in points, not pixels, it's always 320x480 points). So, the images I use are 128x128 pixels.
In the drawing code I call them as 64x64
%obj.setSize( 64, 64 );
this assumes that your camera size if always 320x480, which is what the LoadSceneGraph function does.
Supporting High-Resolution Screens
Custom Icon and Image Creation Guidelines
#13
09/09/2011 (10:28 am)
So, is the image the image and it is just the device itself that defines the clarity (resolution, sharpness) of the image? I am confused.
#14
The best way to see how it works is to try it.
Option 1: Try the above in #12.
Option 2: define 2 images: a 64x64 and a 128x128
in code then something like
if ( $iphone )
display_my_64_sized_image_as_64_with_setSize
else if ( $iphone4 )
display_my_128_sized_image_as_64_with_setSize
It is possible that this Option 2 looks better in some cases. Because in Option 1 we are asking the torque engine (for the iphone case) to scale down an image of 128x128 pixels to a 64x64 pixel size. But so far I have not found yet a case where Option 1 looks worse.
if you do
if ( $iphone4 )
display_my_64_sized_image_as_64_with_setSize
you will see the image in a lower resolution
09/09/2011 (11:09 am)
yes, the device does everything for you. Good news for us programmers. Basically we just have to supply an image twice the size in pixels.The best way to see how it works is to try it.
Option 1: Try the above in #12.
Option 2: define 2 images: a 64x64 and a 128x128
in code then something like
if ( $iphone )
display_my_64_sized_image_as_64_with_setSize
else if ( $iphone4 )
display_my_128_sized_image_as_64_with_setSize
It is possible that this Option 2 looks better in some cases. Because in Option 1 we are asking the torque engine (for the iphone case) to scale down an image of 128x128 pixels to a 64x64 pixel size. But so far I have not found yet a case where Option 1 looks worse.
if you do
if ( $iphone4 )
display_my_64_sized_image_as_64_with_setSize
you will see the image in a lower resolution
#15
09/09/2011 (11:41 am)
So the only part where I am currently unclear is, if I have an image, that needs to display 64x64 (ported in at 64x64 in iTGB, 480x320). If the project is playing in non retina display my image ported in will suffice as is, but if I want to display it in retina display, I must have a second (that will be called by iOS) that is 128x128?
#16
09/09/2011 (12:00 pm)
yes, correct. That is Option 2 above. Or you can just have one image version of 128x128 pixels and always display it as 64x64 for both normal and retina (Option 1). Note that "you don't have to" have that 128x128 version, you can still use the 64x64 pixel version in retina; it's just that the 128x128 version will look like hi-res in the iPhone4 (occupying the same 64x64 space). :-)
#17
And when loaded, in retina display, the image appears to be 64x64, but is the whole scene doubled, in resolution so a 128x128 appears as 64x64?
And, then, again, I have not yet looked at your resource closely, but is it simply (as use that term lightly) just plug the code into the proper scripts and supply the correct imagery?
Also, what do we do in case of 1024x1024 images?
09/09/2011 (1:00 pm)
So would it be safe to say I could just have one image 128x128 for both, this way I can save memory?And when loaded, in retina display, the image appears to be 64x64, but is the whole scene doubled, in resolution so a 128x128 appears as 64x64?
And, then, again, I have not yet looked at your resource closely, but is it simply (as use that term lightly) just plug the code into the proper scripts and supply the correct imagery?
Also, what do we do in case of 1024x1024 images?
#18
09/09/2011 (2:04 pm)
Only thing I would add is that loading the "retina" image and resizing will eat up more memory on the non-retina displays e.g. 3GS, iPod Touch 2G
#19
09/09/2011 (2:46 pm)
@Scott is that simply because, if you go with two images, obvious, twice (probably more) space, but if you go with one, it is automatically twice the size?
#20
09/09/2011 (2:49 pm)
If you detect device type i.e. 3GS or retina then you can control which image is loaded; the smaller size for 3GS or the larger size for retina. This was you use less memory on the 3GS.
Torque 3D Owner Pedro Vicente
Space Research Software LLC
// ..................................................................................... function loadSceneGraph( %scenegraph_name ) { //delete the previous scenegraph deleteSceneGraph(); if ( $pref::iDevice::ScreenOrientation == $iDevice::constant::Landscape ) { if( $platform $= "ipad" || ( $platform $= "windows" && $platform_simul $= "ipad") ) { %scenegraph = new t2dSceneGraph( %scenegraph_name ) { cameraPosition = "0 0"; cameraSize = "1024 768"; }; } else { %scenegraph = new t2dSceneGraph( %scenegraph_name ) { cameraPosition = "0 0"; cameraSize = "480 320"; }; } } else if ( $pref::iDevice::ScreenOrientation == $iDevice::constant::Portrait ) { if( $platform $= "ipad" || ( $platform $= "windows" && $platform_simul $= "ipad") ) { %scenegraph = new t2dSceneGraph( %scenegraph_name ) { cameraPosition = "0 0"; cameraSize = "768 1024"; }; } else { %scenegraph = new t2dSceneGraph( %scenegraph_name ) { cameraPosition = "0 0"; cameraSize = "320 480"; }; } } sceneWindow2D.setSceneGraph(%scenegraph); %scenegraph.performPostInit(); %cameraPosition = sceneWindow2D.getCurrentCameraPosition(); %cameraSize = t2dVectorSub(getWords(sceneWindow2D.getCurrentCameraArea(), 2, 3), getWords(sceneWindow2D.getCurrentCameraArea(), 0, 1)); if (%scenegraph.cameraPosition !$= "") %cameraPosition = %scenegraph.cameraPosition; if (%scenegraph.cameraSize !$= "") %cameraSize = %scenegraph.cameraSize; sceneWindow2D.setCurrentCameraPosition(%cameraPosition, %cameraSize); } // ..................................................................................... function deleteSceneGraph() { %scenegraph = sceneWindow2D.getSceneGraph(); if (isObject(%scenegraph)) { %sceneObjectList = %sceneGraph.getSceneObjectList(); // And finally, notify all the objects that they were loaded. for (%i = 0; %i < getWordCount(%sceneObjectList); %i++) { %sceneObject = getWord(%sceneObjectList, %i); if( %sceneObject.isMethod( "onLevelEnded" ) ) %sceneObject.onLevelEnded(%sceneGraph); } // Notify the scenegraph that the level ended. if( %sceneGraph.isMethod( "onLevelEnded" ) ) %sceneGraph.onLevelEnded(); %scenegraph.clearScene(true); } if (isObject(%scenegraph)) { if( isObject( %scenegraph.getGlobalTileMap() ) ) %scenegraph.getGlobalTileMap().delete(); %scenegraph.delete(); } }