Skip to content
Daan van Yperen edited this page Jun 1, 2019 · 7 revisions

Some conventions and best practices while using odb.

Entity best practices

  • Use int entities and API calls, instead of the legacy Entity API calls.
  • Beware the intricacies of the entity lifecycle.
    • Subscriptions are frozen during system processing and do not reflect deleted or new entities.
  • Prefer systems over entities for singletons.
  • Consider using Fluid-Entities, but only if you hate typing.

Components best practices

  • Avoid logic in components, use entity systems instead.
  • Strive for reusability in your components.
    • Limit the amount of fields to the smallest amount possible. A component with position, rotation, sprite hurts re usability.
    • Avoid repeating the same information across multiple components.
  • Strive for primitives, strings and simple data as component fields. This is easier to persist and network.
  • Use entity references judiciously. They go stale when you delete them elsewhere and cause bugs.
    • Consider letting odb manage entity references for you.
    • Consider referring to entities via identifiers like tag or group.
    • Or manage the references via a system or manager by listening in on the EntitySubscription.
  • Prefer component mappers over Entity-Transmuter.
  • Use prefabs or Entity-Transmuter for high volume creation of entities, like particles.
  • Consider using IDE Code Templates for adding component mappers quickly.
  • You'll be typing component names a lot, avoid the Component class name suffix.
  • Mark components with unserializable information with @Transient.
  • Do not alter composition of deleted entities.

System best practices

  • Operate on entity subscriptions using IteratingSystem.
  • Implement supporting services using BaseSystem.
  • Favor small systems over less systems.
  • Avoid a deep system hierarchy, keep it flat.
  • Avoid initializing in system constructors.

Decoupling systems

Always look for ways to decouple systems.

  • Put code with multiple call sites in dedicated utility Pojo's or systems.
  • Avoid coupling systems whenever possible.
  • Avoid coupling systems that operate on entities.
  • Consider decoupling systems using singleton components or events. (pass data, don't chain behavior).

Aspects

  • Beware the performance of creating aspects at runtime.
  • Avoid creating aspects in tight loops.
  • Use the aspect annotations over Aspect.all builder whenever possible.

Iterating entities best practices

Fastest (and verbose) way to iterate entity identifiers:

IntBag entities = subscription.getEntities();
int[] ids = entities.getData();
for (int i = 0, s = entities.size(); s > i; i++) {
     // do something with ids[i]
}
Clone this wiki locally