SA-MP Forums Archive
How to make online time? - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: How to make online time? (/showthread.php?tid=313917)



How to make online time? - geerdinho8 - 27.01.2012

How can I make something, that detects how long a player is playing?

I just need an example

I use this variable:
pawn Код:
pInfo[playerid][Online]



Re: How to make online time? - [ABK]Antonio - 27.01.2012

I use this

Outside of everything
pawn Код:
new OneSecondTimer;
Under OnGameModeInit
pawn Код:
OneSecondTimer  = SetTimer("OneSecondUpdate", 1000, true);
Under OnGameModeExit
pawn Код:
KillTimer(OneSecondTimer);
Outside of everything
pawn Код:
forward OneSecondUpdate(playerid)
public OneSecondUpdate(playerid)
{
    if(pInfo[playerid][Seconds] == 60)
    {
        pInfo[playerid][Seconds] = 0;
        if(pInfo[playerid][Minutes] == 60)
        {
            pInfo[playerid][Minutes] = 0;
            pInfo[playerid][Hours]++;
        }
        else pInfo[playerid][Minutes]++;
    }
    else pInfo[playerid][Seconds]++;
}
There's probably other methods, this is just the one I use, there could even be better ones


Re: How to make online time? - Jack_Fox - 27.01.2012

Create a timer, you can put it in OnGameModeInit:

SetTimer("PlayerTime", 1000, 0);

Create the function:

forward PlayerTime(); // dont forget the forward since its called in a timer
public PlayerTime()
{
for(new i = 0; i < MAX_PLAYERS; i++) // go through each player
{
if(IsPlayerConnected(i) && (maybe put logged-in variable here))
{
pInfo[i][Online]++; // add one second to the played time
}
}
}

if you want it to add time each minute, change the timer to 60000 milliseconds


Re: How to make online time? - Tanush123 - 27.01.2012

Just make pInfo[playerid][Hour] and pInfo[playerid][Minute]. then you must need a timer so on top add
pawn Код:
new OnlineTimer[MAX_PLAYERS];
under onplayerdisconnect add
pawn Код:
KillTimer(OnlineTimer[playerid]);
so it wont bug players
and under your login command/dialog where the player gets his stats add
pawn Код:
OnlineTimer[playerid] = SetTimerEx("OnlineTimeUpdate", 60000, 1, "i", playerid);
so first go down to your script and add
pawn Код:
forward OnlineTimeUpdate(playerid);
public OnlineTimeUpdate(playerid)
{
    PlayerData[playerid][Minutes] ++;
    if(PlayerData[playerid][Minutes] == 60)
    {
        PlayerData[playerid][Hours] ++;
        PlayerData[playerid][Minutes] = 0;
    }
        return 1;
}



Re: How to make online time? - [ABK]Antonio - 27.01.2012

If I want something to happen every minute they're online, I can simply modify what they gain when a minute happens, or an hour, or even a second. It's a universal thing, if they play for 15 minutes, I can do that in there too. I personally think it's just simpler than getting time over and over.

EDIT:

I would say to use ******'s method if you aren't planning on doing anything with it, like putting in certain things at certain times. I can't think of a way without using a timer to do such a thing. Though, making a separate timer for every single player would probably create more of a mess than just using one global timer. Of course you could call it less like one minute and increase their minutes and when they're at that you could.


Re: How to make online time? - geerdinho8 - 27.01.2012

@[ABK]Antonio
Thanks, it worked.


Re: How to make online time? - MP2 - 27.01.2012

Quote:
Originally Posted by ******
Посмотреть сообщение
Why are you all using timers? Just get the time when they join and the time when they leave.
What if the server crashes?


Re: How to make online time? - MP2 - 28.01.2012

Well yes, but updating every 5 minutes or so isn't. That's what I do

You shouldn't have a one second timer anyway - imagine that with 100 players. Use a minute timer, and save it every 5 or so timer calls.