diff --git a/addons/source-python/packages/source-python/entities/entity.py b/addons/source-python/packages/source-python/entities/entity.py index 87b5f32f4..01da6d2c9 100644 --- a/addons/source-python/packages/source-python/entities/entity.py +++ b/addons/source-python/packages/source-python/entities/entity.py @@ -155,6 +155,40 @@ def create(cls, classname): """Create a new entity with the given classname.""" return cls(create_entity(classname)) + @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 + """ + # Import this here to fix a circular import + from filters.entities import EntityIter + + for entity in EntityIter(classname): + return entity + + return None + + @classmethod + def find_or_create(cls, classname): + """Try to find an entity with the given classname. + + If no entity has been found, it will be created. + + :param str classname: The classname of the entity. + :return: Return the found or created entity. + :rtype: Entity + """ + entity = cls.find(classname) + if entity is None: + entity = cls.create(classname) + + return entity + def spawn(self): """Spawn the entity.""" spawn_entity(self.index)