Is this bad practice?

76 views
Skip to first unread message

Damion Murray

unread,
Feb 7, 2015, 2:50:45 PM2/7/15
to ash-fr...@googlegroups.com
Sometimes I need to access a component of an entity within a system that does not process said entity. To get around this I override the system's addToEngine method and fetch the entity via the engine's getEntityByName method (this assumes the entity is already created). It's a convenient work-around but I can't help but feel this is bad practice. Are there any pitfalls to doing this?

Eric Lund

unread,
Feb 7, 2015, 5:06:57 PM2/7/15
to ash-fr...@googlegroups.com
Personally, I don't have a problem with handling named entities/singletons this way. You could also retrieve the entity in the constructor, or eschew entities in this circumstance, passing to the constructor arbitrary data.

As for pitfalls, I can think of:

1) If the system is added before the named entity is added, you'll get a null reference. That's easy to diagnose and fix.
2) If the named entity is replaced for some reason, you'll still have a reference to the old entity and old components, leaving the system to operate on stale data. Solution: don't do that, never remove and recreate a named entity.
3) If the named entity is accidentally removed, the system will chug along as if the entity exists. Other systems that utilize this entity in a different manner (solution: be consistent) may fail and confuse you because, after all, the first system is still working.

None of these are particularly scary to me. :)
Reply all
Reply to author
Forward
0 new messages