Game Development Community

(Solved) Where can I find an example of a finished player.cs for the shooter tutorial?

by Korey Bulloch · in Torque Game Builder · 03/13/2010 (8:47 pm) · 5 replies

I'm an artist with practically no scripting knowledge. I'm going through the Shooter tutorial in an effort to create a player character that can move. The tutorial seems pretty straight forward. There are several lines of script I'm instructed to add to the player.cs, which I do, but I can't get any movement when I Run Game.

TGB is automatically altering the upper/lower case of some characters which may be causing some issues, but I have a funny feeling I'm simply pasting the script into player.cs incorrectly. A complete and verified working player.cs would do me a world of good in order to see where I'm going wrong. Does anyone know where I can find one?

#1
03/14/2010 (5:01 am)
The player.cs file should look like this:

function playerShip::onLevelLoaded(%this, %scenegraph)
{
   //set the player's ship name to the instance
   $pShip = %this;


   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 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 pShipLeftStop()
{
   $pShip.moveLeft = false;
   $pShip.updateMovement();
}

function pShipRightStop()
{
   $pShip.moveRight = false;
   $pShip.updateMovement();
}

function pShipUpStop()
{
   $pShip.moveUp = false;
   $pShip.updateMovement();
}

function pShipDownStop()
{
   $pShip.moveDown = false;
   $pShip.updateMovement();
}

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.setLinearVelocityX( 0 );
   }
   if(!%this.moveUp && !%this.moveDown)
   {
      %this.setLinearVelocityY( 0 );
   }
}

Even if the script is correct, there are other potential errors that could cause the player ship not to move.

1. Did you add the exec("./player.cs"); line to the game.cs script?
2. Did you properly name the player ship's class playerShip?
3. Did you add the hSpeed and vSpeed dynamic fields to the player ship?
4. Are the script files being properly saved as .cs and not .cs.txt?

Alternately, there's a video tutorial based on the creating a side scrolling shooter as well. It doesn't follow the text based one identically - there are major differences in script, but it might help in learning to use TGB.
#2
03/14/2010 (11:48 am)
1. I did:

function initializeProject()
{
// Load up the in game gui.
exec("~/gui/mainScreen.gui");

// Exec game scripts.
exec("./gameScripts/game.cs");
exec("./gameScripts/player.cs");



2. This I've had a hard time with, which is frustrating. The screenshot shows:

Name: pShip
Class: playerShip

However whenever I hit Enter or switch to another field, "pShip" automatically becomes renamed "pship" and "playerShip" becomes "PlayerShip". I'm not sure how case sensitive the scripts are, but I can't imagine this is working in my favor. I tried editing the text in the player.cs accordingly with no results. Also, if I Reload Project, the Name is retained but the Class field becomes blank. I've just been entering text and hitting enter- that should be enough, right?


3. hSpeed is set to 100, vSpeed is set to 80


4. player.cs is correctly named and is showing up as a Visual C# Source file (.cs) in the \game\gameScripts folder.

#3
03/14/2010 (1:37 pm)
Well, Torquescript is not case sensitive, so pShip is the same as pship is the same as PSHIP.

The reason it is not working is because the class field is not being saved properly. If the class field is blank when you reload the project, then that is why you can't move the ship - there is no object which has the playerShip class to bind the keys in onLevelLoaded.

You can open up the level .t2d file with a text editor, find the t2dStaticSprite(pShip), and within that add class = "playerShip";

Not sure why the level builder is bugging out on you.
#4
03/14/2010 (11:47 pm)
Although it isn't saving (or rather, that it isn't saving/appearing in the program), it appears to be in the level1.t2d. I'm a bit at a loss. I think the best course of action at this point is simply to start over and see if I can't catch some other detail I might have overlooked.


new t2dStaticSprite(pship) {
imageMap = "PlayerShip";
frame = "0";
canSaveDynamicFields = "1";
class = "PlayerShip";
Position = "-1.511 4.986";
size = "9.688 12.708";
hSpeed = "100";
mountID = "4";
vSpeed = "80";
#5
03/15/2010 (12:10 am)
I deleted all the old folders and started over again from scratch. This time around the text didn't auto format and the Class field didn't reset when I reloaded the project. The ship is moving around like I would expect and it's as if there were never any issues. I'm going to chalk this up as the program simply hitting a funky state. Thank you very much for your help.