Game Development Community

ok How does this translate in the new T2D

by Michael Branin · in Torque 2D Beginner · 02/12/2013 (2:47 pm) · 13 replies

How does this translate into the new T2D. I have created the sprite. I have givenit the FishPlayer class. I just have no idea where this movement code should reside or what I use in place of onlevelLoaded and %scenegraph
I looked over the example toys and the only movement code I could find was for the trucktoy and that was no help.

function PlayerFish::onLevelLoaded(%this, %scenegraph)
{
   $FishPlayer = %this;


   moveMap.bindCmd(keyboard, "w", "fishPlayerUp();", "fishPlayerUpStop();");
   moveMap.bindCmd(keyboard, "s", "fishPlayerDown();", "fishPlayerDownStop();");
   moveMap.bindCmd(keyboard, "a", "fishPlayerLeft();", "fishPlayerLeftStop();");
   moveMap.bindCmd(keyboard, "d", "fishPlayerRight();", "fishPlayerRightStop();");
}


function fishPlayerUp()
{
   $FishPlayer.setLinearVelocityY( -15 );
}


function fishPlayerDown()
{
   $FishPlayer.setLinearVelocityY( 15 );
}


function fishPlayerLeft()
{
   $FishPlayer.setLinearVelocityX( -30 );
}

function fishPlayerRight()
{
   $FishPlayer.setLinearVelocityX( 30 );
}


function fishPlayerUpStop()
{
   $FishPlayer.setLinearVelocityY( 0 );
}


function fishPlayerDownStop()
{
   $FishPlayer.setLinearVelocityY( 0 );
}


function fishPlayerLeftStop()
{
   $FishPlayer.setLinearVelocityX( 0 );
}


function fishPlayerRightStop()
{
   $FishPlayer.setLinearVelocityX( 0 );
}

#1
02/12/2013 (4:22 pm)
Rather than:

PlayerFish::onLevelLoaded

Use:

PlayerFish::onAddToScene

The onLevelLoaded callback is old TGB. What I suggested is what T2D MIT uses.
#2
02/12/2013 (4:55 pm)
one last thing .. what about

function t2dSceneGraph::onUpdateScene()
#3
02/12/2013 (5:28 pm)
ok Having some issues
function playerClass::onAdd(%this)
{
    Echo ("Player Created");
      
    moveMap.bindCmd(keyboard, "left", "playerLeft();", "playerLeftStop();");
    moveMap.bindCmd(keyboard, "right", "playerRight();", "playerRightStop();");
    moveMap.bindCmd(keyboard, "space", "playerJump();", "");
}

Ok when I run my module I create a sprite and give it the class of playerClass and when its created I get the echo that shows the player is created. But I get these errors on the console log

modules/CharacterMoveToy/1/scripts/player.cs (7): Unable to find object: 'moveMap' attempting to call function 'bindCmd'
modules/CharacterMoveToy/1/scripts/player.cs (8): Unable to find object: 'moveMap' attempting to call function 'bindCmd'
modules/CharacterMoveToy/1/scripts/player.cs (9): Unable to find object: 'moveMap' attempting to call function 'bindCmd'
#4
02/12/2013 (8:00 pm)
@Michael : Concerning you first question about onUpdateScene.

Scene Callback

You have to manually enable your Scene to register for Update Callbacks.
In the Sandbox project that ships with T2D MIT, the scene is called 'SandboxScene'.

1 - Tell the Scene you want it to call an update function on each tick.
//Edit, Now with correct UpdateCallback assignation

SandBoxScene.UpdateCallback = true;

2 - Create your callback function. It must be named onSceneUpdate.
function SandboxScene::onSceneUpdate(%this)
{
echo("Spamming the console");
}

Objects Callback

Note that you can also enable individual objects to call a function on every update.

1 - Simply call setUpdateCallback

MyObject.setUpdateCallback(true);

2 - Instead of onSceneUpdate, simply create an onUpdate function

function MyObject::onUpdate(%this)
{
echo("Spamming the console, yet again");
}

-------------------------------------

Be aware that most gameplay can also be handled pretty accurately with schedules instead. You can look at the Behaviors in DeathBallToy's scripts folder for examples.
#5
02/12/2013 (8:13 pm)
@Michael : Concerning the Movemap issue

The error message you get is basically telling you that moveMap cannot be found, so calling bindCmd on a non-existing object simply won't work.

In the Sandbox project, all input is handled via the Sandbox module's manipulation script.

The only exception is the Console, which can be toggled with Ctrl~.

If you look in the Sandbox module's main.cs file, you will find the following line :

GlobalActionMap.bind( keyboard, "ctrl tilde", toggleConsole );

All ActionMap-related code seems to have remained the same since TGB so all you need to do is to create MoveMap manually.

moveMap = new ActionMap();

