Game Development Community

Display timer countdown on the screen.

by Daniel J Lee · in Torque Game Engine · 10/31/2002 (10:54 am) · 8 replies

Hi Guys,

How can I display timer countdown in the main play window? I am using schedule in script to get the timer countdown.

Any Ideas? Please let me know.

Thanks.

#1
10/31/2002 (11:11 am)
I imagine that you could schedule an update for every second, or you could modify the default Torque clock HUD element to allow countdowns and not have to schedule anything except the final event.
#2
10/31/2002 (11:15 am)
You could use the default game/fps/guiHudClock.cc and simply modify getTime() to show the remaining time instead of the passed time:
F32 GuiClockHud::getTime()
{
   // Return elapsed time in seconds.
   // beffy: changed to return the remaining mission time...
   const char*  gameDuration = Con::getVariable("Game::Duration");
   return dAtof(gameDuration) - F32(mTimeOffset + Platform::getVirtualMilliseconds()) / 1000;
}
#3
10/31/2002 (11:26 am)
Yes, something like that
#4
10/31/2002 (11:42 am)
You'll have to make a schedule however to constantly send time updates to the clients. Every 15 to 30 seconds will do just fine.

The reasoning is because the clients will eventually start to have differing times and you want them all to be sync'ed up correctly especially if your game is time dependant.
#5
10/31/2002 (12:43 pm)
Thanks for the quick responses. Wow! ^^

Here is what I am trying to accomplish. I want to create a timed turn based game. The basic idea is that all the players or clients (bots included) have this timed free roam period that they can run around and shoot each other. But when the roam time is over, everyone is frozen (the object control is switched over to the orbiting camera) except one player who gets a turn. The player cannot move but he can perform actions like shooting a missile at other frozen players. This frozen time is also timed as well. The frozen players can look around with the camera. When the player in turn performs an action, the frozen timer is canceled and the other players will be thawed once again and be able to move. And the next player in queue will be notified that he is next. Nobody else would no that. So the player that runs right next to you could be next and you will get your butt kicked!. Anyways, like Robert said, I need all the clients to be synced up. This brings a good question for me.

Here is the code script of what I have gotten so far in server\scripts\game.cs:

function GameConnection::createPlayer(%this, %spawnPoint)
{
if (%this.player > 0) {
// The client should not have a player currently
// assigned. Assigning a new one could result in
// a player ghost.
error( "Attempting to create an angus ghost!" );
}

// Create the player object
%player = new Player() {
dataBlock = LightMaleHumanArmor;
client = %this;
};
MissionCleanup.add(%player);

// Player setup...
%player.setTransform(%spawnPoint);
%player.setEnergyLevel(60);
%player.setShapeName(%this.name);

// Update the camera to start with the player
%this.camera.setTransform(%player.getEyeTransform());

// Give the client control of the player
%this.player = %player;
%this.setControlObject(%player);

// Start the Roam Time
$Game::Schedule = schedule($Game::Roam * 1000, 0, "OnRoamEnd", %this);
}

//-----------------------------------------------------------------------------
// Player roam and freeze functions
//-----------------------------------------------------------------------------
function OnRoamEnd(%this)
{
if ($Game::Roam)
{
%control = %this.getControlObject();
%control = %this.camera;
%this.setControlObject(%control);
bottomPrintAll("Roam is over", 1, 1);
//echo("Roam is over!");
$Game::Schedule = schedule($Game::Freeze * 1000, 0, "OnFreezeEnd", %this );
}
}

function OnFreezeEnd(%this)
{
if ($Game::Freeze)
{
%control = %this.getControlObject();
%control = %this.player;
%this.setControlObject(%control);
bottomPrintAll("Freeze is over", 1, 1);
//echo("Freeze is over!");
$Game::Schedule = schedule($Game::Roam * 1000, 0, "OnRoamEnd", %this );
}
}


As you can see, I have created a global variable called Roam and Freeze that are part of Game class. I also created the schedule under Game class that are global. Does that mean that all the client will be synced up. I am not sure yet. By the way, this code is my older one. The new one that I have at home has more advanced stuff like orbitting camera kinda like the ones in Tribes2.

I think if I can resolve the syncing and displaying issue. I should be okay. I still need to research little bit how to rotate the turns.

I have made lots of models in lightwave. I feel pretty comfortable about modeling. Little bit less on texturing, but I will eventually get there. Thanks for all your help, and I will post a plan with few shots of the models soon.

Torque scripting is fun. Coming from embedded world of programming... It is little different, but still fun.

Thanks guys!!!
#6
11/01/2002 (12:18 pm)
Hi Guys,

I think I have it figured out. It is working. At least with one client. I have to try multiple clients.

Thanks anyways.
#7
09/15/2003 (10:33 am)
Man this is an old topic, but I've got questions :)

I noticed that once you are in game the clock has already ran for ~10 seconds, causing a bit of unwanted timer time ;) Has anyone looked as a resolution for this? I was thinkin of doing a schedule to kick off the timer in 10 seconds, but that is a hack and a kludge ;)

The ideal solution would be to not start the timer until the player is actualy in game (this is SP only), but when is the player officaly in the game?

-Ron
#8
06/01/2007 (5:55 pm)
Quick update on how to get the countdown timer working cleanly with TGE 1.5.2

First, go into starter.fps\client\scripts\client.cs and find function clientCmdSyncClock(%time). You may notice that it is empty! That's no good =P Replace it with:

function clientCmdSyncClock(%time)
{
   // Time update from the server, this is only sent at the start of a mission
   // or when a client joins a game in progress.
   if (isObject(HudClock))
      HudClock.setTime(%time);
}

This, of course, means that you need to name your guiClockHud HudClock.

At this point you should have a pretty accurate countdown clock the first time you run a mission but it is most likely going into the negatives after the mission cycles.

To fix that you need to drill down into starter.fps\server\scripts\game.cs and find function startGame(). Just after the if ($Game::Running) {} block of code add $Game::StartTime = $Sim::Time; so that the top of your function startGame() looks like this:

function startGame()
{
   if ($Game::Running) {
      error("startGame: End the game first!");
      return;
   }
   
   $Game::StartTime = $Sim::Time;

   // Inform the client we're starting up

Now your countdown clock should be correct for both game duration mission cycles and for high score mission cycles.