[COD4] Adding a timer to codjumper mod

Have a question about modding, modelling or skinning? Have a tutorial to post? Post here!

Moderator: Core Staff

User avatar
Rezil
Core Staff
Core Staff
Posts: 2030
Joined: July 24th, 2006, 11:21 am
Location: Cramped in a small cubicle/making another jump map

Re: [COD4] Adding a timer to codjumper mod

Post by Rezil » April 26th, 2012, 2:28 pm

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.
:roll:

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]
Drofder2004: Drofder's rules for reviewing a map
[...]
#5 If your name is Rezil, minimum 5/5.
---
<LT>YosemiteSam[NL]:
I heard somewhere that the best way to start is juggling 2 balls with one hand, so you will get a feel for it.

User avatar
MORGOTH
CJ Newbie
CJ Newbie
Posts: 82
Joined: July 30th, 2009, 4:41 pm

Re: [COD4] Adding a timer to codjumper mod

Post by MORGOTH » April 26th, 2012, 5:41 pm

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...

User avatar
Drofder2004
Core Staff
Core Staff
Posts: 13313
Joined: April 13th, 2005, 8:22 pm
Location: UK, London

Re: [COD4] Adding a timer to codjumper mod

Post by Drofder2004 » April 26th, 2012, 8:54 pm

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

Code: Select all

if(!self.isTiming)
To a new line above it.
Image
Virgin Media 20Mb Broadband:
"Perfect for families going online at the same time, downloading movies, online gaming and more."
Borked internet since: 22-07-2010

User avatar
Drofder2004
Core Staff
Core Staff
Posts: 13313
Joined: April 13th, 2005, 8:22 pm
Location: UK, London

Re: [COD4] Adding a timer to codjumper mod

Post by Drofder2004 » April 26th, 2012, 8:58 pm

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 :D
Image
Virgin Media 20Mb Broadband:
"Perfect for families going online at the same time, downloading movies, online gaming and more."
Borked internet since: 22-07-2010

User avatar
Rezil
Core Staff
Core Staff
Posts: 2030
Joined: July 24th, 2006, 11:21 am
Location: Cramped in a small cubicle/making another jump map

Re: [COD4] Adding a timer to codjumper mod

Post by Rezil » April 26th, 2012, 9:29 pm

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

Code: Select all

if(!self.isTiming)
To a new line above it.
**:

Code: Select all

if(self.isTiming)
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 :D
As long as it makes sense to me I'm fine with it. :D

--

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. :)
Drofder2004: Drofder's rules for reviewing a map
[...]
#5 If your name is Rezil, minimum 5/5.
---
<LT>YosemiteSam[NL]:
I heard somewhere that the best way to start is juggling 2 balls with one hand, so you will get a feel for it.

User avatar
MORGOTH
CJ Newbie
CJ Newbie
Posts: 82
Joined: July 30th, 2009, 4:41 pm

Re: [COD4] Adding a timer to codjumper mod

Post by MORGOTH » April 26th, 2012, 11:47 pm

Many thanks, worked like a charm. Thank you all codjumper :)

Post Reply

Who is online

Users browsing this forum: No registered users and 40 guests