April 18, 2024, 10:31:35 pm

Author Topic: [Basic] Countdown announcing  (Read 1464 times)

0 Members and 1 Guest are viewing this topic.

morphine

  • Crazy Man
  • *****
  • Posts: 1909
  • Country: lt
    • View Profile
[Basic] Countdown announcing
« on: May 10, 2012, 03:06:53 pm »
You may usually see a little digital count-down clock when you play on a VC-MP server which runs a timed gamemode, for example CTF, A/D or racing timelaps. Making such a count-down clock is relatively easy but it requires a bit of thinking. Just because I had nothing better to do, I decided to write a short tutorial on how to do it. This works, by the way.  :)

Code: [Select]
// This isn't really useful as a stand-alone script; I'm just trying to get the idea of countdown announces across.
// Note, .Stop(); is the equivalent of pausing a round in an A/D gamemode, it does not reset the seconds and/or minutes. I've made a command dedicated to that, called "reset".

// First we'll declare a few global variables for this snippet.

minutes <- 0; // This will be your minute counter.
seconds <- 0; // This will be your second counter.
isTimerRunning <- false; // This will decide when to run the announce timer and when not to.

// Now to loading stuff.

function onScriptLoad()
{
try
{
// Reset all values.
minutes = 0;
seconds = 0;
if( isTimerRunning ) isTimerRunning = false;
announceTimer <- NewTimer( "announcer", 1000, 0 ); // The timer itself.
announceTimer.Stop();
print( "Loaded." );
}
catch( error ) print( "onScriptLoad error: " + error );
}

// A few needed functions.

function NumTok(string, separator)
{
local tokenized = split(string, separator);
return tokenized.len();
}
function GetTok(string, separator, n, ...)
{
local m = vargv.len() > 0 ? vargv[0] : n,
  tokenized = split(string, separator),
  text = "";

if (n > tokenized.len() || n < 1) return null;
for (; n <= m; n++)
{
text += text == "" ? tokenized[n-1] : separator + tokenized[n-1];
}
return text;
}

// Command stuff.

function onPlayerCommand( player, command, text )
{
if( command == "minlimit" )
{
if( !text ) PrivMessage( "/c minlimit <newMinuteValue> or 'current' to get the current value.", player );
else if( text.tolower() == "current" )
PrivMessage( "The current starting minute is set to " + minutes + ".", player );
else if( IsNum( text ) )
{
local newMin = GetTok( text, " ", 1 );
if( !IsNum( newMin ) ) PrivMessage( "The new minute value has to be an integer.", player );
else
{
minutes = newMin.tointeger();
PrivMessage( "Minute value updated to " + minutes + ".", player );
}
}
}
else if( command == "announcer" )
{
if( !text ) PrivMessage( "/c announcer on/off", player );
else
{
if( text.tolower() == "on" )
{
if( isTimerRunning ) PrivMessage( "There's an announcer already running.", player );
else
{
isTimerRunning = true;
PrivMessage( "Starting the timer...", player );
announceTimer.Start();
}
}
else if( text.tolower() == "off" )
{
if( !isTimerRunning ) PrivMessage( "There is no announcer to stop to begin with.", player );
                                else
{
                                        isTimerRunning = false;
PrivMessage( "Stopping the timer...", player );
announceTimer.Stop();
}
}
}
}
else if( command == "reset" )
{
if( !text ) PrivMessage( "/c reset min/sec", player );
else
{
if( text.tolower() == "min" )
{
minutes = 0;
PrivMessage( "Minutes reset.", player );
}
else if( text.tolower() == "sec" )
{
seconds = 0;
PrivMessage( "Seconds reset.", player );
}
}
}
}

// The announcer himself.

function announcer()
{

  seconds--;

        if( seconds < 0 )
        {
                seconds = 59;
                minutes--;
                if( minutes == 0 )
                {
                        Message( "Timer stopped." );
                        seconds = 0;
                        announceTimer.Stop();
                }
        }


if( seconds < 10 ) AnnounceAll( "~y~" + minutes + ":0" + seconds, 1 );
else AnnounceAll( "~y~" + minutes + ":" + seconds, 1 );

}


This is a command-based mechanism but it should be relatively easy for people with medium-sized+ brains to implement this any other way. (That's why I didn't post this on the VC:MP forum :angel:)

Agree Disagree Funny Winner Pwnt Informative Friendly Useful Optimistic Artistic Late Brain Donor


Charley

  • Vice Underdog
  • Crazy Man
  • *
  • *
  • Posts: 4751
  • Country: 00
    • View Profile
Re: [Basic] Countdown announcing
« Reply #1 on: May 10, 2012, 05:07:31 pm »
Nice one, might be better placed in the snippets topic though.
Agree Disagree Funny Winner Pwnt Informative Friendly Useful Optimistic Artistic Late Brain Donor

Writer of excessively long posts


morphine

  • Crazy Man
  • *****
  • Posts: 1909
  • Country: lt
    • View Profile
Re: [Basic] Countdown announcing
« Reply #2 on: May 10, 2012, 05:15:26 pm »
Oh right, forgot about that. Merge it then if needed.
Agree Disagree Funny Winner Pwnt Informative Friendly Useful Optimistic Artistic Late Brain Donor


tato

  • Vice Underdog Rookie (Inactive)
  • Fanatic
  • *
  • Posts: 134
    • View Profile
    • Extreme Addicts
Re: [Basic] Countdown announcing
« Reply #3 on: May 16, 2012, 06:49:08 am »
Pretty cool, nice
Agree Disagree Funny Winner Pwnt Informative Friendly Useful Optimistic Artistic Late Brain Donor