Jump to content
Sign in to follow this  
treoctone

Timer Script Question

Recommended Posts

I have written this timer script to display throughout the duration of a mission I am working on. I have read through and tried to determine how I could make a lot of the topics that already cover this subject(usually with laps) work, but in the end wrote it myself. The question I have is whether or not there is an easier way to do this without so much code? Thanks for any help and if anything someone else will be able to benefit from this.

//timer script
[] spawn
{	
if (isServer) then 
{
	_seconds = 0;
	_minutes = 0;
	_hours = 0;
	my_Timer = format ["0" + "%1" +":0"+ "%2" +":0"+ "%3", _hours, _minutes, _seconds ];

	disableSerialization;
	"myuniquelayer" call BIS_fnc_rscLayer cutRSC ["TreoHUD", "PLAIN"];

	waitUntil {!isNull (uiNameSpace getVariable "TreoHUD")}; 
	_display = uiNameSpace getVariable "TreoHUD";
	_setText = _display displayCtrl 1003;

	while {true} do
	{			
		_setText ctrlSetStructuredText (parseText format ["%1", my_Timer]);
		_setText ctrlSetBackgroundColor [0,0,0,0.5];

		_seconds = _seconds + 1;
		sleep 1;

		if (_seconds <=9 && _minutes <= 9 && _hours <= 9) then
		{	
			my_Timer = format ["0" + "%1" +":0"+ "%2" +":0"+ "%3", _hours, _minutes, _seconds ];							
		};

		if (_seconds >=10 && _minutes <= 9 && _hours <= 9) then
		{				
			my_Timer = format ["0" + "%1" +":0"+ "%2" +":"+ "%3", _hours, _minutes, _seconds];
		};

		if (_seconds >=10 && _minutes >= 10 && _hours <= 9) then
		{				
			my_Timer = format ["0" + "%1" +":"+ "%2" +":"+ "%3", _hours, _minutes, _seconds];
		};

		if (_seconds >=10 && _minutes >= 10 && _hours >= 10) then
		{				
			my_Timer = format ["%1" +":"+ "%2" +":"+ "%3", _hours, _minutes, _seconds];
		};

		if (_seconds >= 60) then
		{
			_minutes = _minutes + 1;
			_seconds = 0;
			if (_minutes >= 60) then
			{	
				_hours = _hours + 1;
				_minutes = 0;
			};
		};			
	};		
};
};

//dialogs
class RscTitles
{
class Default 
     {
      idd = -1;
      	fadein = 0;
      	fadeout = 0;
      	duration = 0;
     };
   	class TreoHUD
   	{

   		idd = 1000000;
     	movingEnable =  0;
     	enableSimulation = 1;
     	enableDisplay = 1;
      duration     =  1e+1000;
     	fadein       =  0.1;
      fadeout      =  2;
     	name = "TreoHUD";
	onLoad = "with uiNameSpace do { TreoHUD = _this select 0 }";

	class controls 
     	{

           	class RTA_Timer
           	{
            	access = 0;
                	type = 13;
     	          	idc = 1003;
           	    	style = 0x00;
               		lineSpacing = 1;
				x = 0.40 * safezoneW + safezoneX;
				y = 0.025 * safezoneH + safezoneY;
				w = 0.10 * safezoneW;
				h = 0.040 * safezoneH;
               		size = 0.020;
                	colorBackground[] = {0,0,0,0};
     	          	colorText[] = {1,1,1,1};
           	    	text = "";
               		font = "PuristaSemiBold";

			class Attributes
			{
				font = "PuristaSemiBold";
				color = "#FFFFFF";
				align = "CENTER";
				valign = "top";
				shadow = false;
				shadowColor = "#000000";
				underline = false;
				size = "2";
			}; 
           	};
	};
};
};

Share this post


Link to post
Share on other sites
I have written this timer script to display throughout the duration of a mission I am working on. I have read through and tried to determine how I could make a lot of the topics that already cover this subject(usually with laps) work, but in the end wrote it myself. The question I have is whether or not there is an easier way to do this without so much code? Thanks for any help and if anything someone else will be able to benefit from this.

//timer script
[] spawn
{	
if (isServer) then 
{
	_seconds = 0;
	_minutes = 0;
	_hours = 0;
	my_Timer = format ["0" + "%1" +":0"+ "%2" +":0"+ "%3", _hours, _minutes, _seconds ];

	disableSerialization;
	"myuniquelayer" call BIS_fnc_rscLayer cutRSC ["TreoHUD", "PLAIN"];

	waitUntil {!isNull (uiNameSpace getVariable "TreoHUD")}; 
	_display = uiNameSpace getVariable "TreoHUD";
	_setText = _display displayCtrl 1003;

	while {true} do
	{			
		_setText ctrlSetStructuredText (parseText format ["%1", my_Timer]);
		_setText ctrlSetBackgroundColor [0,0,0,0.5];

		_seconds = _seconds + 1;
		sleep 1;

		if (_seconds <=9 && _minutes <= 9 && _hours <= 9) then
		{	
			my_Timer = format ["0" + "%1" +":0"+ "%2" +":0"+ "%3", _hours, _minutes, _seconds ];							
		};

		if (_seconds >=10 && _minutes <= 9 && _hours <= 9) then
		{				
			my_Timer = format ["0" + "%1" +":0"+ "%2" +":"+ "%3", _hours, _minutes, _seconds];
		};

		if (_seconds >=10 && _minutes >= 10 && _hours <= 9) then
		{				
			my_Timer = format ["0" + "%1" +":"+ "%2" +":"+ "%3", _hours, _minutes, _seconds];
		};

		if (_seconds >=10 && _minutes >= 10 && _hours >= 10) then
		{				
			my_Timer = format ["%1" +":"+ "%2" +":"+ "%3", _hours, _minutes, _seconds];
		};

		if (_seconds >= 60) then
		{
			_minutes = _minutes + 1;
			_seconds = 0;
			if (_minutes >= 60) then
			{	
				_hours = _hours + 1;
				_minutes = 0;
			};
		};			
	};		
};
};

