Game Development Community

dev|Pro Game Development Curriculum

Countdown Clock

by Nick Matthews · 01/18/2006 (5:19 pm) · 20 comments

Basically you just need to add the following to your playGui.gui:
new GuiClockHud(CDBG) {
      profile = "GuiDefaultProfile";
      horizSizing = "left";
      vertSizing = "bottom";
      position = "274 5";
      extent = "45 25";
      minExtent = "8 8";
      visible = "0";
      helpTag = "0";
      showFill = "1";
      showFrame = "0";
      fillColor = "0.000000 0.000000 1.000000 0.500000";
      frameColor = "0.000000 1.000000 0.000000 1.000000";
      textColor = "0.000000 0.000000 0.000000 0.000000";
   };
   new GuiClockHud(CDBGR) {
      profile = "GuiDefaultProfile";
      horizSizing = "left";
      vertSizing = "bottom";
      position = "274 5";
      extent = "45 25";
      minExtent = "8 8";
      visible = "0";
      helpTag = "0";
      showFill = "1";
      showFrame = "0";
      fillColor = "1.000000 0.000000 0.000000 0.500000";
      frameColor = "0.000000 1.000000 0.000000 1.000000";
      textColor = "0.000000 0.000000 0.000000 0.000000";
   };
   new GuiTextCtrl(CDTime) {
      profile = "InventoryTextProfile";
      horizSizing = "left";
      vertSizing = "bottom";
      position = "276 8";
      extent = "42 18";
      minExtent = "8 2";
      visible = "0";
      helpTag = "0";
      text = "00:00";
      maxLength = "255";
   };

And then this Script stuff goes into the client.cs file:

function clientCmdunpauseCDC()
{
                  CDM();
}
function clientCmdpauseCDC()
{
                 cancel($CDS);
}
function clientCmdstopCDC()
{
	cancel($CDS);
	CDTime.visible = 0;
	CDBG.visible = 0;
	CDBGR.visible = 0;
}
function clientCmdsetCDC(%min)
{
	cancel($CDS);
	CDTime.visible = 1;
	CDBG.visible = 1;
	CDTime.setValue(%min@":00");
	$Min = %min;
	$Sec = "a";
	CDM();
}

function CDM()
{
	if($Sec $= "a")
	{
	$Min--;
	$Sec = "59";
	}
	else
	{
	$Sec--;
	}

	if($Sec $= "0" && $Min $= "0")
	{
	CDTime.visible = 0;
	CDBGR.visible = 0;
	cancel($CDS);
	}
	else
	{

	if($Sec $= "-1")
	{
	$Sec = "59";
	$Min--;
	}
	if($Sec <= "9")
	{
	$Zero = "0";
	}
	else
	{
	$Zero = "";
	}
	if($min <= "9")
	{
	$Zero2 = "0";
	}
	else
	{
	$Zero2 = "";
	}
	if($Min $= "0" && $Sec $= "10")
	{
	CDBGR.visible = 1;
	CDBG.visible = 0;
	}

	CDTime.setValue($Zero2@$Min@":"@$Zero@$sec);
	%cdschedule = Schedule(1000,0,"CDM");
	$CDS = %cdschedule;
	}
	
}

Basically you just do:
commandtoclient(%client, "setCDC", 2); to set the client's countdown to 2 minutes.
You can do:
commandtoclient(%client,"stopCDC"); to stop the clock altogether.
Then there is:
commandtoclient(%client,"pauseCDC"); and commandtoclient(%client,"unpauseCDC"); to pause/unpause.

I hope this resource proves useful!

#1
01/17/2006 (3:00 am)
#2
01/19/2006 (7:17 pm)
Extremely useful, thank you very much!
#3
01/20/2006 (6:59 pm)
I put this in my test script folder last night and while the code added fine i still cant see a clock counting down after you send the command. Am i missing a step i checked the console log and it doesnt say much.
#4
01/24/2006 (7:46 am)
Perhaps you don't have the right profiles? Try adding the guis manually through the editor, and remember to name them the same, then try it. Hope it works for you :)
#5
01/26/2006 (6:59 am)
this is exaclty what i need! Now I gotta start a new project, but hell it is fun!

