Game Development Community

Changing scenes

by Pedro Vicente · in Torque 2D Beginner · 10/20/2013 (11:51 pm) · 5 replies

Hello everyone

I am trying Torque 2D for the first time, coming from iTorque 1.5.
After reading the excellent Getting started guide,

github.com/GarageGames/Torque2D/wiki/Getting-started-guide#chapter-8-creating-an...

I was able to do a simple project that loads a sprite.

My first question is, how to change to another scene?
Can anyone point to the code in the samples where this is done? I am still catching up with the name changes. It seems most API names changed:-) ? What was in iTorque 1.5 a "SceneGraph", is now a "Scene", I believe.

Thanks

Pedro

#1
10/21/2013 (4:39 am)
It's pretty much the same as before. You need a SceneWindow and a Scene.

// Create the scene window.
new SceneWindow(SandboxWindow);

// Set profile.        
SandboxWindow.Profile = SandboxWindowProfile;
        
// Push the window.
Canvas.setContent( SandboxWindow );

// Set the rest of the properties of the window
...

// Create the scene.
new Scene(SandboxScene);

// Or load it from file
// %scene = TamlRead("pathToScene.taml");

// Set scene to window.
SandboxWindow.setScene( SandboxScene );

// Remove current scene
SandboxScene.delete();

// Switch to new scene
%scene = new Scene(NewScene);

SandboxWindow.setScene(%scene);

Those are the basics. You'll want to swap the above names for your own variables and fill out object properties, of course.
#2
11/01/2013 (4:14 pm)
Thanks, Michael.

I have an issue, when I try to delete the scene inside of an event

here's the complete code

if(!isObject(GuiDefaultProfile)) new GuiControlProfile (GuiDefaultProfile)
{
  Modal = true;
};  


//------------------------------------------------------------------------------
// resetCanvas
// Forces the canvas to redraw itself.
//------------------------------------------------------------------------------
function resetCanvas()
{
  if (isObject(Canvas))
      Canvas.repaint();
}

//-----------------------------------------------------------------------------

function games::create( %this )
{
  createCanvas("window_name");  
  setScreenMode( 320, 480, 32, false );

  // 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);
  Canvas.setContent(mainWindow);

  // Finally, connect our scene into the viewport (or sceneWindow).
  // Note that a viewport comes with a camera built-in.
  mainWindow.setCameraPosition( 0, 0 );
  mainWindow.setCameraSize( 320, 480 );
  mainWindow.setUseObjectInputEvents(true);

  %this.createScene(mainScene);
  %this.createBackground(mainScene);
  %this.createButton(mainScene,"MainMenuButtonRun", "games:set_01_button_start_128", 0,  0, 128, 128);

}

//-----------------------------------------------------------------------------

function games::destroy( %this )
{
  %this.deleteScene();
}

//-----------------------------------------------------------------------------

function games::createScene(%this, %scene_obj)
{
  %this.deleteScene();
  new Scene(%scene_obj);
  mainWindow.setScene(%scene_obj);
}

//-----------------------------------------------------------------------------

function games::deleteScene(%this)
{  
  %scene_obj = mainWindow.getScene();
  
  if (isObject(%scene_obj))
  {
    %scene_obj.delete();
  }
}

//-----------------------------------------------------------------------------

function games::createBackground(%this, %scene_obj)
{
  // Create the sprite.
  %background = new Sprite();

  // Set the sprite as "static" so it is not affected by gravity.
  %background.setBodyType( static );

  // Set the object's center to coordinates 0 0 which corresponds to the center of the Scene
  // Remember that our camera is set to point to coordinates 0 0 as well
  %background.Position = "0 0";

  // Set the object's size. Notice that this corresponds to the size of our camera, which was created in
  // scenewindow.cs. The background will thus cover the entirety of our scenewindow.
  %background.Size = "320 480";

  // Set to the furthest background layer.
  %background.SceneLayer = 31;

  // Sets the image to use for our background
  %background.Image = "games:set_01_bk_wood_iphone";

  %background.setDefaultRestitution(1);

  // Add the sprite to the scene.
  %scene_obj.add( %background );   
}

//-----------------------------------------------------------------------------

function games::createButton(%this, %scene_obj, %class, %image_map, %xpos, %ypos, %xsize, %ysize )
{
  %obj = new Sprite() 
  { 
    Scene = %scene_obj;
    class = %class;
    UseInputEvents = "1";
  };
  %obj.setVisible( true );
  %obj.setImage( %image_map );
  %obj.setPosition( %xpos, %ypos );	
  %obj.setSize( %xsize, %ysize );     
  %obj.setSceneLayer( 1 );
  return %obj;
}


//-----------------------------------------------------------------------------

function MainMenuButtonRun::OnAdd( %this )
{ 
  %this.setUseInputEvents(true);
}

//-----------------------------------------------------------------------------

function MainMenuButtonRun::onTouchUp( %this, %touchId, %worldposition )
{ 
  %this.schedule(0, "LoadLevelGame" );  
}

//-----------------------------------------------------------------------------

function MainMenuButtonRun::LoadLevelGame(%this)
{
  games.createScene(gameScene);
}

This code creates a "button", that is a sprite that responds to mouse events.
When the event is triggered, this function is called

function MainMenuButtonRun::LoadLevelGame(%this)
{
  games.createScene(gameScene);
}

And the error I have is this assertion

void SimObject::deleteObject()
{
    // Sanity!
    AssertISV( getScriptCallbackGuard() == 0, "SimObject::deleteObject: Object is being deleted whilst performing a script callback!" );

It seems that in iTorque 1.5 this was OK, to delete objects inside a callback? But not anymore?




#3
11/02/2013 (11:27 pm)
@Michael

I changed the call from delete() to clear(false) and no assertions on the engine, is this the way to go?

function games::clearScene(%this)
{  
  %scene_obj = mainWindow.getScene();
  if (isObject(%scene_obj))
  {
    %scene_obj.clear(false); //false: do not delete objects, otherwise engine asserts when called on a callback
  }
}
#4
11/03/2013 (4:40 am)
I had something similar a while ago, you just have to schedule the clear() or delete(), like this:

%scene.schedule(32,"clear");

and the error will stop.
#5
11/03/2013 (7:00 pm)
I use the EventManager object to handle stuff like this - you post the delete request event and the event manager then deletes the object at a later time, preventing the issue with trying to delete an object in its own callback.