//dialogs
class RscTitles
{
class Default 
     {
      idd = -1;
      	fadein = 0;
      	fadeout = 0;
      	duration = 0;
     };
   	class TreoHUD
   	{

   		idd = 1000000;
     	movingEnable =  0;
     	enableSimulation = 1;
     	enableDisplay = 1;
      duration     =  1e+1000;
     	fadein       =  0.1;
      fadeout      =  2;
     	name = "TreoHUD";
	onLoad = "with uiNameSpace do { TreoHUD = _this select 0 }";

	class controls 
     	{

           	class RTA_Timer
           	{
            	access = 0;
                	type = 13;
     	          	idc = 1003;
           	    	style = 0x00;
               		lineSpacing = 1;
				x = 0.40 * safezoneW + safezoneX;
				y = 0.025 * safezoneH + safezoneY;
				w = 0.10 * safezoneW;
				h = 0.040 * safezoneH;
               		size = 0.020;
                	colorBackground[] = {0,0,0,0};
     	          	colorText[] = {1,1,1,1};
           	    	text = "";
               		font = "PuristaSemiBold";

			class Attributes
			{
				font = "PuristaSemiBold";
				color = "#FFFFFF";
				align = "CENTER";
				valign = "top";
				shadow = false;
				shadowColor = "#000000";
				underline = false;
				size = "2";
			}; 
           	};
	};
};
};

-_-

you really didn't need to go through all that trouble to get the time right, BIS_fnc_timetostring could help cut the work down in the script by about a forth.

Share this post


Link to post
Share on other sites

_seconds = 0;
_secondsTemp = 0;
_minutes = 0;
_hours = 0;

while {true} do
{	
_timer = [] spawn
{
	uiSleep 1; //uiSleep uses system time rather than in-game timekeeping (which is tied to framerate) and is therefore more accurate
};
_seconds = _seconds + 1;
_hours = floor (_seconds / 3600);
_minutes = floor((_seconds - (_hours * 3600)) / 60);
_secondsTemp = floor (_seconds - (_hours * 3600) - (_minutes * 60));
if (_hours < 10) then
{
	_hours = "0" + (str _hours);
};
if (_minutes < 10) then
{
	_minutes = "0" + (str _minutes);
};
if (_secondsTemp < 10) then
{
	_secondsTemp = "0" + (str _secondsTemp);
};

waitUntil {scriptDone _timer}; //all math should be done before before the script has finished,
//this prevents the CPU from simply sitting idle and then wasting time number crunching when it should be displaying information

hint format ["%1:%2:%3", _hours, _minutes, _secondsTemp]; //after script is finished then the proper amount of time has passed, display the info
};

That's as efficient in every aspect as I can make this in my current state of inebriation (script size, script speed, cpu time wasted). Also, the total amount of seconds that has passed is preserved so that it can be used elsewhere as an event trigger. With 2 small changes you can modify this from counting up to counting down and make some other cool things.

Actually, I think this is so cool that I will post this as a code snippet

Edited by DreadedEntity

Share this post


Link to post
Share on other sites

@austin_medic thanks for poinitng me in the direction of BIS_FNC_timetostring. I messed around with it a little and will definitely use it when I just need to manipulate daytime time. I didn't mention that my future goal is to be able to save record time to profilenamespace and display that in the mission. The BIS function unfortunately will not allow me to do that since it is only a string.

@DreadedEntity I like your code more than mine and have added it to my code. I wish there was an easier way to save the whole string to profilenamespace without having to save each _hour, _minute, and _second to individual profilenamespace variables and then convert them to spring after using getvariable, but it doesn't look like that is possible.

I appreciate both replies.

Share this post


Link to post
Share on other sites
I wish there was an easier way to save the whole string to profilenamespace without having to save each _hour, _minute, and _second to individual profilenamespace variables and then convert them to spring after using getvariable, but it doesn't look like that is possible.

Like this?:

profileNamespace setVariable ["timeVariable",format ["%1:%2:%3", _hours, _minutes, _secondsTemp]];

_string = profileNamespace getVariable ["timeVariable",""];//returns "_hours:_minutes:_secondsTemp" (where the variables are the actual values of course)

Another alternative:

profileNamespace setVariable ["timeVariable",(_hours + ":" + _minutes + ":" + _secondsTemp)];

Edited by JShock

Share this post


Link to post
Share on other sites

Thanks or that JShock it helps out a lot. Sorry for such a delayed response though, it has been a hectic last couple of days.

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
Sign in to follow this  

×