Skip to content

player_weapons_update2: issue with Weapon.find and Weapon.find_or_create #138

@KirillMysnik

Description

@KirillMysnik

Issue
Weapon.find simply returns instances of entities.entity.Entity class.
Weapon.find_or_create returns instance of the Weapon class if the weapon was created, and instance of the Entity class if it existed.

Code to reproduce

from events import Event
from weapons.entity import Weapon


@Event('player_jump')
def on_player_jump(game_event):
    weapon = Weapon.find_or_create("weapon_deagle")
    print("type(weapon): {}, index: {}".format(type(weapon), weapon.index))

Jump twice. Output:

type(weapon): <class 'weapons._entity.csgo.Weapon'>, index: 229
type(weapon): <class 'entities.entity.Entity'>, index: 229

Source of the problem
entity.py, line 159

    @staticmethod
    def find(classname):
        """Try to find an entity with the given classname.
        If not entity has been found, None will be returned.
        :param str classname: The classname of the entity.
        :return: Return the found entity.
        :rtype: Entity
        """
        entity = BaseEntity.find(classname)
        if entity is not None and entity.is_networked():
            return Entity(entity.index)

        return None

As you can see, this method, unlike .create method, always returns instances of the Entity class.

Possible solution
Make this method classmethod instead of staticmethod and replace line 171 with

return cls(entity.index)

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