Game Development Community

Dingo's Question of the day thread

by Braedon Hinchcliffe · in Torque Game Builder · 12/19/2007 (2:04 pm) · 10 replies

Hello. as you may already know I am working on a platformer demo, Every few hours or so I seem to come across some sort of issue or question that the community is able to answer much better than trial and error. As a result I am creating this thread where I will post questions as they come up so that I don't need to create any more threads.

-------- Current Question -------
How would you be able to know if a player has pressed a key twice quickly (for the purpose of running/walking in a platformer)

This is how I currently do movement with linear motion

moveMap.bindCmd(keyboard, "right", "playerRight();", "playerRightStop();");

if(%this.moveRight)
{
%this.setLinearVelocityX(60);
}

Of course thats just a few lines but from that you can get the general idea (its modified from the platformer tutorial)

#1
12/19/2007 (2:50 pm)
At the end of playerRight(), set a global variable ($lastTimeRightPressed) equal to getSimTime(). At the beginning of the playerRight() function, check the difference between getSimTime() and $lastTimeRightPressed. If it is less than your pre-defined amount (say 500 for 1/2 second), set the velocity to something different.
#2
12/19/2007 (8:04 pm)
I think getsimtime gives the time since the program started. so its always above 500?

--------------
function playerRight()
{
$pGuy.moveRight = true;
$lastTimeRightPressed = getsimtime()
}


if (%this.moveRight)
{
%this.setLinearVelocityX(30);
}

if(%this.moveRight && $lastTimeRightPressed < 500)
{
%this.setLinearVelocityX(50);
}
--------------------------------------------------------------------------------
Is this sort of the right idea?
#3
12/19/2007 (9:09 pm)
I mentioned that you should check the difference between getSimTime() and $lastTimeRightPressed. In your case...

function playerRight()
{
  $pGuy.moveRight = true;
  if( getSimTime() - $lastTimeRightPressed < 500 )
  {
    $pGuy.moveSpeed = 50;
  }
  else
  {
    $pGuy.moveSpeed = 30;
  }
  $lastTimeRightPressed = getSimTime();
}

...

if( %this.moveRight )
{
  %this.setLinearVelocity( %this.moveSpeed );
}
#4
12/20/2007 (2:36 pm)
This doesn't work. Is getsimtime the time from the program's start or the time its called?
#5
12/20/2007 (3:33 pm)
Well, getSimTime returns the current sim time in milliseconds. This is the number of milliseconds since the program began (although it can vary if you modify the speed the game is running at).

I'm not at a point where I can quickly put this into my current project, but I'm certain the above code will work.

To debug this, make the following changes:

function playerRight()
{
  $pGuy.moveRight = true;
  echo( "Time since last press: " @ (getSimTime() - $lastTimeRightPressed) );
  if( getSimTime() - $lastTimeRightPressed < 500 )
  {
    $pGuy.moveSpeed = 50;
  }
  else
  {
    $pGuy.moveSpeed = 30;
  }
  $lastTimeRightPressed = getSimTime();
}

Inside the console, you will see a message every time you press your 'right' key. It should tell you the number of milliseconds since you last pressed the same key. If it doesn't look reasonable, let me know... I'm sure I can find a moment to put in a quick test before I leave for the holidays.
#6
12/20/2007 (9:32 pm)
T2dSceneObject::setLinearVelocity - Invalid number of parameters!

On a side note how do I put code into that white window. Does this forum support bbcode?
#7
12/21/2007 (5:47 am)
Braedon,

I am glad you are working at learning TGB. And when I can, I will post to help answer your questions. I'd like to take this opportunity to help you, and other newcomers. This is not meant as a slam, flame bait, or any such thing. It is along the lines of teaching one to fish instead of giving them a fish.

Quote:On a side note how do I put code into that white window. Does this forum support bbcode?

The best way to learn, is to experiment, to try things yourself, before asking for help. Did you try using BBCode? What happened? Did it work? Did some of them work and others didn't? Did you look right below the box you were typing in and see where it says, "(Want to use bold, italics and add links to your text? Click here to learn how)"?

Quote:This doesn't work. Is getsimtime the time from the program's start or the time its called?

Did you even take the time to search the documentation of TDN? You would have found your own answer if you had.

There is nothing wrong with asking for help, but you are more likely to get help if you have already tried to help yourself.

Quote:T2dSceneObject::setLinearVelocity - Invalid number of parameters!

Umm?!? OK?!? Since you didn't specify, shall I guess you used 42 parameters when you got this error? How about providing some context, a code snippet? Tell me what were trying to do?

Do your research. Provide an example. Show me you tried. And, in return, I will attempt to help you.
#8
12/21/2007 (9:33 am)
Function setLinearVelocity takes 2 parameters (or 1 parameter with two strings in it). You need the function setLinearVelocityX, which takes only 1 parameter and sets your left/right movement. My suggestion of using setLinearVelocity was a simple oversight on my part.
#9
12/21/2007 (6:17 pm)
Alright, I will post what ive done when I ask further questions.
#10
12/23/2007 (4:59 pm)
Next 2 questions:

1. What in the platformer tutorial causes the camera to follow the player, I want to make the camera so that it follows the player but doesn't go past the world boundaries. However I cant figure out what made the camera follow the player to start with (looking through the platformer tutorial)

2. I will eventually (months from now in milestone 6) be adding network code in for the eventual online aspect. Of course the first 6 milestones are player movement and simple platformer stuff. When I get to milestone 6 will I need a TGB pro licence to add network code or can it be done with the normal TGB license?