Thanks!
#6
02/23/2006 (9:43 pm)
This is probably a stupid question but if you don't ask you never learn. Will this work across a network? IE will all players 'clock gui's' be synced?
#7
02/24/2006 (12:11 am)
oh no, this is an individual thing. If you want them all to be synched then you need to do probably a for() on all the clients, and give them all a commandtoclient.

This was done for a "building game" called Blockland, where I made a little game mode where you have a set time to build something, and i used this to give all clients playing, a little countdown clock so they know when their time is up.
#8
02/24/2006 (12:46 am)
This might be a silly question, but where does this go?

commandtoclient(%client, "setCDC", 2);
commandtoclient(%client,"stopCDC");
commandtoclient(%client,"pauseCDC");
commandtoclient(%client,"unpauseCDC");
#9
02/27/2006 (3:03 am)
Oh, where ever you want to start the countdown. Like if you had the function "startGame();":

function startGame()
{
        blahblahrandomcode();
        //Insert whatever code you need here, im guessing its the setCDC one.
        commandtoclient(%client, "setCDC", 2);
}

If you just want to test it, go into the console ingame and type "clientCmdsetCDC("2");".

Hope this helps!
#10
03/29/2006 (6:52 am)
Simple and helpful! :)
Just in Time as i have needed it ;)
Thx.
#11
03/29/2006 (5:41 pm)
perfect Nick, thanx. I can hide out now until the terany of my uber-powered bots has ended ;)
#12
10/20/2007 (9:25 pm)
(Newbie) Question:

I can get the timer to display but I can't get it to begin the countdown. The only command that gets the timer to start the countdown is

clienCmdsetCDC("2");

As suggested by Nick Matthews. How do I get the timer to begin the countdown automatically as soon as I start a mission? Is there something I should change in the client.cs file or is there something else?
-Thanks in advance : )
#13
11/02/2007 (5:30 pm)
Well it depends if the game is singleplayer or not I guess. If it's single player then just find the function onServerCreated in game.cs and at the end of that function add clientCmdSetCDC(NumberOfMinutes); and it'll countdown as the server is created.

Alternatively if you're doing it multiplayer you can get the function GameConnection::onClientEnterGame from missiondownload.cs and just add commandtoclient(%client,'setCDC',numberofMinutes); and it should solve your problems. Hope that helps somewhat.
#14
01/11/2008 (6:06 am)
Cool, Thank you. :-)
#15
03/09/2008 (9:19 pm)
(Newbie Question)

I'm sorry...a little embarrassed to ask this; but where can I find the client.cs?? I have a clientGame.cs; But that's it. Any help??
#16
04/01/2008 (11:32 am)
This will work great for my RTS game

Thank You
#17
06/18/2008 (4:18 pm)
@Jorge: It can go in basically any of your client.cs files. I think clientGame.cs would be fine (assuming it is in starter.fps/client or base/client.
#18
06/24/2008 (6:50 am)
I have got a (newbie) question:
I've got the timer working. But nothing happens when it hits 0. How can I apply a function to it?
Tnx
#19
06/25/2008 (3:51 am)
In the function CDM() look for this bit:

if($Sec $= "0" && $Min $= "0")
	{
	CDTime.visible = 0;
	CDBGR.visible = 0;
	cancel($CDS);
	}

You could easily put in a function call under the cancel($CDS) bit like so:

if($Sec $= "0" && $Min $= "0")
	{
	CDTime.visible = 0;
	CDBGR.visible = 0;
	cancel($CDS);
        [b]cow("moo");[/b]
	}
#20
07/14/2008 (10:30 pm)
I just want to say thanks for posting this, It helped me a lot. Seriously.

-Cappa