Creating An Object In Code - (Warning! 3 Newbie Questions!)
by Jay Jennings · in Torque Game Builder · 10/28/2009 (5:57 am) · 5 replies
Here is a chunk of code I'm working on from the 2D Game Building for Teens book (I'm not a teen, but I was one once, plus I have two of my own) and the questions I have concerning it afterwards:
I get no errors in the console when running this, but the deathRayImageMap I expect to see on the screen never shows up.
However, I'm not really looking for debugging help (although if you spot the problem, feel free to sing out!), I need clarification on what's happening.
The deathRayImageMap has already been put into the project but nothing else has been done with it.
1. Does BattyPlayer::createDeathray() create an object called $playerDeathRay that I can then use in other areas of the program?
2. Since $playerDeathRay starts with a $ and not %, does that mean it's global to the program or does the fact that it was created inside a function trump that and make the variable local only?
3a. When this line is run, $playerDeathray.fire();, does that mean in this line, function playerDeathray::fire(%this), the %this variable is set to the $playerDeathray object? 3b. Or does the %this just refer to the object and how it was called is not relevant?
Thanks for the help -- this code is from a book with a LOT of mistakes, and while I think I've fixed all the typos, I'm thinking maybe the reason the code still won't work is because of a "real" problem and not just a typo. So I'm trying to buckle down and finally get a handle on TorqueScript.
Thus, the newbie questions. =:)
Thanks.
Jay Jennings
function BattyPlayer::createDeathray()
{
if(!%this.isDead)
{
$playerDeathray = new t2dStaticSprite()
{
scenegraph = %this.scenegraph;
class = playerDeathray;
missileSpeed = %this.missileSpeed;
player = %this;
};
$playerDeathray.fire();
}
}
function playerDeathray::fire(%this)
{
%this.setWorldLimit(kill,"-40 -30 40 30");
%this.setLinearVelocityX($BattyPlayer.missileSpeed);
%this.setPosition($BattyPlayer.getPosition());
%this.setImageMap(deathrayImageMap);
%this.setSize(83,38);//approximately half size of original graphic
%this.setCollisionActive(true,true);
%this.setCollisionPhysics(false,false);
%this.setCollisionCallback(true);
}First, does that look syntactically and logically correct? What's supposed to happen is $playerDeathray is created when the shoot button is pressed and then the fire function is run.I get no errors in the console when running this, but the deathRayImageMap I expect to see on the screen never shows up.
However, I'm not really looking for debugging help (although if you spot the problem, feel free to sing out!), I need clarification on what's happening.
The deathRayImageMap has already been put into the project but nothing else has been done with it.
1. Does BattyPlayer::createDeathray() create an object called $playerDeathRay that I can then use in other areas of the program?
2. Since $playerDeathRay starts with a $ and not %, does that mean it's global to the program or does the fact that it was created inside a function trump that and make the variable local only?
3a. When this line is run, $playerDeathray.fire();, does that mean in this line, function playerDeathray::fire(%this), the %this variable is set to the $playerDeathray object? 3b. Or does the %this just refer to the object and how it was called is not relevant?
Thanks for the help -- this code is from a book with a LOT of mistakes, and while I think I've fixed all the typos, I'm thinking maybe the reason the code still won't work is because of a "real" problem and not just a typo. So I'm trying to buckle down and finally get a handle on TorqueScript.
Thus, the newbie questions. =:)
Thanks.
Jay Jennings
About the author
Make my living writing and selling ecommerce tools -- but I'd rather be writing games (of course). My family of four lives in 208 square feet and we may be in YOUR town next week.
#2
There are still some problems with the code, but I can see the graphic flashing into place now, so I can probably track down the rest of the problems.
Man, I wish I had the time to just do Torque all the time instead of bits and pieces here and there -- immersion is the *only* way to get good at something like this and I'm only able to wade in the puddles!
Thank you!
Jay Jennings
10/28/2009 (7:17 am)
Bless you!There are still some problems with the code, but I can see the graphic flashing into place now, so I can probably track down the rest of the problems.
Man, I wish I had the time to just do Torque all the time instead of bits and pieces here and there -- immersion is the *only* way to get good at something like this and I'm only able to wade in the puddles!
Thank you!
Jay Jennings
#3
Also, the function call "$playerDeathray.fire();" works not because of the name "$playerDeathray", but because of the "class = playerDeathray;" defined when it is created. (Just something that threw me many a year ago.)
Good luck!
10/28/2009 (4:23 pm)
Just to help you out, I want to point out that "$playerDeathray" will be available throughout all scripts because of the global variable tag "$".Also, the function call "$playerDeathray.fire();" works not because of the name "$playerDeathray", but because of the "class = playerDeathray;" defined when it is created. (Just something that threw me many a year ago.)
Good luck!
#4
So, that function call works not because of the name it was given, but it has to be called using the name, doesn't it?
Or, I don't think TorqueScript has a "me," but in another language you could use me.fire(); from inside the object, yes?
I think I get it. (We'll see, won't we?) =:)
Thanks.
Jay Jennings
10/28/2009 (5:35 pm)
William, thanks for that.So, that function call works not because of the name it was given, but it has to be called using the name, doesn't it?
Or, I don't think TorqueScript has a "me," but in another language you could use me.fire(); from inside the object, yes?
I think I get it. (We'll see, won't we?) =:)
Thanks.
Jay Jennings
#5
The "me" (if I'm understanding you right), is equivalent to "%this" inside the "playerDeathray::fire" function. The variable "%this" will be exactly the same as the variable "$playerDeathray".
10/28/2009 (6:16 pm)
Exactly... it does have to be called using the variable name, but it used the class name to determine which function to call.The "me" (if I'm understanding you right), is equivalent to "%this" inside the "playerDeathray::fire" function. The variable "%this" will be exactly the same as the variable "$playerDeathray".
Associate Phillip O'Shea
Violent Tulip
function BattyPlayer::createDeathray() { // ... }To this:function BattyPlayer::createDeathray( %this ) { // ... }As you can see, there was no "%this" variable defined so the object was not getting added to the scene, nor was it referencing the other variables correctly. Give that a whirl.