Timer...
#1

How can i make a timer that will reduce a players wanted level every minute ?

Something like
pawn Код:
public OnPlayerUpdate(playerid);
{
if(GetPlayerWantedLevel(playerid, >=1)
{
 SetTimerEx("ReduceWantedLevel", 60000, false, "i", playerid);
}
return 1;
}
forward ReduceWantedLevel(playerid)
Public ReduceWantedLevel(playerid);
{
if(GetPlayerWantedLevel(playerid, >=1)
{
SetPlayerWantedLevel(playerid,-1);
}
return 1;
}
Would this work ? If not , provide me a working code (If you want).
Thanks .
Reply
#2

Well no its the wrong way of doing it
You gotta set timer at the time the wanted level is increased
pawn Код:
timid[MAX_PLAYERS];//on top
/*===================================*/

//this when player wanted level is increased
KillTimer(timid[playerid]);
timid[playerid]= SetTimerEx("ReduceWantedLevel", 60000, false, "i", playerid);

/*=====================================*/

//the public function to reduce it
forward ReduceWantedLevel(playerid);
public ReduceWantedLevel(playerid)
{
SetPlayerWantedLevel(playerid, GetPlayerWantedLevel(playerid)-1);
return 1;
}
Reply
#3

The following compiles, I've added comments to explain why I do things.

pawn Код:
new reducetimer[MAX_PLAYERS]; // timer variable

public OnPlayerUpdate(playerid)
{
    if(GetPlayerWantedLevel(playerid) >= 1)
    {
        reducetimer[playerid] = SetTimerEx("ReduceWantedLevel", 60000, false, "i", playerid);
        // we store it in a variable so we can kill the timer later
    }
   
    return 1;
}

forward ReduceWantedLevel(playerid);
public ReduceWantedLevel(playerid)
{
    if(GetPlayerWantedLevel(playerid) >= 1)
    {
        SetPlayerWantedLevel(playerid, GetPlayerWantedLevel(playerid) -1);
    }
   
    // if player has no wanted level left, kill the timer
    else if(GetPlayerWantedLevel(playerid) == 0) return KillTimer(reducetimer[playerid]);
    // theres no reason for this timer to keep running if they have no wanted level
   
    return 1;
}
If you have any queries, please do ask. Also, I'm not entirely sure but I think it's very dodgy to use timers under OnPlayerUpdate.
Reply
#4

First, don't start a timer under OnPlayerUpdate, because player updates the server while moving in miliseconds, so this function is called a lot of times in one second. Set it under OnPlayerConnect. Now, when you create a timer with only one parameter (in this case playerid) a public function which is called from the timer also must be with the same parameter. If you set timer with more params ("ii", playerid, wantedlevel), then your public function has to be with playerid and wanted level. By the way, I don't see any reason why you need wantedlevel in you func. as a param, so delete it, and move the time under OnPlayerConnect.
Reply
#5

@Mionee never use timer under OnPlayerUpdate it would make numerous of timer at once that would make server super laggy and the code is also bugged see the code which i gave^
Reply
#6

Quote:
Originally Posted by BroZeus
Посмотреть сообщение
@Mionee never use timer under OnPlayerUpdate it would make numerous of timer at once that would make server super laggy and the code is also bugged see the code which i gave^
I did mention that it was dodgy to use a timer under OnPlayerUpdate, it'd have more sense for you to show alternatives instead of just parsing codes though!

Also, the code isn't bugged, other than the OnPlayerUpdate. Your code will cause a player's wanted level to keep decreasing because you don't kill the timer after their wanted level is 0.

@op; You should loop a timer when a player spawns to check if they actually have a wanted level as an alternative to OnPlayerUpdate.
Reply
#7

Quote:
Originally Posted by Mionee
Посмотреть сообщение
The following compiles, I've added comments to explain why I do things.

pawn Код:
new reducetimer[MAX_PLAYERS]; // timer variable

public OnPlayerUpdate(playerid)
{
    if(GetPlayerWantedLevel(playerid) >= 1)
    {
        reducetimer[playerid] = SetTimerEx("ReduceWantedLevel", 60000, false, "i", playerid);
        // we store it in a variable so we can kill the timer later
    }
   
    return 1;
}

forward ReduceWantedLevel(playerid);
public ReduceWantedLevel(playerid)
{
    if(GetPlayerWantedLevel(playerid) >= 1)
    {
        SetPlayerWantedLevel(playerid, GetPlayerWantedLevel(playerid) -1);
    }
   
    // if player has no wanted level left, kill the timer
    else if(GetPlayerWantedLevel(playerid) == 0) return KillTimer(reducetimer[playerid]);
    // theres no reason for this timer to keep running if they have no wanted level
   
    return 1;
}
If you have any queries, please do ask. Also, I'm not entirely sure but I think it's very dodgy to use timers under OnPlayerUpdate.
Thanks for the code & for explaining .
I kinda messed it up so thats why i asked .
Thanks once again.
EDIT: Since you're discussing about wrong/right places of putting this timer , where should i put it ?
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)