Page 2 of 2
Re: [COD4] Adding a timer to codjumper mod
Posted: April 26th, 2012, 2:28 pm
by Rezil
MORGOTH wrote:Thanks anyway... since i don't have any basis of coding and i don't know where to start from i was looking for a "ready-made" solution like you said.
Add this to your gsc(together with the code posted above). Set developer to 2. Test. If/when it crashes, post the error here.
Code: Select all
init()
{
    precachestring(&":");
    precachestring(&" ");
   Â
    level.clientid = 0;
    level thread onPlayerConnect();
}
Â
onPlayerConnect()
{
    while( true )
    {
        level waittill("connecting", player);
     Â
        player.clientid = level.clientid;
        level.clientid++;
     Â
        player.isTiming = false;
     Â
        player thread onPlayerSpawned();
    }
}
Â
onPlayerSpawned()
{
    self endon("disconnect");
 Â
    while( true )
    {
        self waittill( "menuresponse", menu, response );
        if( menu == "cj" )
        {
            if( response == "stopwatchstart" && !self.isTiming) {
                self playerInit();
                self stopWatchInit(100, 0, 30);
                self miscHUDinit(35);
               Â
                self.isTiming = true;
                self thread monitorTime();
            }
            if( response == "stopwatchstop" && self.isTiming)
                self.isTiming = false;
        }
        wait 0.5;
    }
}
[/size]
Re: [COD4] Adding a timer to codjumper mod
Posted: April 26th, 2012, 5:41 pm
by MORGOTH
First of all: many thanks for your time and patience with me
second: i made some tests and it works, but i found that when i stop it, it always print the sentence "You've been going for a day now, don't you think it's time to quit already?". Furthermore when i stop it, the time stays printed on the screen and if i start another timer it just shows on the previous stopped...
Re: [COD4] Adding a timer to codjumper mod
Posted: April 26th, 2012, 8:54 pm
by Drofder2004
Just remove the entire line from the code, it is only put there as a small 'fun factor' and is not crucial to the working.
If you DO want to keep it, add
To a new line above it.
Re: [COD4] Adding a timer to codjumper mod
Posted: April 26th, 2012, 8:58 pm
by Drofder2004
Rezil wrote:The break statements are needed because you're in a nested loop
And yeah, I could've written this much more efficiently but in my defence I was planning on optimizing this code next week.
Yeh, wasn't quite thinking properly last night, was looking at while loop more as a "stop counting" loop instead of its real purpose of counting beyond the hour.
In theory, it probably isn't as program heavy as it looks as there is still only ever one loop running, but just for simplicity it is out there in the over-complicated stages

Re: [COD4] Adding a timer to codjumper mod
Posted: April 26th, 2012, 9:29 pm
by Rezil
Drofder2004 wrote:Just remove the entire line from the code, it is only put there as a small 'fun factor' and is not crucial to the working.
If you DO want to keep it, add
To a new line above it.
**:
because we want to display it only if the timer is still running.
Drofder2004 wrote:In theory, it probably isn't as program heavy as it looks as there is still only ever one loop running, but just for simplicity it is out there in the over-complicated stages

