Game Development Community

OOP trouble

by Christian Stock · in Torque Game Builder · 03/22/2006 (5:25 pm) · 24 replies

I have defined an object with create function:

new t2dStaticSprite( CPlayer )
{
class = Player;
scenegraph = t2dScene;

name = "";
};

function Player::create( %this, %name, %nation )
{
%this.name = %name;
}

The problem I have is if I do this:

%this.player = new t2dStaticSprite( : CPlayer );
%this.player.create( "name", 0 );

%this.player doesn't know about create, which puzzles me a bit. If I change the player object to be a ScriptObject and then make the sprite an inner object, everything works fine, ie:

new ScriptObject( CPlayer )
{
class = Player;

sprite = 0;
};

function Player::create( %this, %name, %nation )
{
%this.name = %name;
%this.sprite = new t2dStaticSprite() { scenegraph = t2dScene; };
}

%this.player = new ScriptObject( : CPlayer );
%this.player.create( "name", 0 );

This works, but is a bit more programming hassle since I now have to deal with the sprite object (and expose some functions). This is not a big deal, but I'd like to understand why A (t2dStaticSprite) doesn't work and B (ScriptObject) works.

Christian
Page «Previous 1 2
#1
03/22/2006 (5:33 pm)
Because namespacing at the moment only works for ScriptObjects ...
There is somewhere another thread on OOP and TorqueScript where a hack is presented, that helps out, when I remember correctly
#2
03/22/2006 (5:35 pm)
Thanks, I'll have a browse
#3
03/23/2006 (7:03 am)
@Christian

That's because %this.player is of class t2DStaticSprite, not of Class Player.

Just change to this :
function t2DStaticSprite::create( %this, %name, %nation )
{
%this.name = %name;
}
#4
03/23/2006 (1:47 pm)
Here is the thread you seek:

http://www.garagegames.com/mg/forums/result.thread.php?qt=39092
#5
03/23/2006 (3:47 pm)
Thanks guys. I don't exactly love the solutions I found and working with inner/imbedded objects isn't a huge hassle. I was just wondering if there is an alternative, but it seems sticking to inner object is the best one. (Going down the class factory route seems a bit overkill and changing source code that may change soon anyway seems not ideal either).

Christian
#6
03/23/2006 (9:39 pm)
The latest release in your downloads page, its listed as 1.1 beta (though the file should be beta 2) has script class linking now... you can create a script class just like a script object

new ScriptClass(testClass);

Now you can directly link the namespaces of these script classes using class and superclass :)
#7
03/23/2006 (9:47 pm)
Thanks, although I'm not quite sure how that would help me.

Would I use it like this?

new ScriptClass( CPlayer )
{
class = Player;
superclass = t2dStaticSprite;
scenegraph = t2dScene;

name = "";
};

and thus inherit from t2dStaticSprite?

Christian
#8
03/23/2006 (9:49 pm)
No, you could do it like this

new t2dStaticSprite(testSprite)
{
   sceneGraph = t2dScene;
   class = Player;
}

then you could even use the script class "player" to share a namespace, this includes callbacks like onCollision callbacks.
#9
03/23/2006 (9:56 pm)
Like this?

new t2dStaticSprite(testSprite)
{
   sceneGraph = t2dScene;
   class = Player;
}

new ScriptClass( CPlayer )
{
   class = Player;
}

or are you saying that using t2dStaticSprite now works like ScriptObjects? Sorry, but I don't quite understand how ScriptClass differs from ScriptObjects...
#10
03/23/2006 (10:11 pm)
Script Class can be used very similar to Script Objects, except you can link them in with class and superclass directly to a T2D scene object...


like this

new ScriptClass(Player);

function Player::onCollsion(... all the values passed)
{

}

new t2dStaticSprite(testSprite)
{
   sceneGraph = t2dScene;
   class = Player;
}

the Player on collision callback willg et called on test sprite... previously you had to do all callbacks through an onCollision callback on t2dSceneObject::
#11
03/23/2006 (10:23 pm)
Thanks, Matthew

I just need an example how to exactly use ScriptClass. The above is perfect...

The only thing to remain unclear now is the following:

new ScriptClass(Aircraft);

function Aircraft::onCollsion(...)
{
}

new t2dStaticSprite(CAircraft)
{
sceneGraph = t2dScene;
class = Aircraft;

type = "Spitfire";
}

$aircraft = new t2dStaticSprite( : CAircraft );
$aircraft.onCollision();

Is this the way to use the "copy constructor", ie will this work?
#12
03/23/2006 (10:39 pm)
That will work, though keep in mind the copy ":" only copies all the data properties... so it will copy the scenegraph, class, and type fields and should link the classes by copying...
#13
03/23/2006 (10:40 pm)
Also note you can type these script classes in the class or superclass field in the Level Builder, I used this extensively in Mighty Fist and they're showing it off at GDC
#14
03/23/2006 (10:47 pm)
Excellent, thanks. I still need to explore the Level Builder, I'm not quite at that stage in my game development, but will keep it in mind...
#15
03/23/2006 (10:48 pm)
:) There is a very good reference .pdf (I can't take credit for it though it is very awesome) in the docs folder/
#16
04/02/2006 (12:36 pm)
Doesn't seem to work when I try it...
I'm trying to make a current player class, so I can say $cPlayer.jump(); for example.
This is the code I tried:

new ScriptClass (cPlayerClass);

function cPlayerClass::myFunction()
{
   echo("\ncPLAYER CLASS --> MY FUNCTION\n");
}

new fxImageMapDatablock2D(cPlayer)
{
   mode = "full";
   textureName = "./images/tileMap";
   class = cPlayerClass;
};

$currPlayer = new fxImageMapDatablock2D( : cPlayer );
$currPlayer.myFunction();

The error message in the console tells me:

$currPlayer #=# new fxImageMapDatablock2D( : cPlayer );

What am I doing wrong?
Thanks,

Efraim
#17
04/02/2006 (1:36 pm)
You don't link the image map to the class, you link the staticsprite (or animated sprite) to the class... so you can put the class name in the class or superclass field in the Level Builder when you create a sprite
#18
04/02/2006 (3:06 pm)
The fx...2D naming is also invalid now, that was for the 1.0.2 version. Since the console is not spitting out errors for an invalid datablock, perhaps you are not using Beta 2 Efraim?
#19
04/03/2006 (8:20 am)
@Matthew - That makes total sence, thanks!

@Mike - You're right, I was using the old version, Thanks!
#20
04/23/2006 (9:19 am)
There's one thing that I'm not certain about. Is it possible to declare a body with the ScriptClass to declare new variables such as the following?

new ScriptClass (Player)
{
  myNewValue = 10;
};

function Player::someFunction(...)
{
  ...use myNewValue somehow...
}

And, my Level Builder object has the following:

new t2dStaticSprite(myPlayerObject) {
  class = "Player";
  ...other data here...
};

I'm unable to use myNewValue in Player::someFunction. The function call is fine, it's just that myNewValue seems to not exist.
Using beta 2.
Page «Previous 1 2