Skip to content

take_damage() with skip_hooks=True doesn't work as expected #236

@Ayuto

Description

@Ayuto

skip_hooks=True doesn't work as expected, because on_take_damage is a virtual function. Example:

  1. Hook on_take_damage on a human player.
  2. Call take_damage with skip_hooks=True on a bot.

The hook will still get called, because bots have their own implementation (CCSBot) of on_take_damage which later calls the overwritten implementation (CCSPlayer).

One workaround would be to create an OnTakeDamage listener and let Source.Python call the listener from an internal hook. Here's some pseudo-code:

# Internal SP code
disabled = False
    
def internal_hook(args):
    if disabled:
        return

    OnTakeDamage.manager.notify(...)
    
def take_damage(self, ..., skip_hooks=True): # Let it default to True
    global disabled
    disabled = skip_hooks
    
    # Use try/finally just to be sure
    try:
        really_do_damage()
    finally:
        disabled = False
# Plugin code
@OnTakeDamage
def on_take_damage(entity, info):
    # You can call it directly
    take_damage()

    # Or delayed
    delay(10, take_damage)

# Or from somewhere else
take_damage()

The listener would also make hooking on_take_damage much easier for plugin authors.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions