OnPlayerSwitchWeapon?
#1

Hey everyone, is there any callback such as OnPlayerSwitchWeapon, OnPlayerChangeWeapon? I tried to use OnPlayerUpdate to detect when player changes weapon, but it's not stable lets say. It works sometimes, and do not when there is fast weapon switch, soo I need callback which will be called every time weapon changes. It may be plugin (I think it would be more accurate), it may be include either, whatever, I need this callback . I saw OnPlayerStatsAndWeaponsUpdate callback in YSF plugin, but it's called like every second, that's too big interval for me, cause I need to change player weapon to another right after previous one is given. Anyway, it's not big deal what am I doing, as I said - I need that callback .
Reply
#2

@SV

Код:
new lastWeapon[MAX_PLAYERS];public OnPlayerConnect(playerid){    lastWeapon[playerid] = 0;    return 1;}public OnPlayerUpdate(playerid){    new w = GetPlayerWeapon(playerid);    if(w != lastWeapon[playerid])    {        OnPlayerChangeWeapon(playerid, w, lastWeapon[playerid]);    }    lastWeapon[playerid] = w;    return 1;}OnPlayerChangeWeapon(playerid, newgun, oldgun){    return 1;}
Reply
#3

Yeah, tried something like that, but the thing is that I make over 50iterations on every weapon change, and that's big amount for OnPlayerUpdate which is called about 32 times a second as far as am I aware of, despite the fact, that I use several functions, comparissions and other stuff in that time.
Reply
#4

Just check OnPlayerKeyStateChange

There you can check if he presses Q and can instant check if the weapon got changed

So you dont need unnecessary checks
Reply
#5

Quote:
Originally Posted by Kaliber
Посмотреть сообщение
Just check OnPlayerKeyStateChange

There you can check if he presses Q and can instant check if the weapon got changed

So you dont need unnecessary checks
But what if player changed their weapon by mouse? Or if he had other key that made him able to change weapon.
Reply
#6

Quote:
Originally Posted by Kaliber
Посмотреть сообщение
Just check OnPlayerKeyStateChange

There you can check if he presses Q and can instant check if the weapon got changed

So you dont need unnecessary checks
This is a totally stupid idea...
Reply
#7

Quote:
Originally Posted by Kaliber
Посмотреть сообщение
Just check OnPlayerKeyStateChange

There you can check if he presses Q and can instant check if the weapon got changed

So you dont need unnecessary checks
The weapon switch keys are not synced, neither OnPlayerKeyStateChange nor GetPlayerKeys will detect them. So this isn't even possible. Also there are multiple ways to switch a weapon so additional checks would be required anyway.

You need to use a timer or something similar for this.
If OnPlayerUpdate is called too often in your opinion, use a timer with a higher interval.
I'd suggest starting with a 500ms timer. If it skips too many weapon changes, lower it until it works as expected.

OnPlayerUpdate is the fastest detection you can do ("fastest" as of reaction time) since it's called everytime the weapon ID gets synced, so you cannot technically check it more often than that (you can check it, but the value won't change until the next update).
Reply
#8

Alright, I will try to figure something with timer. Thanks for response!

PS: What do you mean by saying "or something similar for this"? Like timestamp or tick count check in OnPlayerUpdate? Or you see other way to do this? I thought maybe I could try to write simple plugin to detect weapon change, but I'm zero on C++ (at least I have not tried that programming language.) :/.
Reply
#9

pawn Код:
public OnPlayerUpdate(playerid)
{
    new iCurWeap = GetPlayerWeapon(playerid); // Return the player's current weapon    
    if(iCurWeap != GetPVarInt(playerid, "iCurrentWeapon")) // If he changed weapons since the last update
    {
        // Lets call a callback named OnPlayerChangeWeapon
        OnPlayerChangeWeapon(playerid, GetPVarInt(playerid, "iCurrentWeapon"), iCurWeap);
        SetPVarInt(playerid, "iCurrentWeapon", iCurWeap);//Update the weapon variable
    }
    return 1; // Send this update to other players.
}
 
stock OnPlayerChangeWeapon(playerid, oldweapon, newweapon)
{
    new     s[128],
        oWeapon[24],
        nWeapon[24];
 
    GetWeaponName(oldweapon, oWeapon, sizeof(oWeapon));
    GetWeaponName(newweapon, nWeapon, sizeof(nWeapon));
 
    format(s, sizeof(s), "You changed weapon from %s to %s!", oWeapon, nWeapon);
 
    SendClientMessage(playerid, 0xFFFFFFFF, s);
}
From SAMP's wiki.
Reply
#10

Quote:
Originally Posted by antixgaming
Посмотреть сообщение
pawn Код:
public OnPlayerUpdate(playerid)
{
    new iCurWeap = GetPlayerWeapon(playerid); // Return the player's current weapon    
    if(iCurWeap != GetPVarInt(playerid, "iCurrentWeapon")) // If he changed weapons since the last update
    {
        // Lets call a callback named OnPlayerChangeWeapon
        OnPlayerChangeWeapon(playerid, GetPVarInt(playerid, "iCurrentWeapon"), iCurWeap);
        SetPVarInt(playerid, "iCurrentWeapon", iCurWeap);//Update the weapon variable
    }
    return 1; // Send this update to other players.
}
 
stock OnPlayerChangeWeapon(playerid, oldweapon, newweapon)
{
    new     s[128],
        oWeapon[24],
        nWeapon[24];
 
    GetWeaponName(oldweapon, oWeapon, sizeof(oWeapon));
    GetWeaponName(newweapon, nWeapon, sizeof(nWeapon));
 
    format(s, sizeof(s), "You changed weapon from %s to %s!", oWeapon, nWeapon);
 
    SendClientMessage(playerid, 0xFFFFFFFF, s);
}
From SAMP's wiki.
Already tried that, read the comments below.
Reply
#11

Well that is SAMP\'s limitation. You cannot have callback that will happen only when player switches weapon. That\'s not yet added on SAMP (as I know). You have to either research for something similar or you have to accept our help.
Reply
#12

Quote:
Originally Posted by antixgaming
View Post
Well that is SAMP\'s limitation. You cannot have callback that will happen only when player switches weapon. That\'s not yet added on SAMP (as I know). You have to either research for something similar or you have to accept our help.
Yeah, I hoped maybe there is another way to do what I\'m going to do, but seems like I should stay with OnPlayerUpdate. Thanks everyone who helped!
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)