CS:GO giving weapons weird problem

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:

CS:GO giving weapons weird problem

Postby BackRaw » Wed Oct 01, 2014 8:30 pm

Hi,

I'm currently trying to find out how to give weapons propery in CS:GO. I use this code:

Syntax: Select all

from players.entity import PlayerEntity

class _Player(PlayerEntity):

# __init__(self, index) ...

def on_spawn(self):

self.cash = 0
self.armor = 0

self.equip(True)

def equip(self, strip=False):
"""
Checks the player if they're already equipped before giving them another flashbang grenade.
"""

# are we dead?
if self.isdead:

# if yes, no need to go further
return

# is stripping forced or do we already have at least one flashbang?
if strip or self.get_prop_int("m_iAmmo.012"):

# if yes, strip the player after a tick
tick_delays.delay(0, self._strip)

# equip the player after two ticks
tick_delays.delay(0.1 if strip else 0, self._equip)

def _equip(self):

# create the weapon_flashbang entity
flashbang = BaseEntity(server_tools.create_entity("weapon_flashbang"))

# set its origin to the player's location
flashbang.set_key_value_vector("origin", self.location)

# spawn it
server_tools.spawn_entity(flashbang.index)

def _strip(self):
"""
Strip the player off their weapons and items.
"""

# loop through all weapons the player currently carries
try:
for weapon in map(BaseEntity, self.weapon_indexes()):

# set its hammerid to its index (CS:GO crash fix)
weapon.set_key_value_int("hammerid", weapon.index)

# remove it
server_tools.remove_entity(weapon.index)

except ValueError:

# if the index_from_inthandle() converison failed, the console would be spammed with the error
# so just pass here to not spam the console
pass

# _Player objects are held in a player_manager dict


And this code to execute it:

Syntax: Select all

from events import Event

from flashfunsp.players import player_manager # dict that holds _Player objects


@Event
def flashbang_detonate(game_event):
"""
Gets called whenever a flashbang grenade detonates.
"""

# get the player's _Player instance
player = player_manager[game_event.get_int("userid")]

# were we successful?
if not player is None:

# give the player another one after a tick
player.equip()


@Event
def player_spawn(game_event):
"""
Gets called whenever a player entity spawns on the server (doesn't necessarily mean 'physically')
"""

# grab the player object
player = player_manager[game_event.get_int("userid")]

# were we successful and are we on a team?
if not player is None and player.team > 1:

# if yes, call the _Player instance's on_spawn() method
player.on_spawn()


Most of the time the players receive their flashbangs, but sometimes they don't. They just walk around without any gun and there's no error in the console. What am I doing wrong?
My Github repositories:

Source.Python: https://github.com/backraw
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Postby satoon101 » Wed Oct 01, 2014 9:48 pm

Have you tried adding debug messages to if the code is supposed to be giving them a flashbang or not? That is where I would start. Add a message when they throw a flash, another when equip is called, and another when you move the flashbang to their location. Include the userid or name in the message so you can keep better track.
Image
User avatar
BackRaw
Senior Member
Posts: 537
Joined: Sun Jul 15, 2012 1:46 am
Location: Germany
Contact:

Postby BackRaw » Wed Oct 01, 2014 9:55 pm

satoon101 wrote:Have you tried adding debug messages to if the code is supposed to be giving them a flashbang or not? That is where I would start. Add a message when they throw a flash, another when equip is called, and another when you move the flashbang to their location. Include the userid or name in the message so you can keep better track.

That's what I'm doing now, thanks for the heads up :)
My Github repositories:

Source.Python: https://github.com/backraw
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Postby satoon101 » Thu Oct 02, 2014 12:55 am

You might also use a message if their m_iAmmo.012 isn't 0 to see if that is causing the issue. Actually, you should change your m_iAmmo.012 line to:

Syntax: Select all

if strip or self.get_flashbang_count():


I haven't tested any of this, but you might also use self.get_flashbang_indexes() to see what its value is when this issue occurs.

Off topic, I thought I would share a bit of advice. When using a try/except and only using pass or continue inside the except, you should use contextlib.suppress:

Syntax: Select all

# loop through all weapons the player currently carries
try:
for weapon in map(BaseEntity, self.weapon_indexes()):

# set its hammerid to its index (CS:GO crash fix)
weapon.set_key_value_int("hammerid", weapon.index)

# remove it
server_tools.remove_entity(weapon.index)

except ValueError:

# if the index_from_inthandle() converison failed, the console would be spammed with the error
# so just pass here to not spam the console
pass


Could just be:

Syntax: Select all

from contextlib import suppress


with suppress(ValueError):

for weapon in map(BaseEntity, self.weapon_indexes()):
weapon.set_key_value_int("hammerid", weapon.index)
server_tools.remove_entity(weapon.index)


Also, we did fix the need for hammerid when removing an entity on CS:GO. The current release does not have that fix, but the next release will.
Image
User avatar
BackRaw
Senior Member
Posts: 537
Joined: Sun Jul 15, 2012 1:46 am
Location: Germany
Contact:

Postby BackRaw » Fri Oct 03, 2014 4:16 pm

satoon101 wrote:You might also use a message if their m_iAmmo.012 isn't 0 to see if that is causing the issue. Actually, you should change your m_iAmmo.012 line to:

Syntax: Select all

if strip or self.get_flashbang_count():


I haven't tested any of this, but you might also use self.get_flashbang_indexes() to see what its value is when this issue occurs.

