Jump to content
ItsBasicallyAneko

Countdown timer script HELP!

Recommended Posts

So, I'm trying to make a MP countdown timer script for players to hold a certain point for a x amount of time. The problem is, I don't know if this script functional for MP and to deactivate it after timer reaches 00:00 so that it doesn't stay there for the entirety of the mission. Any assistance on this will be greatly appreciated!

 

Code:

Spoiler

 

//function:
TAG_fnc_coloredCountdownHint = {
    _color = "#45f442";//green
    _timeLeft = TAG_fnc_countdownEndTime - time;
    if (_timeLeft < 16) then {_color = "#eef441";};//yellow
    if (_timeLeft < 6) then {_color = "#ff0000";};//red
    if (_timeLeft < 0) exitWith {
        //exit and remove eventhandler while politely closing the door
        removeMissionEventhandler ["EachFrame",_thisEventHandler];
        hintSilent parseText format ["<t color='%1'>--- Time is up! ---</t>",_color];
    };
    hintSilent parseText format ["Time Left:<br/><t color='%1'>--- %2 ---</t>", _color, [(_timeLeft/3600),"HH:MM:SS"] call BIS_fnc_timetostring];
};

//calling the function:
_duration = 20;
TAG_fnc_countdownEndTime = time + _duration;
addMissionEventHandler ["EachFrame",{[] call TAG_fnc_coloredCountdownHint}];


 

 

Share this post


Link to post
Share on other sites

Perfectly local but this is not a problem. What you need is a common reference of time for all players.

 

You don't need to use an "on each Framed" EH. Your countdown changes every second.

You can stay local (hintSilent) for display. No need to broadcast a display every second! All computers can manage a second of time.

 

See bis_fnc_countDown .

  • Like 1

Share this post


Link to post
Share on other sites
16 hours ago, pierremgi said:

Perfectly local but this is not a problem. What you need is a common reference of time for all players.

 

You don't need to use an "on each Framed" EH. Your countdown changes every second.

You can stay local (hintSilent) for display. No need to broadcast a display every second! All computers can manage a second of time. 

 

See bis_fnc_countDown .

Yeah the problem is, this is kinda a first time so I'm not really familiar with scripting.

Share this post


Link to post
Share on other sites

To be placed in a running trigger (not server only) or init.sqf waiting for a condition:


 

0 = [] spawn {
  private _duration = 20;
  private "_color";
  if (isServer) then {[_duration,true] call BIS_fnc_countdown};
  waitUntil {!isNil "BIS_fnc_countdown_time"};
  _time = BIS_fnc_countdown_time;

  for "_i" from _duration to 0 step -1 do {
    call {
      if (_i < 6) exitWith {_color = "#ff0000"};
      if (_i < 16) exitWith {_color = "#eef441"};
      _color = "#45f442";
    };
    _tt = if isMultiplayer then [{serverTime},{time}];
    hintSilent parseText format ["Time Left:<br/><t color= '%1'>--- %2 ---</t>", _color, [(_time - _tt)/3600,"HH:MM:SS"] call BIS_fnc_timetostring];
    uisleep 1;
  };
  
  uisleep 1;
  hintSilent parseText format ["<t color='%1'>--- Time is up! ---</t>",_color];
};

 

  • Like 1

Share this post


Link to post
Share on other sites

You could also calculate the text color based on time left, by using linearConversion for red and green.

Also BIS_fnc_secondsToString is most probably better seeing as you are not using a daytime format.

Spoiler

0 = [] spawn {
	private _duration = 20;
	
	if (isServer) then { [ _duration, true ] call BIS_fnc_countdown };
	
	waitUntil{ [ true ] call BIS_fnc_countdown };
	
	while{ [ true ] call BIS_fnc_countdown } do {
		private _timeLeft = [ 0 ] call BIS_fnc_countdown;
		private _colorHTML = [
			linearConversion[ 0, _duration, _timeLeft, 1, 0 ],
			linearConversion[ 0, _duration, _timeLeft, 0, 1 ], 
			0
		] call BIS_fnc_colorRGBtoHTML;
		
		hintSilent parseText format[ "Time Left:<br/><t color= '%1'>--- %2 ---</t>", _colorHTML, [ _timeLeft, "HH:MM:SS" ] call BIS_fnc_secondsToString ];
		uiSleep ( _timeLeft min 1 );
	};

	hintSilent parseText "<t color='#FF0000'>--- Time is up! ---</t>";
};

 

 

 

  • Like 4

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

×