OnWeaponReloaded listener?

Please post any questions about developing your plugin here. Please use the search function before posting!
User avatar
BackRaw
Senior Member
Posts: 537
Joined: Sun Jul 15, 2012 1:46 am
Location: Germany
Contact:

OnWeaponReloaded listener?

Postby BackRaw » Mon May 23, 2016 7:01 am

Hi,

is it possible to (probably measure by weapon classname) how long the reload animation would take?

Or probably just by using a listener or something:

Code: Select all

from listeners import OnWeaponReloaded

@OnWeaponReloaded
def on_weapon_reloaded(player, weapon):
    print(player.name, "reloaded", weapon.classname)


Thanks!
User avatar
Ayuto
Project Leader
Posts: 2195
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Re: OnWeaponReloaded listener?

Postby Ayuto » Mon May 23, 2016 6:00 pm

What do you want to do when the weapon has been reloaded? Maybe there are better approaches for what you want to do.
User avatar
BackRaw
Senior Member
Posts: 537
Joined: Sun Jul 15, 2012 1:46 am
Location: Germany
Contact:

Re: OnWeaponReloaded listener?

Postby BackRaw » Mon May 23, 2016 6:04 pm

I want to refill the weapon's ammo :D
User avatar
Ayuto
Project Leader
Posts: 2195
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Re: OnWeaponReloaded listener?

Postby Ayuto » Mon May 23, 2016 6:37 pm

A hook might be a better choice. But well... this is working without additional game data. :D

Syntax: Select all

from events import Event
from players.entity import Player
from players.helpers import index_from_userid
from entities.helpers import index_from_inthandle
from entities.entity import Entity
from engines.server import global_vars
from listeners.tick import Delay

@Event('weapon_reload')
def weapon_reload(event):
userid = event['userid']
index = index_from_userid(userid)
player = Player(index)

try:
weapon_index = index_from_inthandle(player.active_weapon)
except (ValueError, OverflowError):
return

weapon = Entity(weapon_index)
next_attack = weapon.get_property_float('m_flNextPrimaryAttack')
sequence_duration = next_attack - global_vars.current_time

# TODO:
# You need to cancel this in some rare situations.
# Might also be a good idea to add some more time to sequence_duration
# just to make sure.
Delay(sequence_duration, on_weapon_reloaded, player, weapon)

def on_weapon_reloaded(player, weapon):
print('finished')
necavi
Developer
Posts: 129
Joined: Wed Jan 30, 2013 9:51 pm

Re: OnWeaponReloaded listener?

Postby necavi » Mon May 23, 2016 7:13 pm

Note: That event won't be called if the weapon is completely out of ammo (if I recall right).
User avatar
BackRaw
Senior Member
Posts: 537
Joined: Sun Jul 15, 2012 1:46 am
Location: Germany
Contact:

Re: OnWeaponReloaded listener?

Postby BackRaw » Tue May 24, 2016 6:33 am

Ayuto wrote:A hook might be a better choice. But well... this is working without additional game data. :D

Syntax: Select all

from events import Event
from players.entity import Player
from players.helpers import index_from_userid
from entities.helpers import index_from_inthandle
from entities.entity import Entity
from engines.server import global_vars
from listeners.tick import Delay

@Event('weapon_reload')
def weapon_reload(event):
userid = event['userid']
index = index_from_userid(userid)
player = Player(index)

try:
weapon_index = index_from_inthandle(player.active_weapon)
except (ValueError, OverflowError):
return

weapon = Entity(weapon_index)
next_attack = weapon.get_property_float('m_flNextPrimaryAttack')
sequence_duration = next_attack - global_vars.current_time

# TODO:
# You need to cancel this in some rare situations.
# Might also be a good idea to add some more time to sequence_duration
# just to make sure.
Delay(sequence_duration, on_weapon_reloaded, player, weapon)

def on_weapon_reloaded(player, weapon):
print('finished')

That is exactly what I wanted, thanks a bunch. "Some rare situations" are map changes etc, right? I had problems with that before but I have managed to fix them so that won't be a problem :D

necavi wrote:Note: That event won't be called if the weapon is completely out of ammo (if I recall right).

That will never happen in my plugin :D But thanks for the heads up, very helpful!
User avatar
Ayuto
Project Leader
Posts: 2195
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Re: OnWeaponReloaded listener?

Postby Ayuto » Tue May 24, 2016 7:44 am

BackRaw wrote:"Some rare situations" are map changes etc, right?

I meant situations like player left the server or switched the weapon while reloading.
User avatar
BackRaw
Senior Member
Posts: 537
Joined: Sun Jul 15, 2012 1:46 am
Location: Germany
Contact:

Re: OnWeaponReloaded listener?

Postby BackRaw » Tue May 24, 2016 7:58 am

Ayuto wrote:
BackRaw wrote:"Some rare situations" are map changes etc, right?