As long as it makes sense to me I'm fine with it.
--
Anyway, here's your updated code, it should work as intended now:
Code: Select all
/*
- Call playerInit(), stopwatchInit(x, y, offset) and miscHUDinit(offset) on a player
(eg. stopWatchInit(100, 0, 30);, miscHUDinit(35); )
- When you're ready to start the stopwatch, set isTiming to true and thread monitorTime()
- When you're done with the timing, simply set isTiming to false
*/
init()
{
precachestring(&":");
level thread onPlayerConnect();
}
onPlayerConnect()
{
while( true )
{
level waittill("connecting", player);
player.isTiming = false;
player thread onPlayerSpawned();
}
}
onPlayerSpawned()
{
self endon("disconnect");
while(true)
{
self waittill( "menuresponse", menu, response );
if( menu == "cj" )
{
if( response == "stopwatchstart") {
if(!self.isTiming)
{
self playerInit();
self stopWatchInit(100, 0, 30);
self miscHUDinit(35);
self startTiming();
} else self iprintln("Stopwatch already running!");
}
if( response == "stopwatchstop")
if(self.isTiming)
self stopTiming();
else self iprintln("Stopwatch hasn't started!");
if( response == "stopwatchdestroy")
self destroyStopwatch();
}
wait 0.5;
}
}
playerInit()
{
if(!isDefined(self.stopwatchCounter))
{
self.stopwatchCounter = [];
self.stopwatchCounter[0] = 0; //seconds
self.stopwatchCounter[1] = 0; //minutes
self.stopwatchCounter[2] = 0; //hours
//(could've used the loop below but wanted to put more emphasis on what each element does)
}
if(!isdefined(self.stopwatch) && !isdefined(self.misc))
{
self.stopwatch = [];
self.misc = [];
for(i=0;i<3;i++)
{
self.stopwatch[i] = newclientHUDelem(self);
if(i<2) self.misc[i] = newclientHUDelem(self);
}
}
}
/*x and y are self explanatory, offset is the length between each element(sec, min, hr) */
stopwatchInit(x_v, y_v, offset)
{
for(i=0;i<self.stopwatch.size;i++)
{
if(i==0) self.stopwatch[i].alignX = "left";
else self.stopwatch[i].alignX = "center";
self.stopwatch[i].alignY = "middle";
self.stopwatch[i].horzAlign = "left";
self.stopwatch[i].vertAlign = "middle";
if(i==0)
{
self.stopwatch[i].x = x_v;
self.stopwatch[i].y = y_v;
}
else
{
self.stopwatch[i].x = self.stopwatch[i-1].x-offset;
self.stopwatch[i].y = self.stopwatch[i-1].y;
}
self.stopwatch[i].foreground = 1;
self.stopwatch[i].color = (1, 1, 1);
self.stopwatch[i].alpha = 1;
self.stopwatch[i].font = "default";
self.stopwatch[i].fontScale = 2;
}
updateStopwatchHUD();
}
miscHUDinit(offset) /*[0] is the : in between hours and minutes, [1] is the : inbetween minutes and seconds*/
{
for(i=0;i<self.misc.size;i++)
{
self.misc[i].alignY = "middle";
self.misc[i].alignX = "left";
self.misc[i].horzAlign = "left";
self.misc[i].vertAlign = "middle";
self.misc[i].font = "default";
self.misc[i].fontScale = 2;
self.misc[i].foreground = 1;
self.misc[i].color = (1, 1, 1);
self.misc[i].alpha = 0;
self.misc[i].x = self.stopwatch[i].x - (offset/2);
self.misc[i].y = self.stopwatch[i].y;
self.misc[i] setText(&":");
}
}
monitorTime()
{
self thread stopWatchColon();
while(self.isTiming)
{
for(h=0;h<24;h++)
{
self.stopwatchCounter[2] = h;
for(i=0;i<60;i++)
{
self.stopwatchCounter[1] = i;
for(j=0;j<600;j++)
{
self.stopwatchCounter[0] = j/10;
self updateStopwatchHUD();
wait 0.1;
if(!self.isTiming)
break;
}
if(!self.isTiming)
break;
}
if(!self.isTiming) //no 'goto' keyword to break out of all loops :(
break;
}
if(self.isTiming)
self iprintlnbold("You've been going for a day now, don't you think it's time to quit already?");
wait 0.05;
}
}
stopwatchColon()
{
while(self.isTiming)
{
if(self.misc[0].alpha==1)
self.misc[0].alpha = 0;
else self.misc[0].alpha = 1;
if(self.misc[1].alpha==1)
self.misc[1].alpha = 0;
else self.misc[1].alpha = 1;
wait 0.5;
}
}
updateStopwatchHUD()
{
if(isdefined(self.stopwatch))
{
for(i=0;i<self.stopwatch.size;i++)
self.stopwatch[i] setValue(self.stopwatchCounter[i]);
}
}
startTiming()
{
self.isTiming = true;
for(i=0;i<self.stopwatchCounter.size;i++)
{
self.stopwatchCounter[i] = 0;
self.stopwatch[i] setValue(self.stopwatchCounter[i]);
if(i<2) self.misc[i] setText(&":");
}
self thread monitorTime();
}
stopTiming()
{
self.isTiming = false;
self iprintln("Time: "+self.stopwatchCounter[2]+":"+self.stopwatchCounter[1]+":"+self.stopwatchCounter[0]);
}
destroyStopwatch()
{
if(isDefined(self.stopwatch) && isDefined(self.misc))
{
for(i=0;i<self.stopwatch.size;i++)
{
self.stopwatch[i] destroy();
self.stopwatch[i] = undefined;
if(i<2)
{
self.misc[i] destroy();
self.misc[i] = undefined;
}
}
self.stopwatch = undefined;
self.misc = undefined;
}
}
[/size]
Credit goes to CoDJumper.com modding forum.

Re: [COD4] Adding a timer to codjumper mod
Posted: April 26th, 2012, 11:47 pm
by MORGOTH
Many thanks, worked like a charm. Thank you all codjumper
