objectById method

TiledObject? objectById(
  1. int id
)

Finds the TiledObject in this map with the unique id. Objects have map wide unique IDs which are never reused. https://doc.mapeditor.org/en/stable/reference/tmx-map-format/#object

This reads through a cached map of all the objects so it does not need to loop through all the object layers each time.

Returns null if not found.

Implementation

TiledObject? objectById(int id) {
  if (_cachedObjects == null) {
    _cachedObjects = {};
    layers.whereType<ObjectGroup>().forEach((objectGroup) {
      for (final object in objectGroup.objects) {
        _cachedObjects![object.id] = object;
      }
    });
  }

  return _cachedObjects?[id];
}