moveMap.bindCmd(keyboard, "left", "playerLeft();", "playerLeftStop();");  
moveMap.bindCmd(keyboard, "right", "playerRight();", playerRightStop();");  
moveMap.bindCmd(keyboard, "space", "playerJump();", "");
#6
02/12/2013 (9:05 pm)
That results in a error

C:/Users/Michael Branin/Desktop/Torque2D-master/modules/NinjaDemoToy/1/scripts/player.cs Line: 7 - parse error
>>> Advanced script error report.  Line 7.
>>> Some error context, with ## on sides of error halt:
unction playerClass::onAddToScene(%this)
>>> Error report complete.

Torque simply closes out.

the only way I could get it working was

function playerClass::onAddToScene(%this)
{
    Echo ("Player Created");
    
    $pGuy = %this;
      
    GlobalActionMap.bindCmd(keyboard, "left", "playerLeft();", "playerLeftStop();");
    GlobalActionMap.bindCmd(keyboard, "right", "playerRight();", "playerRightStop();");
    GlobalActionMap.bindCmd(keyboard, "space", "playerJump();", "playerFall();");
}
#7
02/12/2013 (9:35 pm)
ok What I am trying to do is convert as much of the old Ninja Platform tutorial over to T2D MIT as possible.

docs.garagegames.com/tgb/official/content/documentation/Game%20Tutorials/Ninja%2...

I have some of it working but I am having some issues. Can someone look at it and look at the old tutorial and see if they can help out?

Basically I created a back ground and a ground level. I created the player and have him moving left and right and even jumping up and he plays the correct animations. but ... I had to hack the movement code some because I can not for the life of me get the callback code working.

here is my Module. Simply Drop it in your sandbox and run

www.dropbox.com/s/4b18wjsmb5qydaj/NinjaDemoToy.rar
#8
02/12/2013 (9:38 pm)
@Michael - There's some additional code that Simon forgot to mention if you are going to make your own ActionMap. I'm actually working on a toy that covers this but it's not quite ready for release but here's the basic code:

function AstralToy::create( %this )
{
    // Load game scripts
    exec("./scripts/game.cs");
    exec("./scripts/moveBehavior.cs");
   
    // Setup keyboard bindings.
    new ActionMap(moveMap);   
    moveMap.push();
   
    $enableDirectInput = true;
    activateDirectInput();
    
    // Create behaviors
    %this.createMoveBehavior();
    
    // Create starting values
    AstralToy.acceleration = 30;
    AstralToy.turnSpeed = 150;
    AstralToy.damping = 1;
    
    // Add Sandbox config options
    addNumericOption( "Acceleration", 1, 100, 5, "setAcceleration", AstralToy.acceleration, true, "Sets the forward acceleration" );
    addNumericOption( "Turn Speed", 1, 300, 5, "setTurnSpeed", AstralToy.turnSpeed, true, "Sets the velocity of turning" );
    addNumericOption( "Damping", 0, 5, 0.1, "setDamping", AstralToy.damping, true, "Sets the amount to damp movement" );
    
    // Reset the game.
    AstralToy.reset();
    
}

//-----------------------------------------------------------------------------

function AstralToy::destroy( %this )
{
    // Destroy keyboard bindings.
    moveMap.pop();
    moveMap.delete();
}

The .push() method activates the custom ActionMap (and .pop() deactivates it). activateDirectInput() activates polling of all input devices (keyboard, mouse, etc). There's also an activateKeyboard() function if you only want polling of the keyboard.

After that, you setup your custom ActionMap similarly to how it was done in TGB (using bindCmd or bindObj). Note that this is inside a behavior, adding this to the onAddToScene callback would work as well.

function moveBehavior::onBehaviorAdd(%this)
{
   if (!isObject(moveMap))
      return;
   
   %this.owner.setLinearDamping(%this.damping);
   
   moveMap.bindObj(getWord(%this.upKey, 0), getWord(%this.upKey, 1), "moveUp", %this);
   moveMap.bindObj(getWord(%this.downKey, 0), getWord(%this.downKey, 1), "moveDown", %this);
   moveMap.bindObj(getWord(%this.leftKey, 0), getWord(%this.leftKey, 1), "moveLeft", %this);
   moveMap.bindObj(getWord(%this.rightKey, 0), getWord(%this.rightKey, 1), "moveRight", %this);
   
   %this.up = 0;
   %this.down = 0;
   %this.left = 0;
   %this.right = 0;
}
#9
02/12/2013 (10:32 pm)
Something you're going to have to be aware of is that collision does not work at all like it used to. You will have to build a scene with things for the player to stand on - of course, the composite sprite component parts can each have a collision body.

Also, anything with "t2d" in front of it has "t2d" removed. So t2dSceneObject becomes simply SceneObject as a general rule.
#10
02/12/2013 (10:56 pm)
Not to discourage Michael or anyone else reading from trying to "translate" TGB tutorials into the MIT version (I'm doing the same), but if you do not have a way to read or generate a script reference for the new engine - it is only going to be an exercise in frustration.

If you have a copy of Torsion, check out the code browser tab next to your project tree after opening the sandbox project file. It has been a huge help for me figuring out the names of the new or changed methods for stuff like Sprite - SpriteBase - SceneObject. If you don't have Torsion, hopefully a reference doc will be put online soon.
#11
02/13/2013 (6:41 am)
Richard I know about the collision changes. So far I just have the character collding with the ground I am actually ripping apart the truck toy for code to fix the busted collision issues. and I am built the floor in script from one of the other toys. but again the ninga platformer uses a scenecallback and a scene update to monitor changes in the keys movement. That part I flat out could not get working even with Simons decription a few post up. so I had to modify and hack some other movement code. Looking in the project I uploaded to drop box if Anyone wants to see what I have so far.

I just need someone to look at that tutorial and see if they can help me either get the callback stuff working or walk me through a better way.
#12
02/13/2013 (6:52 am)
So, Scene::onSceneUpdate() isn't being called if you set the scene's UpdateCallback field is set to true?
#13
02/13/2013 (7:03 am)
Note : I had to edit the code in my example above, now it works properly.

For scenes -> Scene.UpdateCallback = true;
For objects -> SceneObject.setUpdateCallback(true);