Play Sound for Attacker

Please post any questions about developing your plugin here. Please use the search function before posting!
canibozz
Member
Posts: 32
Joined: Wed Apr 11, 2018 7:55 am

Play Sound for Attacker

Postby canibozz » Wed Apr 11, 2018 12:19 pm

Hello,

iam trying to play a sound for the attacker.

Sadly i dont know where to start to debug since i don't get any error messages, etc.

Here my code so far:

Syntax: Select all

@EntityPreHook(EntityCondition.is_human_player, 'on_take_damage')
def _pre_take_damage(stack_data):
take_damage_info = make_object(TakeDamageInfo, stack_data[1])
attacker = Entity(take_damage_info.attacker)
victim = make_object(Entity, stack_data[0])

if attacker.classname != 'player':
take_damage_info.damage = 0
return

if victim.team_index == attacker.team_index:
return

attackerPlayer = Player(take_damage_info.attacker)
hitSound(attackerPlayer, take_damage_info.attacker)

def hitSound(attackingPlayer, index):
soundArr = [];
for file in os.listdir("./csgo/sound/instagib/"):
soundArr.append(os.path.join("instagib/", file))
randomSound = random.choice(soundArr)
attackingPlayer.play_sound(randomSound, volume=1.0, attenuation=5000)
print("played!")


There are currently two methods so far.
Player.play_sound
and
Sound.play(index)
am i right?

I would like to get both to work but it seems like it doesn't even work with the "radius" one.

Cheers,
CANi
User avatar
Ayuto
Project Leader
Posts: 2193
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Re: Play Sound for Attacker

Postby Ayuto » Wed Apr 11, 2018 4:46 pm

I can think of three possible problems:
  • The client doesn't have the sound file.
  • In CS:GO you sometimes need to stream the sound. So, adding stream=True to your play_sound() call might fix the issue.
  • It's an unsupported sound file.

Beside that I have added a few hints to your code:

Syntax: Select all

@EntityPreHook(EntityCondition.is_human_player, 'on_take_damage')
def _pre_take_damage(stack_data):
take_damage_info = make_object(TakeDamageInfo, stack_data[1])
attacker = Entity(take_damage_info.attacker)
victim = make_object(Entity, stack_data[0])

# Better use attacker.is_player()
if attacker.classname != 'player':
take_damage_info.damage = 0
return

if victim.team_index == attacker.team_index:
return

# You already created a Player instance in the 4th line. Better reuse it!
attackerPlayer = Player(take_damage_info.attacker)
hitSound(attackerPlayer, take_damage_info.attacker)

def hitSound(attackingPlayer, index):
soundArr = [];
for file in os.listdir("./csgo/sound/instagib/"):
soundArr.append(os.path.join("instagib/", file))
randomSound = random.choice(soundArr)

# volume is already 1.0 by default.
# attenuation doesn't need to be set if you only want to play it to a single
# player
attackingPlayer.play_sound(randomSound, volume=1.0, attenuation=5000)
print("played!")
Player.play_sound() is just a wrapper for Sound/StreamSound.

Return to “Plugin Development Support”

Who is online

Users browsing this forum: No registered users and 26 guests