I meant situations like player left the server or switched the weapon while reloading.

Oh, cool. Thank makes sense.
User avatar
BackRaw
Senior Member
Posts: 537
Joined: Sun Jul 15, 2012 1:46 am
Location: Germany
Contact:

Re: OnWeaponReloaded listener?

Postby BackRaw » Thu May 26, 2016 9:10 pm

Just a hint: To get the closest to 'directly after the reloading animation has finished', I use this on Arch Linux (maybe different on Debian/Ubuntu and Windows - needs more testing):

Syntax: Select all

from events import Event
from players.entity import Player
from players.helpers import index_from_userid
from entities.helpers import index_from_inthandle
from entities.entity import Entity
from engines.server import global_vars
from listeners.tick import Delay

@Event('weapon_reload')
def weapon_reload(event):
userid = event['userid']
index = index_from_userid(userid)
player = Player(index)

try:
weapon_index = index_from_inthandle(player.active_weapon)
except (ValueError, OverflowError):
return

weapon = Entity(weapon_index)
next_attack = weapon.get_property_float('m_flNextPrimaryAttack') + .2 # <-- That's the fix :D
sequence_duration = next_attack - global_vars.current_time

# TODO:
# You need to cancel this in some rare situations.
# Might also be a good idea to add some more time to sequence_duration
# just to make sure.
Delay(sequence_duration, on_weapon_reloaded, player, weapon)

def on_weapon_reloaded(player, weapon):
print('finished')
User avatar
Ayuto
Project Leader
Posts: 2195
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Re: OnWeaponReloaded listener?

Postby Ayuto » Thu May 26, 2016 9:34 pm

Yeah, that's what I meant with my comment:

Syntax: Select all

# Might also be a good idea to add some more time to sequence_duration
# just to make sure.
I believe the problem is that the Delay class compares its delay to real time (time.time()), but the sequence duration is dependend on the game time. So, when there is a little lag in execution, a small gap can arise between the next attack value and the delay. We can easily find that out by simulating a lag. Just add time.sleep(1) after the creation of the Delay instance. If I'm right, the on_weapon_reloaded callback should always get called too early and the animation sequence hasn't finished yet.
User avatar
BackRaw
Senior Member
Posts: 537
Joined: Sun Jul 15, 2012 1:46 am
Location: Germany
Contact:

Re: OnWeaponReloaded listener?

Postby BackRaw » Thu May 26, 2016 10:17 pm

Ayuto wrote:Yeah, that's what I meant with my comment:

Syntax: Select all

# Might also be a good idea to add some more time to sequence_duration
# just to make sure.
I believe the problem is that the Delay class compares its delay to real time (time.time()), but the sequence duration is dependend on the game time. So, when there is a little lag in execution, a small gap can arise between the next attack value and the delay. We can easily find that out by simulating a lag. Just add time.sleep(1) after the creation of the Delay instance. If I'm right, the on_weapon_reloaded callback should always get called too early and the animation sequence hasn't finished yet.

Good point. What about the hook you mentioned earlier? Would it eliminate all of that? :D
User avatar
Ayuto
Project Leader
Posts: 2195
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Re: OnWeaponReloaded listener?

Postby Ayuto » Sat May 28, 2016 11:26 am

