Game Development Community

Dynamic creation of t2dAnimatedSpirte

by Karl Jahnke · in Torque Game Builder · 11/05/2007 (1:06 pm) · 5 replies

Howdy again,

I am trying to create an animated sprite from a function call, but it is not showing up.

function t2dscenegraph::onLevelLoaded()
{
	echo("LOADED");
	createImage();
}

function createImage()
{
	
	%TestAnimation = new t2dAnimatedSprite()
   {
      sceneGraph = %this;
      class = "TestAnimation";
      layer = 0;
      size = "10 10";
      
   };
   
   %TestAnimation.playAnimation("TestAn");
   %TestAnimation.setPositionX(0);
   %TestAnimation.setPositionY(0);
   echo("TestAnimation createImage Function Called");
   
	
	
}

I have created the animation if the level builder called "TestAn". Most of that code is ripped from the Mole Tutorial. I thought I understood it, but if someone could spread some light on to why my "TestAn" animation isnt showing up in the level, that would be totally awesome.
Thanks people for the help!

#1
11/05/2007 (1:23 pm)
function createImage()
{
	
	%TestAnimation = new t2dAnimatedSprite()
   {
      sceneGraph = %this;

You are using a script function (createImage() with no namespace), which does not have a %this parameter sent to it, yet are referencing "%this" when you attempt to assign a sceneGraph. Since in this scope, %this hasn't been defined, it has a value of "", which effectively is setting your sceneGraph to nothing for this object.

You need to provide a valid sceneGraph for the object to be placed within.
#2
11/05/2007 (1:43 pm)
Thank you for the quick replay!

I got it to work, but I think i need a little better understanding of "namespace".
function t2dscenegraph::onLevelLoaded(%this)
{
	
	echo("LOADED");
	createImage(%this);
}

calls the global function onLevelLoaded() and gives its "namespace" %this?
it then passes the %this in to the function createImage()?
so, now, i have 2 things called %this. my scenegraph (i think of this as a camera), and my homemade function createImage().

function createImage(%this)
{
	
	%TestAnimation = new t2dAnimatedSprite()
   {
      sceneGraph = %this;
      class = "TestAnimation";
      layer = 0;
      size = "10 10";
      
   };
   
   %TestAnimation.playAnimation("TestAn");
   %TestAnimation.setPositionX(0);
   %TestAnimation.setPositionY(0);
   echo("TestAnimation createImage Function Called");
   
	
	
}

taking the createImage() %this from the onLevelLoaded() function and passing it into the createImage() call function now gives the %this that I was calling in

sceneGraph = %this

the same namespace as my orginal t2dscenegraph::onLevelLoaded()

Am I correct in those assumptions?
I really thank you people for all your help!!!
#3
11/05/2007 (2:10 pm)
You are correct in your first statement, but as soon as you call a global script funtion (your "createImage()"), you no longer have a namespace--you are calling a global function.

For the automatic passing of the objectID of the calling object as the first parameter to the receiving function, you need to use a namespace method (also known as a script method), instead of a script function:

function [b]t2dSceneGraph[/b]::createImage([b]%this[/b])
{
	
	%TestAnimation = new t2dAnimatedSprite()
   {
      sceneGraph = %this;
      class = "TestAnimation";
      layer = 0;
      size = "10 10";
      
   };
   
   %TestAnimation.playAnimation("TestAn");
   %TestAnimation.setPositionX(0);
   %TestAnimation.setPositionY(0);
   echo("TestAnimation createImage Function Called");
   
	
	
}

See the difference in how the function is written?

And to call this type of method, you call it on the object itself:

%this.createImage();

The difference is merely in syntax--what you wrote and what I wrote are effectively the same. However, good programming practices indicate that you should use script methods (as opposed to script functions) whenever appropriate--otherwise you will have literally hundreds of global functions, executable by any area of your code, when it's not appropriate.
#4
11/05/2007 (2:49 pm)
Thank you Stephen. I understand the global function that you were talking about. %local vs $global was kinda making me screwy, and i had an idea that my 2nd assumption was wrong. (doesnt %local get erased when the function ends?! )

And again thanks for the expanded discussion on Method calls. I am still struggling with hiearchal namespaces like Class vs ObjectID. I come from 3D charchter animation and think about the stuff in a parent/child hiearchy like the bones that make up the char, and how those move. It applies here, kinda.

But lets leave Methods and ObjectID/Class discusion for another post. And for a third time, thanks! You gave me alot better understanding of global function calling.
#5
11/06/2007 (9:03 am)
Karl, a better way to assign the scenegraph would be:
%TestAnimation = new t2dAnimatedSprite()   
{      
   sceneGraph = sceneWindow2D.getSceneGraph();
   class = "TestAnimation";      
   layer = 0;      
   size = "10 10";         
};
sceneWindow2D is defined in mainScreen.gui, and t2dSceneWindow objects have this handly console method available for you to use.