there is any samp function to detect if player cha...
# support-forum
c
i have a function to check if player change weapons, its working and i need it but it interate to all players online each 1 sec there is any better way to do this?
Copy code
c++
SetTimer("CheckIfPlayersChangedWeapon", 1500, true);
Copy code
c++
public CheckIfPlayersChangedWeapon() {
    foreach(new i : Player) {
        if(paused[i] == false && Logged[i] > 0)
        {
            new Now_WeaponInHands = GetPlayerWeapon(i);
            if(LastWeaponInHands[i] != Now_WeaponInHands) {
                OnPlayerChangeWeapon(i, LastWeaponInHands[i], Now_WeaponInHands);
                LastWeaponInHands[i] = Now_WeaponInHands;
            }
        }
    }
}
there's an example of exactly what you need
g
You could also create an iterator or a group for online players (
Logged[index]
is the array I'm talking about rather than the standard
Player
iter)
c
This callback is called, on average, 30 times per second, per player 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. https://team.sa-mp.com/wiki/OnPlayerUpdate.html are u sure? i my function interate in every player each 1.5 secs onplayerupdate looks atlest 45 times high then my function (30×1.5) i'm wrong?
i dont get what u mean Logged is a variable for players looged or not logged
new Logged[MAX_PLAYERS];
c
OPU is called for every sync packet sent by the player, so it depends on how accurate you want it to be
with your current code, if someone changes the weapon to another and switches back in the 1.5 seconds between timer calls, the callback won't be called
g
Yes. Make it an iterator, or a group. No need for pointless checks then
c
i know, i was asking if samp has a callback to deal with weapon change
c
no (regarding the native implementation of
OnPlayerChangeWeapon
)
c
uses OPU will do the same thing more times
sorry, i don't get what are u trying to suggest
c
here's an example: connected players:
Copy code
0
1 logged
2 logged
3
6
current version:
Copy code
c
foreach (new i : Player)
{
    // executed for 0, 1, 2, 3, 6
    if (Logged[i])
    {
        // executed for 1, 2
    }
}
improved:
Copy code
c
new Iterator:Logged<MAX_PLAYERS>;

public OnPlayerLogin(playerid) // wherever you change the value of `Logged[playerid]`
{
    Iter_Add(Logged, playerid);
}

public OnPlayerDisconnect(playerid, reason)
{
    Iter_Remove(Logged, playerid);
}
Copy code
c
foreach (new i : Logged)
{
    // executed for 1, 2 - no need for additional variable check
}