So far I've been doing something like this to check if an entity has a particular component:
if hasattr(entity,'movement'):
#For entities without physics
entity.movement.vx +=
component.ax entity.movement.vy += component.ay
#Apply acceleration to cymunk_physics if present
elif hasattr(entity,'cymunk_physics'):
body = entity.cymunk_physics.body
body.velocity += (
component.ax,component.ay)
However I found that in python 3 this actually does not work because hasattr is implemented by calling getattr, leading to the following:
File "/Users/sfasd/Code/ElectricNemo/electricnemo/electricnemo.py", line 441, in update
if hasattr(entity,'movement'):
File "kivent_core/entity.pyx", line 57, in kivent_core.entity.Entity.__getattr__ (kivent_core/entity.c:2472)
kivent_core.entity.NoComponentActiveError: Entity 1 has no component active for movement
Is there a more proper way of checking for presence or absence of a component?