Commands on Timers

 

Ok this is another tut on request. I was asked how to create a command that would have a time delayed effect, so heres how to do it. Thanks go to Sorak, for keeping me busy.


First of all, we need to add a timer to the client structure in g_local.h. This will keep the time when the command will activate. I'm using the value -1 for an inactive timer. Find the line qboolean update_chase; add this directly after it, before the }.

float          comtime;

You could use any variable name you want, just remember to change all occurances.


Now we need to set it to -1, so the command doesn't active as soon as you start the level. find putclientinserver in p_client.c. add the line below, somewhere within that function. it shouldn't matter where.

ent->client->comtime=-1;

Adding the check to see if the timer has run out seems like a good idea to me. find ClientBeginServerFrame in p_client.c. add this check in at the end

if (!(ent->client->comtime==-1))
        if (ent->client->comtime<level.time)
               [effect function goes on this line];

Its fairly easy to understand. First it checks to see if the timer is active, and then to see if the time has elapsed. If it has, then do the action. If the action is nonfatal to the user, reset the comtime to -1 in your effect funtion.


All we need to do now is to allow the timer to be set. this goes in g_cmds.c. go to the last function (unless you've changed the order) and before the last else add the code below. change the text in quotes to whatever you want. replace 180 with the time in seconds before you want your effect to happen. the gi.cprintf is there to let you know it's active

else if (Q_stricmp(cmd,"swear")==0)
        {
        ent->client->comtime=level.time+180;
        gi.cprintf(ent,PRINT_HIGH,"Swearing active. 3 minutes to go :)");
        }

If you want to be able to stop the coutdown then have a section like the above that sets ent->client->comtime to -1. as it stands the code above will reset the timer to 3 minutes to go. if you want to stop this. then use the code below

else if (Q_stricmp(cmd,"swear")==0)
        if (ent->client->comtime=-1)
        {
               ent->client->comtime=level.time+180;
               gi.cprintf(ent,PRINT_HIGH,"Swearing active. 3 minutes to go :)");
        }
        else
               gi.cprintf(ent,PRINT_HIGH,"No return sucker ;)"); 

Well thats it. Enjoy :)


Skunkworks Turorials