Game Development Community

problems drawing t2dStaticSprite after level change

by Joseph Bosch · in Torque Game Builder · 08/11/2012 (8:03 am) · 2 replies

I have a weird problem drawing t2dStaticSprites after a level change were basically it can take up to one minute for the game to render properly

so I have my function to draw up to 10 objects

function drawScene(%char, %index)
{
	
	if (!isObject($sChar[%index]))
	{
		echo("new char");
        %x = getWord(%char.Position, 0);
	%y = getWord(%char.Position, 1);
       echo ("x = " @ %x);
       echo ("y = " @ %y);
		$sChar[%index] = new t2dStaticSprite(char @ %index)
		{
		  scenegraph = sceneWindow2d.getSceneGraph();
		  imageMap = "charImageMap";
		  frame = "0";
		  useSourceRect = "0";
		  sourceRect = "0 0 0 0";
		  canSaveDynamicFields = "1";
		  Position = %char.Position;
		  size = "15.000 14.875";
		  Layer = "1";
		  Visible = "1";
			 mountID = "2";
			 PID = %index;
			 isSelectable = 0;
		  _behavior0 = "mouseEvent";
		  };
	}
		else
		{
			$sChar[%index].Position = %char.Position;
		}

this renders properly the first time the level is loaded and all is good and the positions update as I would expect them too, echo statements show valid x y cords

I change levels and do some stuff (everything is still fine) then have the player return to the first level
//level change uses the exact same code as when the level is first loaded
deleteVariables( "$sChar*" );
        exec("~/gui/mainScreen.gui");
	Canvas.clear();

	Canvas.setContent(mainScreenGui);
	Canvas.setCursor(DefaultCursor);
	sceneWindow2D.loadLevel("game/data/levels/untitled.t2d");
        addstar();
(echo testing shows $schar index is deleted)
and then after some server client commands to get current information about the level, the render function is called again, the only problem is the game does not render any of the sChars, if I sit and wait long enough (some time up to a full minute) the scene will suddenly render properly, ANY attempt to change levels or repeat the cycle before the screen renders causes ghost images of the first level.

oddly other things in the level draw normally such as

function addStar()
{

		%tempStar = new t2dStaticSprite() {
		  scenegraph = sceneWindow2d.getSceneGraph();
		  imageMap = "starImageMap";
		  frame = "0";
		  useSourceRect = "0";
		  sourceRect = "0 0 0 0";
		  canSaveDynamicFields = "1";
		  Position = "0 0";
		  size = "15.000 14.875";
			 mountID = "1";
		  }
		  schedule (100, 0, "addStarMap");
		 
		 
}

I have tried everything I can think of from deleting every object from the scene graph to loading in a completely different level and gui to using canvas.reset() and canvas.repaint() nothing seems to help. further more I conducted some proof of concepts in a different project using similar code but on a smaller scale and everything works fine. If any one has any ideas I could really use the help as I have hit a wall on this one.

#1
08/11/2012 (12:28 pm)
I think the issue may be that deleting the variables is not the same thing as deleting the objects they reference. I'm guessing:

deleteVariables( "$sChar*" );

may be your problem.

Trying doing a loop through $sChar[%index] and delete each item in it individually instead. Each iteration should call something like $sChar[%index].safeDelete() or $sChar[%index].delete() .
#2
08/12/2012 (10:33 am)
I figured out what was happening, there were so many client net work commands in the second level that it had not finished dealing with everything before loading the current level, I will have to make a new post about that.