Off topic, I thought I would share a bit of advice. When using a try/except and only using pass or continue inside the except, you should use contextlib.suppress:

Syntax: Select all

# loop through all weapons the player currently carries
try:
for weapon in map(BaseEntity, self.weapon_indexes()):

# set its hammerid to its index (CS:GO crash fix)
weapon.set_key_value_int("hammerid", weapon.index)

# remove it
server_tools.remove_entity(weapon.index)

except ValueError:

# if the index_from_inthandle() converison failed, the console would be spammed with the error
# so just pass here to not spam the console
pass


Could just be:

Syntax: Select all

from contextlib import suppress


with suppress(ValueError):

for weapon in map(BaseEntity, self.weapon_indexes()):
weapon.set_key_value_int("hammerid", weapon.index)
server_tools.remove_entity(weapon.index)


Also, we did fix the need for hammerid when removing an entity on CS:GO. The current release does not have that fix, but the next release will.


Thanks a bunch that's awesome.
My Github repositories:

Source.Python: https://github.com/backraw
8guawong
Senior Member
Posts: 148
Joined: Sat Sep 20, 2014 3:06 am

Postby 8guawong » Mon Oct 06, 2014 3:26 am

Not sure if this is the same problem that I'm facing... so i'll post it here
i spawn c4 instead of fb........ so if i don't have c4 it'll spawn one for me but if i drop the c4 and spawn another c4 i can still pick up the c4 that is on the ground :confused: and then sometime you can't drop the c4 anymore :cool:

tested game: csgo
User avatar
BackRaw
Senior Member
Posts: 537
Joined: Sun Jul 15, 2012 1:46 am
Location: Germany
Contact:

Postby BackRaw » Mon Oct 06, 2014 9:25 am

I give up. It seems the 'location' player property is messed up somehow, flashbangs get spawned somewhere on the map but not where the player is lol. I'll wait for the next release or give_named_item() fix for csgo :)
My Github repositories:

Source.Python: https://github.com/backraw
User avatar
L'In20Cible
Project Leader
Posts: 1533
Joined: Sat Jul 14, 2012 9:29 pm
Location: Québec

Postby L'In20Cible » Mon Oct 06, 2014 11:28 am

I'm not able to reproduce this using the following code.

Syntax: Select all

from entities.entity import BaseEntity
from events import Event
from players.entity import PlayerEntity
from players.helpers import index_from_userid
from tools import server_tools

@Event
def player_spawn(game_event):
player = PlayerEntity(index_from_userid(game_event.get_int('userid')))
entity = BaseEntity(server_tools.create_entity('weapon_flashbang'))
entity.set_key_value_vector('origin', player.location)
server_tools.spawn_entity(entity.index)
I'm always getting a new flashbang. I didn't use your code cause it is incomplete (missing globals, imports, etc.).

In any case, I can confirm that m_iAmmo.012 is always returning 0.

Syntax: Select all

print('>>', player.get_prop_int('m_iAmmo.012'),
player.get_flashbang_count())

Syntax: Select all

>> 0 1
>> 0 1
>> 0 1
As for PlayerEntity.weapon_indexes raising a ValueError, I explained why this was happening here and fixed it yesterday.
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Postby satoon101 » Mon Oct 06, 2014 1:23 pm

Image
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Postby satoon101 » Mon Oct 06, 2014 1:28 pm

8guawong wrote:Not sure if this is the same problem that I'm facing... so i'll post it here
i spawn c4 instead of fb........ so if i don't have c4 it'll spawn one for me but if i drop the c4 and spawn another c4 i can still pick up the c4 that is on the ground :confused: and then sometime you can't drop the c4 anymore :cool:

tested game: csgo

I'm not fully understanding your issue. Are you saying that if you spawn another c4 once you have dropped the current one, that you can pick up the one you dropped while you are still holding the new one? If so, there is nothing we can really do about that, as that is a problem with the game itself. I've never had a problem with dropping the c4, myself, so I'm not sure why that would happen. That seems like an issue with the game itself, as well, though.

If that isn't what you mean, could you try to explain it better? And, better yet, provide reproducible code that causes the issue you are having.
Image
8guawong
Senior Member
Posts: 148
Joined: Sat Sep 20, 2014 3:06 am

Postby 8guawong » Mon Oct 06, 2014 1:51 pm

satoon101 wrote:
8guawong wrote:Not sure if this is the same problem that I'm facing... so i'll post it here
i spawn c4 instead of fb........ so if i don't have c4 it'll spawn one for me but if i drop the c4 and spawn another c4 i can still pick up the c4 that is on the ground :confused: and then sometime you can't drop the c4 anymore :cool:

tested game: csgo

I'm not fully understanding your issue. Are you saying that if you spawn another c4 once you have dropped the current one, that you can pick up the one you dropped while you are still holding the new one? If so, there is nothing we can really do about that, as that is a problem with the game itself. I've never had a problem with dropping the c4, myself, so I'm not sure why that would happen. That seems like an issue with the game itself, as well, though.

If that isn't what you mean, could you try to explain it better? And, better yet, provide reproducible code that causes the issue you are having.


yes thats what i meant if i have c4 on me and i drop that c4 onto the ground then if i spawn another c4 i can pick up the c4 on the ground

off topic but could you guys help me with this pleasssssss: http://www.sourcepython.com/showthread.php?637-need-working-script-that-acutally-switches-player-for-csgo

Return to “Plugin Development Support”

Who is online

Users browsing this forum: No registered users and 136 guests