SA-MP Forums Archive
detect if player doesn't move for x seconds? - 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: detect if player doesn't move for x seconds? (/showthread.php?tid=620871)



detect if player doesn't move for x seconds? - Bondz - 04.11.2016

so,there's a problem on my server,I see many players is online but in reality they're afk,they are doing this
because of the business earnings.
so,is there any code to detect if player doesn't move for x minutes,then they automatically using /afk command?

I have tried them if the player is paused,but they doesn't paused for unknown reason


Re: detect if player doesn't move for x seconds? - VVWVV - 04.11.2016

Use OnPlayerUpdate with SetTimerEx to check it.


Re: detect if player doesn't move for x seconds? - Hansrutger - 04.11.2016

When talking ONLY about the movement, not when keys are pushed etc, I'd do something similar:
Код:
enum afkInfo {
	Float:aX,
	Float:aY,
	Float:aZ,
	aCounter
};
new AFKPlayer[MAX_PLAYERS][afkInfo];


// Inside the timer:
for (new i = 0; i < MAX_PLAYERS; i++)
{
	new Float:x, Float:y, Float:z;
	GetPlayerPos(i, x, y, z);

	if (x == AFKPlayer[i][aX] && y == AFKPlayer[i][aY] && z == AFKPlayer[i][aZ])
	{
		AFKPlayer[i][aCounter]++;

		if (AFKPlayer[i][aCounter] >= 15) // player has been AFk for 15 seconds or more
		{
			// Do things you want to do if player is AFK
		}
	}
	else
	{
		AFKPlayer[i][aX] = x;
		AFKPlayer[i][aY] = y;
		AFKPlayer[i][aZ] = z;
		AFKPlayer[i][aCounter] = 0;
	}
}
The timer part you will have to figure out yourself, but it's not really hard! Note that OnPlayerUpdate is not fully reliable as it is called multiple times per second sometimes, depending on what is happening:
Quote:
This callback is called, on average, 30 times per second, per player; only use it when you know what it's meant for (or more importantly what it's NOT meant for).
The frequency with which this callback is called for each player varies, depending on what the player is doing. Driving or shooting will trigger a lot more updates than idling.



Re: detect if player doesn't move for x seconds? - Bondz - 04.11.2016

Can I start the timer when the player is logged in?


Re: detect if player doesn't move for x seconds? - Micko123 - 04.11.2016

DO NOT USE OnPlayerUpdate!!! NEVER

Quote:
Originally Posted by ReneG
Посмотреть сообщение
Depends on how many people are on the server.
OnPlayerUpdate gets called 30 times a second, so you do the math.
I would recommend a timer though.
So...


Re: detect if player doesn't move for x seconds? - Threshold - 04.11.2016

It's okay to use OnPlayerUpdate... you need to stop being scared of using it. For this exact purpose however, OnPlayerUpdate would be fine to use but UNNECESSARY. Not to mention SetTimerEx in OnPlayerUpdate would most certainly bug things out and return unexpected results...

If I were you, I would start the timer when they spawn/come back from being AFK. Kill the timer when they die/disconnect/go afk.


Re: detect if player doesn't move for x seconds? - Micko123 - 04.11.2016

I am not being scared of OnPlayerUpdate. I just don't like it because it is unpredictable..
Timers arebest for this because you can use them when you need them and kill them when you don't..


Re: detect if player doesn't move for x seconds? - Dayrion - 04.11.2016

PHP код:
OnGamemodeInit()...
    
SetTimer("CheckAFK"800true);

PUBLIC:
CheckAFK()
{
    foreach(new 
Player)
    {
        if(
gettime() - AFKDetect[i] > && !IsPlayerAFK[i])
        {
            
IsPlayerAFK[i] = true;
            
TogglePlayerControllable(ifalse);
            
SCMTAE(RED"%s is now AFK."GetName(i));
        }
        else if(
gettime() - AFKDetect[i] < && IsPlayerAFK[i])
        {
            
IsPlayerAFK[i] = false;
            
TogglePlayerControllable(itrue);
            
SCMTAE(RED"%s is not anymore AFK."GetName(i));
        }
    }
    return 
1;
}

OnPlayerUpdate()..
AFKDetect[playerid] = gettime(); 
I don't really now if it's optimized. ;p