Exec statement not working
by Andrew H · in Torque Game Builder · 07/30/2011 (1:58 pm) · 18 replies
I'm making a nuisance of myself, aren't I? Anyways, here's a sample of my game.cs:
The path to player.cs is correct - but it's not getting executed. What am I doing wrong? If it helps, I'm doing this tutorial: http://docs.garagegames.com/tgb/official/content/documentation/Tutorials/Shooter/3%20-%20Adding%20Player%20Input.html
// startGame
// All game logic should be set up here. This will be called by the level builder when you
// select "Run Game" or by the startup process of your game to load the first level.
//---------------------------------------------------------------------------------------------
function startGame(%level)
{
Canvas.setContent(mainScreenGui);
Canvas.setCursor(DefaultCursor);
new ActionMap(moveMap);
moveMap.push();
$enableDirectInput = true;
activateDirectInput();
enableJoystick();
//Starts up the player scripts
exec("./player.cs");
sceneWindow2D.loadLevel(%level);
}
//---------------------------------------------------------------------------------------------
// endGame
// Game cleanup should be done here.
//---------------------------------------------------------------------------------------------
function endGame()
{
sceneWindow2D.endLevel();
moveMap.pop();
moveMap.delete();
}The path to player.cs is correct - but it's not getting executed. What am I doing wrong? If it helps, I'm doing this tutorial: http://docs.garagegames.com/tgb/official/content/documentation/Tutorials/Shooter/3%20-%20Adding%20Player%20Input.html
About the author
I do everything for my game company. Including the legal stuff.
#2
07/30/2011 (7:03 pm)
Yes, the functions in player.cs are bound to keys. But it's not executing player.cs at all - no .dso file.
#3
07/30/2011 (8:34 pm)
Is the player.cs in the same directory as the file with the startGame function?
#4
Yes, it is. Oh, I didn't think to put this in there, but here's player.cs - if the problem is actually in there:
07/30/2011 (8:41 pm)
@MattYes, it is. Oh, I didn't think to put this in there, but here's player.cs - if the problem is actually in there:
function playerShip::onLevelLoaded (%this, %scenegraph)
{
//set the player's ship name to the instance
$pShip = %this;
//bind keys to movement functions
moveMap.bindCmd (keyboard, "w", "pShipUp();", "pShipUpStop();");
moveMap.bindCmd (keyboard, "s", "pShipDown();", "pShipDownStop();");
moveMap.bindCmd (keyboard, "a", "pShipLeft();", "pShipLeftStop();");
moveMap.bindCmd (keyboard, "d", "pShipRight();", "pShipRightStop();");
}
function playerShip::updateMovement(%this)
{
if(%this.moveLeft)
{
%this.setLinearVelocityX( -$pShip.hSpeed );
}
if(%this.moveRight)
{
%this.setLinearVelocityX( $pShip.hSpeed );
}
if(%this.moveUp)
{
%this.setLinearVelocityY( -$pShip.vSpeed );
}
if(%this.moveDown)
{
%this.setLinearVelocityY( $pShip.vSpeed );
}
if(!%this.moveLeft && !%this.moveRight)
{
%this.setLinearVeloxityX( 0 );
}
if(!%this.moveUp && !%this.moveDown)
{
%this.setLinearVeloxityY( 0 );
}
function pShipUp()
{
$pShip.moveUp = true;
$pShip.updateMovement();
}
function pShipDown()
{
$pShip.moveDown = true;
$pShip.updateMovement();
}
function pShipLeft()
{
$pShip.moveLeft = true;
$pShip.updateMovement();
}
function pShipRight()
{
$pShip.moveRight = true;
$pShip.updateMovement();
}
function pShipUpStop()
{
$pShip.moveUp = false;
$pShip.updateMovement();
}
function pShipDownStop()
{
$pShip.moveDown = false;
$pShip.updateMovement();
}
function pShipLeftStop()
{
$pShip.moveLeft = false;
$pShip.updateMovement();
}
function pShipRightStop()
{
$pShip.moveRight = false;
$pShip.updateMovement();
}
#5
a) Perhaps you have a syntax error in your script. If so, parts of it will just get ignored and it will never load. To check for this, look at the "console.log" file in your project. It's in the top directory of your project. Search for "player.cs" in it. For instance, when I did this tutorial, this is what my console.log said about player.cs:
Perhaps yours has an error and so the "Compiling" part will tell you what that error is and the loading part will never happen.
07/31/2011 (11:36 am)
What you have looks right. I'd suggest two possibilities. I'll write one per post...a) Perhaps you have a syntax error in your script. If so, parts of it will just get ignored and it will never load. To check for this, look at the "console.log" file in your project. It's in the top directory of your project. Search for "player.cs" in it. For instance, when I did this tutorial, this is what my console.log said about player.cs:
Quote:
Compiling C:/Users/charliep/Documents/prj/torque/tutorials/ShooterTutorial/game/gameScripts/player.cs...
<and later>
Loading compiled script C:/Users/charliep/Documents/prj/torque_tutorials/ShooterTutorial/game/gameScripts/player.cs.
Perhaps yours has an error and so the "Compiling" part will tell you what that error is and the loading part will never happen.
#6
You have written PlayerShip class functions, like PlayerShip::onLevelLoaded. But if you don't have any objects in your level that are PlayerShips then nothing is trying to run your functions. They just sit dormant.
Make sure you have a level saved, and that it has a ship in it. Then select the ship, go to the "Edit" tab, scroll down to "Scripting" panel and make sure the ship "Class" is set to playerShip. This is the key to making the on screen ship use the class you just wrote.
(One last post coming up.)
07/31/2011 (11:45 am)
b) maybe the "class" you wrote in your script isn't connected to anything? You have written PlayerShip class functions, like PlayerShip::onLevelLoaded. But if you don't have any objects in your level that are PlayerShips then nothing is trying to run your functions. They just sit dormant.
Make sure you have a level saved, and that it has a ship in it. Then select the ship, go to the "Edit" tab, scroll down to "Scripting" panel and make sure the ship "Class" is set to playerShip. This is the key to making the on screen ship use the class you just wrote.
(One last post coming up.)
#7
In other words, if the script is loaded at all, it should write out "I found player.cs". If you see this in your console.log, then the engine is finding your script. You just may not be using the script successfully.
07/31/2011 (11:46 am)
Here is a way to see if the engine sees your "player.cs" file at all. At the very top of it, outside of any functions, add thisecho("I found player.cs");
function playerShip::onLevelLoaded(%this, %scenegraph)
{
...In other words, if the script is loaded at all, it should write out "I found player.cs". If you see this in your console.log, then the engine is finding your script. You just may not be using the script successfully.
#8
Could you help me out here? I don't understand the error message.
Advanced script error report. Line 45.
Some error context, with ## on sides of error halt:
Then it shows this:
The ship is linked to the class just fine, also.
Wait a minute...OKAY. I get what I got wrong! I forgot to end the updateMovement() function!
Well, I learned that console is helpful for debugging. Thanks a bunch!
EDIT:
But now I have another problem: I'm not getting any console compilation errors. The ship is connected to the right class, and named correctly. Dynamic fields are set up right. But it's just sitting there when I push buttons.
07/31/2011 (12:29 pm)
I checked the console. I got an error message. I just learned that the console is helpful for debugging. It found player.cs...Could you help me out here? I don't understand the error message.
Advanced script error report. Line 45.
Some error context, with ## on sides of error halt:
Then it shows this:
function pShipUp()
##{## //That's the 'error halt'.
$pShip.moveUp = true;
$pShip.updateMovement();
}The ship is linked to the class just fine, also.
Wait a minute...OKAY. I get what I got wrong! I forgot to end the updateMovement() function!
Well, I learned that console is helpful for debugging. Thanks a bunch!
EDIT:
But now I have another problem: I'm not getting any console compilation errors. The ship is connected to the right class, and named correctly. Dynamic fields are set up right. But it's just sitting there when I push buttons.
#9
07/31/2011 (4:23 pm)
Are you defining $pShip.vSpeed and $pShip.hSpeed anywhere?
#11
Your onLevelLoaded is where you are telling Torque to map w, s, a, and d to your ship's movement. So make sure onLevelLoaded is really getting called by putting a line in it like
echo("adding wasd to keyboard map");
If that appears in your console output, then you can move on to the next most plausible site of a problem and add debug echo's there.
I'd try what Tim guessed and do the following in your updateMovement function:
And see what is printed.
The first script or two in a new language are by far the hardest to get working!
07/31/2011 (7:44 pm)
I'd put a few well-placed "echo" statements in your code, and see if they show in the console now.Your onLevelLoaded is where you are telling Torque to map w, s, a, and d to your ship's movement. So make sure onLevelLoaded is really getting called by putting a line in it like
echo("adding wasd to keyboard map");
If that appears in your console output, then you can move on to the next most plausible site of a problem and add debug echo's there.
I'd try what Tim guessed and do the following in your updateMovement function:
echo("did my dynamic fields work? hSpeed is" SPC $pShip.hSpeed);
echo("did my dynamic fields work? vSpeed is" SPC $pShip.vSpeed);And see what is printed.
The first script or two in a new language are by far the hardest to get working!
#12
I've really figured that out. I've learned one BIG thing now: If something's not working, the console is probably your best bet to figure out WHY it isn't.
Also, what does SPC mean and do in this case? It will be handy to know in the future. I'm trying to soak up as much knowledge as I can from everything.
EDIT: WASD were bound. But my dynamic fields did not work. I can't seem to figure out why. It printed:
did my dynamic fields work? hSpeed is
Same thing for vSpeed.
07/31/2011 (7:57 pm)
@CharlieQuote:The first script or two in a new language are by far the hardest to get working!
I've really figured that out. I've learned one BIG thing now: If something's not working, the console is probably your best bet to figure out WHY it isn't.
Also, what does SPC mean and do in this case? It will be handy to know in the future. I'm trying to soak up as much knowledge as I can from everything.
EDIT: WASD were bound. But my dynamic fields did not work. I can't seem to figure out why. It printed:
did my dynamic fields work? hSpeed is
Same thing for vSpeed.
#13
Save this bookmark!
http://docs.garagegames.com/tgea/official/content/documentation/Scripting%20Ref...
I have trouble working around the site and finding the docs myself. However this is the official scripting language reference. Look under "strings" to see what SPC does.
OK. Well one *problem* with scripting languages like TorqueScript is that they try to be very forgiving and keep going past errors. Since your printout does not have an hSpeed from the echo, it probably means the $pShip.hspeed didn't set. However, the echo line doesn't complain. It just prints nothing instead of a valid speed!
Furthern, when you use $pShip.hspeed in your code that goes "%this.setLinearVelocityX( -$pShip.hSpeed );", it also produces nothing. Instead of complaining it probably just assumes that nothing is the same as 0 and doesn't move. Frustrating for me.
My next best guess is that you *didn't* set the dynamic fields on the right object. Make sure they are on your player ship!
07/31/2011 (10:07 pm)
Quote:
Also, what does SPC mean and do in this case?
Save this bookmark!
http://docs.garagegames.com/tgea/official/content/documentation/Scripting%20Ref...
I have trouble working around the site and finding the docs myself. However this is the official scripting language reference. Look under "strings" to see what SPC does.
OK. Well one *problem* with scripting languages like TorqueScript is that they try to be very forgiving and keep going past errors. Since your printout does not have an hSpeed from the echo, it probably means the $pShip.hspeed didn't set. However, the echo line doesn't complain. It just prints nothing instead of a valid speed!
Furthern, when you use $pShip.hspeed in your code that goes "%this.setLinearVelocityX( -$pShip.hSpeed );", it also produces nothing. Instead of complaining it probably just assumes that nothing is the same as 0 and doesn't move. Frustrating for me.
My next best guess is that you *didn't* set the dynamic fields on the right object. Make sure they are on your player ship!
#15
Let's see your playerShip entry in your level file. Your editor saves the level in a .t2d file. Wherever you've saved your project, you'll have a level file. Assuming your project directory is ShooterTutorial, the level is at, for instance, ShooterTutorial\game\data\levels\shooter.t2d
Once you find it an open it (in a text editor), you should have a pShip in it. Back when I did that tutorial, mine looked like this:
Be careful! While there is nothing wrong with editing this file by hand, if you have TGB open as well, it may fight you or get confused if you edit the file manually.
08/01/2011 (9:59 am)
As Luke Skywalker once said, "That's Impossible!"Let's see your playerShip entry in your level file. Your editor saves the level in a .t2d file. Wherever you've saved your project, you'll have a level file. Assuming your project directory is ShooterTutorial, the level is at, for instance, ShooterTutorial\game\data\levels\shooter.t2d
Once you find it an open it (in a text editor), you should have a pShip in it. Back when I did that tutorial, mine looked like this:
new t2dStaticSprite(pShip) {
imageMap = "playerShipImageMap";
frame = "0";
useSourceRect = "0";
sourceRect = "2.26805e-039 0 2.26805e-039 6.20148e-039";
canSaveDynamicFields = "1";
class = "playerShip";
Position = "-29.029 -0.365";
size = "25.000 12.500";
WorldLimitMode = "CLAMP";
WorldLimitMin = "-50.000 -90.000";
WorldLimitMax = "120.000 37.447";
hSpeed = "100";
missileSpeed = "1000";
mountID = "2";
vSpeed = "80";
};Be careful! While there is nothing wrong with editing this file by hand, if you have TGB open as well, it may fight you or get confused if you edit the file manually.
#16
08/01/2011 (12:39 pm)
Mine looks the same way - minus the parts that get put in in parts I haven't gotten to yet. hSpeed and vSpeed are both there.
#17
Here is a rundown of what we've looked at:
So for some reason, your scripts all work but the dynamic field does not seem set.
I'm sure you'll find the one little issue.
08/01/2011 (4:52 pm)
Bummer Laura. I don't know then. I guess more well-placed echo's will find the fatal flaw. Good luck!Here is a rundown of what we've looked at:
- you have a scene object that represents the player's ship and it
- is named pship -- so you can find it by the variable pship (which never happens in this part we are debugging btw b/c we use $pShip instead)
- has the class PlayerShip set -- so events on the ship will go to the PlayerShip class (which you've seen work)
- have dynamic fields hspeed and vspeed set (and you are *sure* they are named correctly)
- you have a script full of PlayerShip:: methods and
- note the name of the file player.cs doesn't hurt the fact that the script methods are called PlayerShip::. There is no connection required between the filename and the methods in it.
- onLevelLoaded immediately sets a global variable named $pship by doing "$pShip = %this". now we can use $pShip anywhere in your game and get the one player ship.
- keyboard events are making it to your script
So for some reason, your scripts all work but the dynamic field does not seem set.
I'm sure you'll find the one little issue.
#18
08/01/2011 (6:11 pm)
Weird...I dunno why - but all of a sudden it just started working! I am mildly confused...oh, well. I can't really complain.
Torque 3D Owner Bloodknight
Bloodknight Studios
I'm not overly familiar with TGB, but the exec(); should be called outside of your startgame() function and then whatever spawnplayer() function TGB uses should then be called inside your startgame() function.