Game Development Community

ScriptObject player

by Adam Johnston · in Torque Game Builder · 10/20/2005 (3:49 pm) · 15 replies

Hello

I'm trying to create an object that encapsulates the player
logic. And this object must to have a Update() member that
I plan to call in Scene::Update()


Until now my attempts have been wrong, because
in the update loop the object don't recognize the
Update() member.

Some ideas?

#1
10/20/2005 (4:02 pm)
Have you created the Scene object in the engine?

--
Hans
#2
10/20/2005 (4:14 pm)
Can you post a sample of the code that isn't working and how you are creating an object to encapsulate the player logic. Torque script isn't object oriented and doesn't really have a concept of a class and member functions. Of course I could be completely misunderstanding what you are trying to do.

--Tom
#3
10/20/2005 (8:17 pm)
I use something like this:

function Player::update( %this )
{
	// .. update Player
}

function setupT2DScene()
{
	// .. other setup coe ...

	$player = new ScriptObject("Player") ;
}

function fxSceneGraph2D::onUpdateScene(%this)
{
	$player.update( ) ;
}
#4
10/20/2005 (8:43 pm)
@James, that code is fine- I threw it into my current project and put in a

function Player::update( %this )
{
   echo("Player::update" @ getRealtime());
}

and the function is indeed called. Try running it in a debugger, or start putting in echo(), warn() or error() so you can see stuff printed to the console.

Maybe you are overriding fxSceneGraph2D::onUpdateScene() somewhere else in your project?
#5
10/20/2005 (9:00 pm)
Heh, I wasn't the original poster - I was just posting a sample to show Adam how I do it :-)
#6
10/20/2005 (9:37 pm)
DOH - I didn't read that thread very carefully
#7
10/21/2005 (10:12 am)
Thank you very much guys
I'm using;

new ScriptObject (chaIma)
{
	health = 100;
};

function chaIma::Create ()
{
	%entity = new ScriptObject() { class = chaIma; };
	
	%entity.gpx = new fxAnimatedSprite2D() { scenegraph = t2dSceneGraph; };
	%entity.gpx.setPosition ("-25 0");
	%entity.gpx.setSize ("16 16");

	// animation
	%entity.gpx.playAnimation (aniIma_Start);

	// set player's collision info

	return (%entity);
}

and

function chaIma::Update (%this)
{
	echo("update");
}

and

function StartGame ()
{
	alxPlay (readyAudio);

	$ge_player = chaIma::Create ();
}

// callback de escena de juego
function fxSceneGraph2D::onUpdateScene (%this)
{
   if (%this == t2dSceneGraph.getID ())
   {
	if (isObject ($ge_player))
	{
		$ge_player.Update();
	}
   }
}
#8
10/21/2005 (3:50 pm)
I'm not an expert on TorqueScript, but...

As Tom noted, TorqueScript isn't really OO. This code doesn't do anything as far as I know:
new ScriptObject (chaIma)
{
   health = 100;
};
I'm guessing that you are trying to define the class "chaIma" with member variable "health". However, this actually creates an OBJECT with no name and no way to access it.

"functionStartGame ()" should be "function StartGame()".

Check your console for errors. Put some echo() statements in different blocks of code and see what is actually being executed.
#9
10/21/2005 (3:57 pm)
Hi James

the OOPness of torquescript wonder my imagination :)

actually I've changed a lot of things, but
I believe that I could access the "health" member
of the object...

I'm going to check
#10
10/21/2005 (4:09 pm)
I am pretty sure this will work fine:
new ScriptObject (chaIma){   health = 100;};
echo("health = " @ chaIma.health );
should print health = 100

It's really hard to understand the OOPness (or non-OOPness of torquescript). On the plus side, torquescript is extremely dynamic and forgiving, and eventually you will settle into a scripting style that works for you ( i feel that I have )
#11
10/21/2005 (8:16 pm)
Oops, Sorry! Yeah.. I don't understand that. Just when I thought I was beginning to understand torquescript.

...

Ok, I just tested this out. But I still don't really understand what it does. E.g. if you do this:
new ScriptObject (chaIma){   health = 100;};
echo( chaIma.health ) ; // this prints 100 like you said

$testObject = new ScriptObject( chaIma ) ;
echo( $testObject.health ) ; // this doesn't print anything

I don't know. I just use onAdd as my constructor. And I have managed to get something resembling inheritance to work. I've seen a few different styles in code examples so I guess it's like you said, you'll settle into a style that works for you. But I still like to have an idea of what's going on. :-)
#12
10/21/2005 (8:34 pm)
The reason
$testObject = new ScriptObject( chaIma ) ;
echo( $testObject.health ) ;
doesn't print anything is because you didn't set the "health" variable.

This would be correct:
$testObject = new ScriptObject( chaIma ) { health = 100; };
echo( $testObject.health ) ;

You should also realize that when you do that, you're using up un-needed memory by using a global variable pointing to the script object when it already has a name that you can access anywhere.
#13
10/21/2005 (8:55 pm)
@James- were you trying to use new ScriptObject( chaIma ) as a kind of copy constructor, or to get testobject to be an instance of the 1st chaIma? It doesn't work that way although the torquescript docs might suggest otherwise.

If $testObject was handle 1234, say, then the previous "new" object was 1233. Both of these will return "chaIma":
1233.getName();
1234.getName();
if you are trying to get inheritance in torquescript, you have to experiment until it works. Inspecting the namespace path is helpful too. You can do it by calling an undefined function and looking at the console. There you will see output like
Quote:T2D/client/test.cs (0): Unknown command dummyFunction.
Object ParentThing(1244) ParentThing -> ScriptObject -> SimObject
The namespace path can help you figure out what's actually going on. The *name* of the object gets appended to the namespace path. So by setting the object's name, it's class, and it's superclass, you can hopefully get the effect you want from torquescript.
#14
10/21/2005 (9:42 pm)
Ohhhh... Ok. I think I get it.

I didn't reallise that the name that you pass to ScriptObject() is actually the object's name. I was using it as the class name instead. Which works because it gets appended to the namespace path. So it is the object's name AND part of the namespace path at the same time!

Then I was using the class variable as the superclass. Then I could use superClass variable for an extra layer. That's the "3 levels of inheritance" that I have heard people talk about. Which I understood.. I just didn't reallize that the one is actually the objects name too.

Is there a difference between global variables (using $) and the object names (which can be accessed globally)?

Thanks!
#15
10/21/2005 (9:50 pm)
The $testObject contains the handle (integer) not the name. I tend to use the name reference if it's a unique thing like MyGame.whatever or FunkyLevelX.whatever. But handles are useful for stuffing in variables and calling objects that way - if the name is not known or not convenient.