SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 22/03/2019, 03:35 PM   #1
Marshas
Little Clucker
 
Join Date: Apr 2017
Posts: 37
Reputation: 0
Default OnPlayerSwitchWeapon?

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 .
Marshas is offline   Reply With Quote
Old 22/03/2019, 03:50 PM   #2
Bolex_
Gangsta
 
Bolex_'s Avatar
 
Join Date: Aug 2015
Location: Croatia.
Posts: 592
Reputation: 117
Default Re: OnPlayerSwitchWeapon?

@SV

Code:
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;}
Bolex_ is offline   Reply With Quote
Old 22/03/2019, 04:08 PM   #3
Marshas
Little Clucker
 
Join Date: Apr 2017
Posts: 37
Reputation: 0
Default Re: OnPlayerSwitchWeapon?

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.
Marshas is offline   Reply With Quote
Old 22/03/2019, 04:14 PM   #4
Kaliber
High-roller
 
Kaliber's Avatar
 
Join Date: Jun 2012
Location: Localhost
Posts: 1,065
Reputation: 151
Default Re: OnPlayerSwitchWeapon?

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
__________________
Kaliber is offline   Reply With Quote
Old 22/03/2019, 05:17 PM   #5
antixgaming
Big Clucker
 
antixgaming's Avatar
 
Join Date: Apr 2018
Location: Knowhere
Posts: 103
Reputation: 13
Default Re: OnPlayerSwitchWeapon?

Quote:
Originally Posted by Kaliber View Post
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.
antixgaming is offline   Reply With Quote
Old 22/03/2019, 05:34 PM   #6
m4karow
Huge Clucker
 
Join Date: Oct 2015
Posts: 287
Reputation: 37
Default Re: OnPlayerSwitchWeapon?

Quote:
Originally Posted by Kaliber View Post
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...
m4karow is offline   Reply With Quote
Old 22/03/2019, 06:04 PM   #7
NaS
High-roller
 
NaS's Avatar
 
Join Date: Mar 2008
Location: 🇩🇪
Posts: 1,656
Reputation: 559
Default Re: OnPlayerSwitchWeapon?

Quote:
Originally Posted by Kaliber View Post
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).
NaS is offline   Reply With Quote
Old 22/03/2019, 06:15 PM   #8
Marshas
Little Clucker
 
Join Date: Apr 2017
Posts: 37
Reputation: 0
Default Re: OnPlayerSwitchWeapon?

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.) :/.
Marshas is offline   Reply With Quote
Old 22/03/2019, 06:29 PM   #9
NaS
High-roller
 
NaS's Avatar
 
Join Date: Mar 2008
Location: 🇩🇪
Posts: 1,656
Reputation: 559
Default Re: OnPlayerSwitchWeapon?

Quote:
Originally Posted by Marshas View Post
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.) :/.
You could do that too but calling one native and doing int comparisons is literally what you do to check the weapon. So if you do that, you could just put the code into OnPlayerUpdate in the first place without additional checks (which I still recommend if you want it to react as fast as possible).

You cannot get more accurate than that even with a plugin. The data is just not synced more often.
NaS is offline   Reply With Quote
Old 22/03/2019, 06:43 PM   #10
Marshas
Little Clucker
 
Join Date: Apr 2017
Posts: 37
Reputation: 0
Default Re: OnPlayerSwitchWeapon?

Quote:
Originally Posted by NaS View Post
You could do that too but calling one native and doing int comparisons is literally what you do to check the weapon. So if you do that, you could just put the code into OnPlayerUpdate in the first place without additional checks (which I still recommend if you want it to react as fast as possible).

You cannot get more accurate than that even with a plugin. The data is just not synced more often.
That's the point. I do not need more often, I need in the right place in the right time. That's why I am searching for callback such as OnPlayerSwitchWeapon.
Marshas is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
OnPlayerSwitchWeapon function without OnPlayerUpdate ? Rubey Scripting Help 4 01/06/2016 02:17 PM
OnPlayerSwitchWeapon milanosie Scripting Help 5 13/05/2012 02:46 PM


All times are GMT. The time now is 11:51 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.