Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Ayuto opened this issue Jan 14, 2018 · 0 comments · Fixed by #326
Closed

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

Ayuto opened this issue Jan 14, 2018 · 0 comments · Fixed by #326
Labels

Comments

@Ayuto
Copy link
Member

Ayuto commented Jan 14, 2018

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.

@Ayuto Ayuto added the bug label Jan 14, 2018
Ayuto added a commit that referenced this issue Apr 30, 2020
@Ayuto Ayuto linked a pull request May 2, 2020 that will close this issue
@Ayuto Ayuto closed this as completed in #326 May 3, 2020
Ayuto added a commit that referenced this issue May 3, 2020
* Added the ability to disable hooks

Generic fix for issue #236

* Moved HooksDisabled-check to the top

* Updated documentation for hooks_disabled context manager
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant