Skip to content

Commit

Permalink
Added Entity.find and Entity.find_or_create
Browse files Browse the repository at this point in the history
  • Loading branch information
Ayuto committed Nov 13, 2015
1 parent 71b2aa0 commit 6133c21
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions addons/source-python/packages/source-python/entities/entity.py
Expand Up @@ -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)
Expand Down

0 comments on commit 6133c21

Please sign in to comment.