Page 1 of 2

[COD4] Adding a timer to codjumper mod

Posted: March 24th, 2012, 12:52 pm
by MORGOTH
Hi all,

I want to add a timer to the codjumper mod, i already pointed it out here but i think it's easier if i add it locally to my mod...

I made a little search on the board and i found some results but i don't understand very well script, how do they work and where to place them.

Can someone help me please?

Re: [COD4] Adding a timer to codjumper mod

Posted: March 26th, 2012, 5:51 pm
by MORGOTH
up?

Re: [COD4] Adding a timer to codjumper mod

Posted: March 26th, 2012, 8:25 pm
by Nekoneko
If you want to add scripts to the cj mod, without editing existing scripts, I'd overwrite the file _clientids.gsc, since I think its not being used by cj mod

you could add the file to any .iwd file in the mod under maps\mp\gametypes
so:
maps\mp\gametypes\_clientids.gsc

i just modified rezils script a bit, so credit goes to him:

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.stopwatchrunning = false;
                
                player thread onPlayerSpawned();
        }
}
 
onPlayerSpawned()
{
        self endon("disconnect");
        
        while( true )
        {
                self waittill( "menuresponse", menu, response );
                if( menu == "cj" )
                {
                        if( response == "stopwatchstart" )
                                self thread start();
                        if( response == "stopwatchstop" )
                                self thread stop();
                }
                wait 0.5;
        }
}
 
 
 
start()
{
        if(!self.stopwatchrunning)
        {
                self thread make_secondswatch();
                self thread make_minuteswatch();
                self thread dots_inbetween();
                self.stopwatchrunning = true;
        }
        else
                self iprintlnbold("The stopwatch is already running!");
}
 
make_secondswatch()
{
        self endon( "stopwatch_destroy" );
        self.secondwatch = newclienthudelem(self);
        self.secondwatch.x = 322; //max 650
        self.secondwatch.y = 450; //max 475
        self.secondwatch.alignX = "left";
        self.secondwatch.alignY = "bottom";
        self.secondwatch.font = "default";
        self.secondwatch.fontScale = 2;
        self.secondwatch.archived = false;
        self.secondwatch.color = (1, 1, 1);
        for(;;)
        {
                secondtime = 0;
                secondtime = secondtime * 10;
                seconds = 0;
                for(i=seconds;i<=59;i++)
                {
                        self.secondtime = i ;
                        self.secondwatch setValue(self.secondtime);
                        wait 1;
                }
                wait 0.05;
        }
}
 
make_minuteswatch()
{
        self endon( "stopwatch_destroy" );
        self.minutewatch = newclienthudelem(self);
        self.minutewatch.x = 311; //max 650
        self.minutewatch.y = 450; //max 475
        self.minutewatch.alignX = "right";
        self.minutewatch.alignY = "bottom";
        self.minutewatch.font = "default";
        self.minutewatch.fontScale = 2;
        self.minutewatch.archived = false;
        self.minutewatch.color = (1, 1, 1);
        self.minutetime = 0;
        minutes = 0; //And no I don't know why I used this instead of just putting i=0;
        for(i=minutes;i>=0;i++)
        {
                self.minutewatch setValue(self.minutetime);
                if(self.secondtime == 599/10) self.minutetime++;
                        wait 0.1;
        }
}
 
dots_inbetween()
{
        self endon( "stopwatch_destroy" );
        self.dots = newclienthudelem(self);
        self.dots.x = 318; //max 650
        self.dots.y = 450; //max 475
        self.dots.alignX = "right";
        self.dots.alignY = "bottom";
        self.dots.font = "default";
        self.dots.fontScale = 2;
        self.dots.archived = false;
        self.dots.color = (1, 1, 1);
        for(;;)
        {
                self.dots.label = &":";
                wait 0.7;
                self.dots.label = &" ";
                wait 0.7;
        }
}
 
 
stop()
{
        if(self.stopwatchrunning)
        {
                self notify( "stopwatch_destroy" );
                self iprintlnbold( self.minutetime + " minutes " + self.secondtime + " seconds" );
                self.secondtime = 0;
                self.minutetime = 0;
                self.minutewatch destroy();
                self.secondwatch destroy();
                self.dots destroy();
                self.stopwatchrunning = false;
        }
        else
                self iprintlnbold("You didn't start the stopwatch!");
}
by typing "/openscriptmenu cj stopwatchstart" the counter will start
and "/ openscriptmenu cj stopwatchstop" will stop it.

Re: [COD4] Adding a timer to codjumper mod