Seems like my hook never gets called. :(

What about something like this?

Syntax: Select all

from events import Event
from entities.helpers import index_from_inthandle
from players.entity import Player
from weapons.entity import Weapon
from weapons.manager import weapon_manager

@Event('weapon_reload')
def weapon_reload(event):
player = Player.from_userid(event['userid'])

try:
weapon_index = index_from_inthandle(player.active_weapon)
except (ValueError, OverflowError):
return

weapon = Weapon(weapon_index)
weapon.ammo = weapon_manager[weapon.classname].maxammo
User avatar
BackRaw
Senior Member
Posts: 537
Joined: Sun Jul 15, 2012 1:46 am
Location: Germany
Contact:

Re: OnWeaponReloaded listener?

Postby BackRaw » Sat May 28, 2016 1:20 pm

Ayuto wrote:Seems like my hook never gets called. :(

What about something like this?

Syntax: Select all

from events import Event
from entities.helpers import index_from_inthandle
from players.entity import Player
from weapons.entity import Weapon
from weapons.manager import weapon_manager

@Event('weapon_reload')
def weapon_reload(event):
player = Player.from_userid(event['userid'])

try:
weapon_index = index_from_inthandle(player.active_weapon)
except (ValueError, OverflowError):
return

weapon = Weapon(weapon_index)
weapon.ammo = weapon_manager[weapon.classname].maxammo

Dang! This way, the ammo will not reset after the weapon has been reloaded, for example with the AK47:

Ammo: 90 (full), Clip: 15
Pressing Reload:
- Ammo: 90
- Clip: 15
... (after the animation)
- Ammo: 75
- Clip: 30

Again pressing reload when clip is 15:
- Ammo: 90
- Clip: 15
...(after the animation)
- Ammo: 75
- Clip: 30

Which would suffice, however that is kinda "ugly" lol. Tried that already :D

I'll stick with the delay method, as this works how I want to. Thank you very much!

Edit: Player.from_userid()? Seems like I can reject my oen implementation for this lol :D
User avatar
Ayuto
Project Leader
Posts: 2195
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Re: OnWeaponReloaded listener?

Postby Ayuto » Sat May 28, 2016 1:25 pm

BackRaw wrote:This way, the ammo will not reset after the weapon has been reloaded, for example with the AK47:

Yeah, I know, but it's less error prone than using the delay method.
User avatar
Mahi
Senior Member
Posts: 236
Joined: Wed Aug 29, 2012 8:39 pm
Location: Finland

Re: OnWeaponReloaded listener?

Postby Mahi » Sat May 28, 2016 1:43 pm

BackRaw wrote:Player.from_userid()? Seems like I can reject my oen implementation for this lol :D
They took away one of the key selling points of my EasyPlayer :( https://github.com/Mahi/EasyPlayer/comm ... 27c9f060b1

But seriously, it's awesome they included it. Makes everything just so much easier when there's no need to import and use index_from_userid every time you want to use events.

Also, I would use the "less error prone" way, who cares if it's ugly if it works and a beautiful one doesn't ^^
User avatar
iPlayer
Developer
Posts: 590
Joined: Sat Nov 14, 2015 8:37 am
Location: Moscow
Contact:

Re: OnWeaponReloaded listener?

Postby iPlayer » Sat May 28, 2016 1:51 pm

You can combine both ways... Refill the ammo 2 times. One to actually make sure the weapon is never empty, and the delayed one (less reliable) to make sure the numbers look beautiful. If you really need this "beauty".
Image /id/its_iPlayer
My plugins: Map Cycle • Killstreaker • DeadChat • Infinite Jumping • TripMines • AdPurge • Bot Damage • PLRBots • Entity AntiSpam

Hail, Companion. [...] Hands to yourself, sneak thief. Image
User avatar
BackRaw
Senior Member
Posts: 537
Joined: Sun Jul 15, 2012 1:46 am
Location: Germany
Contact:

Re: OnWeaponReloaded listener?

Postby BackRaw » Sat May 28, 2016 1:56 pm

Well the delayed way works without crashing the server whatsoever. I'll stick with it until I notice something. :)

Mahi wrote:
BackRaw wrote:Player.from_userid()? Seems like I can reject my oen implementation for this lol :D
They took away one of the key selling points of my EasyPlayer :( https://github.com/Mahi/EasyPlayer/comm ... 27c9f060b1

But seriously, it's awesome they included it. Makes everything just so much easier when there's no need to import and use index_from_userid every time you want to use events.

Also, I would use the "less error prone" way, who cares if it's ugly if it works and a beautiful one doesn't ^^
I care! :D

I got to love classmethods lately, so awesome! SP is growing in the right direction, definitely :)
User avatar
D3CEPTION
Senior Member
Posts: 129
Joined: Tue Jan 26, 2016 1:24 pm
Location: Switzerland

Re: OnWeaponReloaded listener?

Postby D3CEPTION » Sun May 29, 2016 12:48 pm

BackRaw wrote:is it possible to (probably measure by weapon classname) how long the reload animation would take?
I want to refill the weapon's ammo

sorry if im curious, but what use could this have on a server?
User avatar
iPlayer
Developer
Posts: 590
Joined: Sat Nov 14, 2015 8:37 am
Location: Moscow
Contact:

Re: OnWeaponReloaded listener?

Postby iPlayer » Sun May 29, 2016 1:01 pm

D3CEPTION wrote:
BackRaw wrote:is it possible to (probably measure by weapon classname) how long the reload animation would take?
I want to refill the weapon's ammo

sorry if im curious, but what use could this have on a server?


BackRaw wants to have infinite ammo, but also to keep the ammo indicators at their maximum value.
Image /id/its_iPlayer
My plugins: Map Cycle • Killstreaker • DeadChat • Infinite Jumping • TripMines • AdPurge • Bot Damage • PLRBots • Entity AntiSpam

Hail, Companion. [...] Hands to yourself, sneak thief. Image
User avatar
D3CEPTION
Senior Member
Posts: 129
Joined: Tue Jan 26, 2016 1:24 pm
Location: Switzerland

Re: OnWeaponReloaded listener?

Postby D3CEPTION » Sun May 29, 2016 1:32 pm

if someone is handy enough with the memory module, maybe find the respective register in memory and block the "ammo-add" push that changes the clients hud value? ammo should be a networked property and route server -> client? i would like to see that approach, but its probably too hacky for sp

Return to “Plugin Development Support”

Who is online

Users browsing this forum: No registered users and 43 guests