I've just switched from SourceMod to sourcepython recently, since python is my favorite language.
Does sourcepython have something similar to sdkhooks and the OnPlayerRunCmd?
Such as OnWeaponSwitch and if I wanted to force a player to crouch how would I do that?
Something like sdkhooks
Re: Something like sdkhooks
That is all directly built into Source.Python, so you won't need an extension. Some of the virtual/dynamic functions are already in our data:
https://github.com/Source-Python-Dev-Te ... n/entities
If you look at the files in that directory, and the engine/game directories inside that directory, you will find that some have function and/or virtual_function sections. You can use those function names to hook or call the function itself. To hook CBasePlayer::PlayerRunCommand, for instance, you should see this in the data (for CS:S, but exists for other engines/games, as well):
https://github.com/Source-Python-Dev-Te ... er.ini#L40
Then, your plugin might look something like:
We do not have a hook for OnSwitchWeapon, as of yet. Your last question is unrelated and needs to be in its own thread.
https://github.com/Source-Python-Dev-Te ... n/entities
If you look at the files in that directory, and the engine/game directories inside that directory, you will find that some have function and/or virtual_function sections. You can use those function names to hook or call the function itself. To hook CBasePlayer::PlayerRunCommand, for instance, you should see this in the data (for CS:S, but exists for other engines/games, as well):
https://github.com/Source-Python-Dev-Te ... er.ini#L40
Then, your plugin might look something like:
Syntax: Select all
from entities.hooks import EntityCondition, EntityPreHook
@EntityPreHook(EntityCondition.is_player, 'run_command')
def pre_player_run_command(stack_data):
pass
We do not have a hook for OnSwitchWeapon, as of yet. Your last question is unrelated and needs to be in its own thread.
Re: Something like sdkhooks
To force all players to crouch, you could use something like that:
Syntax: Select all
import memory
from entities.hooks import EntityPreHook
from entities.hooks import EntityCondition
from players import UserCmd
from players.constants import PlayerButtons
@EntityPreHook(EntityCondition.is_player, 'run_command')
def pre_run_command(stack_data):
ucmd = memory.make_object(UserCmd, stack_data[1])
# Force the player to crouch
ucmd.buttons |= PlayerButtons.DUCK
Re: Something like sdkhooks
Yo guys, thats super cool thank u!
- L'In20Cible
- Project Leader
- Posts: 1536
- Joined: Sat Jul 14, 2012 9:29 pm
- Location: Québec
Re: Something like sdkhooks
CBaseCombatCharacter::Weapon_Switch has been added to the data:
Syntax: Select all
from entities.hooks import EntityCondition
from entities.hooks import EntityPreHook
from memory import make_object
from players.entity import Player
from weapons.entity import Weapon
@EntityPreHook(EntityCondition.is_player, 'weapon_switch')
def pre_weapon_switch(stack_data):
player = make_object(Player, stack_data[0])
weapon = make_object(Weapon, stack_data[1])
print(player.name, 'switched to his', weapon.classname)
- L'In20Cible
- Project Leader
- Posts: 1536
- Joined: Sat Jul 14, 2012 9:29 pm
- Location: Québec
Re: Something like sdkhooks
I looked more into that function and, in order to use it as a listener, it would ideally look like:
However, from my testings, the player instance is not retrievable from a post-hook. An alternative could be:
However this is not an elegant solution as it may breaks other plugins that would want to block the switch to actually happens. Storing the this pointer into a pre-hook and using it into a post hook is probably the best solution I can think of.
Syntax: Select all
from entities.hooks import EntityCondition
from entities.hooks import EntityPostHook
from memory import make_object
from players.entity import Player
from weapons.entity import Weapon
@EntityPostHook(EntityCondition.is_player, 'weapon_switch')
def post_weapon_switch(stack_data, return_value):
# Was the switch successful?
if not return_value:
return
player = make_object(Player, stack_data[0])
weapon = make_object(Weapon, stack_data[1])
print(player.name, 'switched to his', weapon.classname)
However, from my testings, the player instance is not retrievable from a post-hook. An alternative could be:
Syntax: Select all
from entities.hooks import EntityCondition
from entities.hooks import EntityPreHook
from memory import make_object
from players.entity import Player
from weapons.entity import Weapon
@EntityPreHook(EntityCondition.is_player, 'weapon_switch')
def pre_weapon_switch(stack_data):
# Was the switch successful?
if not pre_weapon_switch.hooked_function.skip_hooks(
*tuple(stack_data)[1:]):
return
player = make_object(Player, stack_data[0])
weapon = make_object(Weapon, stack_data[1])
print(player.name, 'switched to his', weapon.classname)
However this is not an elegant solution as it may breaks other plugins that would want to block the switch to actually happens. Storing the this pointer into a pre-hook and using it into a post hook is probably the best solution I can think of.
Re: Something like sdkhooks
However, from my testings, the player instance is not retrievable from a post-hook.
Related? viewtopic.php?p=7542#p7542

My plugins: Map Cycle • Killstreaker • DeadChat • Infinite Jumping • TripMines • AdPurge • Bot Damage • PLRBots • Entity AntiSpam
Hail, Companion. [...] Hands to yourself, sneak thief.

- L'In20Cible
- Project Leader
- Posts: 1536
- Joined: Sat Jul 14, 2012 9:29 pm
- Location: Québec
Re: Something like sdkhooks
Yup it is.
Return to “Plugin Development Support”
Who is online
Users browsing this forum: No registered users and 85 guests