Posted: March 26th, 2012, 11:11 pm
by Drofder2004
Nekoneko wrote:If you want to add scripts to the cj mod, without editing existing scripts, I'd overwrite the file _clientids.gsc, since I think its not being used by cj mod
Or you could use the "addons.gsc" file that already exists for this very purpose.

Re: [COD4] Adding a timer to codjumper mod

Posted: March 27th, 2012, 12:04 am
by Rezil
i just modified rezils script a bit, so credit goes to him:
This is why I like this community. :)

Feel free to use this script if you find it useful. Truth be told, Drofder came up with the basic idea, I just expanded on it. And do use addons.gsc, it was created for that specific purpose.

Re: [COD4] Adding a timer to codjumper mod

Posted: March 27th, 2012, 12:48 am
by MORGOTH
many thanks, worked like a charm :)

P.S. i used addon.gsc as suggested ;)

Re: [COD4] Adding a timer to codjumper mod

Posted: March 27th, 2012, 8:26 pm
by Nekoneko
Ah, i didn't know about that ^^
Good idea for addons

Re: [COD4] Adding a timer to codjumper mod

Posted: April 25th, 2012, 10:52 pm
by MORGOTH
After a month i realize that there is something wrong in this timer :S when seconds reach 59 it doesn't turn to 1 minute and it resets seconds to 0...

can someone fix it please?

Re: [COD4] Adding a timer to codjumper mod

Posted: April 25th, 2012, 11:16 pm
by Drofder2004
The seconds timer counts 0,1,2,3,etc (each second)
But the minutes timer counts 0,0.1,0.2,0.3 (each tenth of a second)

The "minutetime" is incremented when the secondtime = 59.9 but the second time will never equal 59.9

This whole code is bad practice, it would be a lot easier to simply count in seconds and then ever "wait 1" do a quick calculation to work out minutes and seconds.

Code: Select all

seconds = totalSeconds % 60;
minutes = (totalSeconds - seconds) / 60;
hours = minutes / 60;
too tired to fix your code, but I am sure someone can be arsed with the above explanation.

Re: [COD4] Adding a timer to codjumper mod

Posted: April 25th, 2012, 11:31 pm
by Rezil

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
*/
 
playerInit()
{
        self.counter_sec = 0;
        self.counter_min = 0;
        self.counter_hrs = 0;
        self.isTiming = false;
        
        if(!isdefined(self.stopwatch))
                self.stopwatch = [];
 
        if(!isdefined(self.misc))
                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;
        }
        self.stopwatch[0] setValue(self.counter_sec);
        self.stopwatch[1] setValue(self.counter_min);
        self.stopwatch[2] setValue(self.counter_hrs);
        
}
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.counter_hrs = h;
                        for(i=0;i<60;i++)
                        {
                                self.counter_min = i;
                                for(j=0;j<600;j++)
                                {
                                        self.counter_sec = 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;
                }
                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))
        {
                self.stopwatch[0] setValue(self.counter_sec);
                self.stopwatch[1] setValue(self.counter_min);
                self.stopwatch[2] setValue(self.counter_hrs);
        }
}
Taken straight from some mapping project I did a while back, some variables might be missing. This code has been tested and does work, but try it yourself and report any errors you might get. It's probably nowhere near optimal but it gets the job done better than the code you used previously.

Re: [COD4] Adding a timer to codjumper mod

Posted: April 26th, 2012, 12:09 am
by Drofder2004
Rez, that is some awful loop calculating going on there :P

Also, are the "if(!self.timing) break;" stuff needed, when you are while-looping on the assumption it is alway 1?

Gotta love the flashing colon stuff though, eye for detail :D

Re: [COD4] Adding a timer to codjumper mod

Posted: April 26th, 2012, 7:22 am
by Rezil
The break statements are needed because you're in a nested loop and have no clue when the player will stop timing. The while loop condition is checked only after an iteration completes(once per day in our case :) )

And yeah, I could've written this much more efficiently but in my defence I was planning on optimizing this code next week. :p

Re: [COD4] Adding a timer to codjumper mod

Posted: April 26th, 2012, 9:59 am
by MORGOTH
Can someone please explain me how to use the code posted by rezil? I tried to copy paste it into addon.gsc but if i launch the server i have a script compile error :(

Re: [COD4] Adding a timer to codjumper mod

Posted: April 26th, 2012, 11:46 am
by Rezil
Rezil wrote:

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
*/
I am not going to code the entire thing for you, I have only provided you with the framework. If you need any help debugging you can post here, just don't expect ready-made solutions that you can simply copy-paste into a .gsc file and it will magically work.

Re: [COD4] Adding a timer to codjumper mod

Posted: April 26th, 2012, 12:37 pm
by MORGOTH
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.