How to make online time?
#1

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]
Reply
#2

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
Reply
#3

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
Reply
#4

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;
}
Reply
#5

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.
Reply
#6

@[ABK]Antonio
Thanks, it worked.
Reply
#7

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?
Reply
#8

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


Forum Jump:


Users browsing this thread: 1 Guest(s)