Entities Hierarchy and Marker Components
When a map is loaded, it spawns many entities: for the map, for layers, for tiles, for objects, for colliders, and more.
To keep things organized, these entities are structured in a parent/child hierarchy, and every entity has an associated marker component to help with queries and system targeting.
Using a hierarchy also enables inheritance of certain components from top-level entities down the tree.
For example, if you change the Visibility of a layer entity, it will automatically apply to all entities below it in the hierarchy, such as tiles or objects.
Hierarchy Overview
flowchart TD
W["<b>TiledWorld</b><br/>(optional)"]
M["<b>TiledMap</b>"]
LT["<b>TiledLayer::Tiles</b>"]
LO["<b>TiledLayer::Objects</b>"]
LI["<b>TiledLayer::Image</b>"]
LG["<b>TiledLayer::Group</b>"]
LS["other layers..."]
T["<b>TiledTilemap</b><br/>one per tileset"]
TI["<b>TiledTile</b>"]
O["<b>TiledObject</b>"]
OV["<b>TiledObjectVisualOf</b><br/>(tile objects)"]
OB["<b>TiledObjectTextBox</b><br/>(text objects)"]
OT["<b>TiledObjectText</b>"]
I["<b>TiledImage</b>"]
CC["<b>TiledColliderChunk</b><br/>(colliders chunks)"]
C["<b>TiledColliderOf</b><br/>(additional colliders)"]
W --> M
M --> LT & LO & LI & LG
LG --> LS
LT --> T & TI
LO --> O
O --> OV
O --> OB --> OT
LI --> I
LT -.-> CC -.-> C
LT -.-> C
O -.-> C
Dashed arrows are only present when the physics feature is enabled and the corresponding layer or object is selected to produce colliders.
The first collider of a tiles layer or an object is held by the layer or object entity itself, or by its TiledColliderChunk when colliders are split in chunks: only additional colliders get their own entity (see Physics Colliders).
Tiled Data and Derived Entities
Entities spawned by bevy_ecs_tiled fall into two categories:
- Tiled data: the map, layers, tiles, objects and images entities.
They hold what was defined in Tiled, such as a tile
TiledTile(tileset ID and tile ID) andTilePos, or aTiledObjectshape. - Derived entities and components:
TiledTilemaps, tile objects and text objects visuals, imageSprites and physics colliders. They are computed from the Tiled data duringTiledPostUpdateSystems::SyncRenderandTiledPostUpdateSystems::SyncPhysics.
Tiled data are the source of truth: you can spawn, despawn or modify them after the map has been loaded and derived entities will automatically follow. On the other hand, you should not modify derived entities yourself, since your changes could be overwritten.
See Modify a map at runtime for more information.
Most entities spawned by bevy_ecs_tiled share a few common components:
TiledMapReference, for layers, tiles, tilemaps, objects and images: the map entity they belong to, so you do not have to walk up the hierarchy to find it. Object visuals and physics colliders do not have one: use the map reference of their object or layer.TiledName, for layers, objects and tilemaps: the name given in Tiled to the layer, object or tileset.TiledProperties, for the map, layers and objects: their class and custom properties, as defined in Tiled.
TiledMapReference, TiledName, TiledProperties and TiledLayer are immutable components: they cannot be queried mutably, insert a new value instead.
World
A world is the optional top of the tree.
Loading a .world asset gives you a TiledWorld entity, which holds a handle to the corresponding TiledWorldAsset.
Each map of the world is spawned as a child of this entity, with a TiledWorldReference component giving its world and its WorldMapId: use the TiledWorldMaps system parameter to retrieve the spawned maps of a world.
All map-level settings can be added to the world entity: they are propagated down to every underlying map.
Map
Loading a .tmx asset gives you a TiledMap entity, which holds a handle to the corresponding TiledMapAsset.
It is the top of the tree, unless the map belongs to a world.
This is where you put map-level settings, such as the TilemapAnchor or the TiledMapLayerZOffset.
All the entities spawned from the map have a TiledMapReference to the map entity: use the TiledMapLayers, TiledMapTiles and TiledMapObjects system parameters to iterate over the entities of a given map.
Layers
Each Tiled layer is spawned as a child of the map, with a TiledLayer component.
Its value tells you the kind of layer, and what you will find below it:
| Variant | Children |
|---|---|
TiledLayer::Tiles | one TiledTile per tile and one TiledTilemap per tileset |
TiledLayer::Objects | one TiledObject per object |
TiledLayer::Image | a single TiledImage |
TiledLayer::Group | other layers, nested as in Tiled |
Tiles
Below each TiledLayer::Tiles, you find two kinds of sibling entities:
- the actual
TiledTileentities, one for each tile of the layer. TheirTiledTilecomponent gives their tileset ID and tile ID, and they are positioned with aTilePos. - one
TiledTilemapentity per tileset used by the layer, rendering the tiles using this tileset (given by itstileset_idfield). It holds theTilemapBundlefrombevy_ecs_tilemap, so this is where the tilemap size, grid size or texture live. You can reach the tilemaps of a layer through theTiledTilemapsrelationship, and the tilemap of a tile through itsTilemapId.
Tilemaps are derived from tiles: a tilemap is spawned when a tile using a new tileset is added to a layer, and despawned once it does not contain any tile anymore.
A position of a layer can only hold a single tile, as in Tiled.
To find the tile at a given position, use the TiledTileLayerStorage component of the layer (see Modify a map at runtime).
Spawning a tile, or moving a tile (ie. changing its TilePos), onto a position already used by another tile of the same layer despawns this other tile, whatever its tileset.
Note that:
- tiles outside of the map bounds (see
TiledMapAsset::tilemap_size) are neither rendered nor used to compute physics colliders: layers cannot grow, - moving a tile to another layer (ie. changing its
ChildOf) is not supported: despawn it and spawn a new one in the other layer instead.
Objects
Each Tiled object is spawned as a child of its TiledLayer::Objects, with a TiledObject component describing its shape (point, rectangle, polygon, …).
Most objects are a single entity, but two kinds of objects have children holding their visual.
Tile objects
A TiledObject::Tile has a child entity with a TiledObjectVisualOf component, spawned again whenever the TiledObject changes.
This child holds the Sprite of the tile and, if the tile is animated, a TiledAnimation.
From the object entity, you can reach it through the TiledObjectVisuals relationship.
Visual entities and TiledAnimation only exist with the render feature.
Text objects
A TiledObject::Text is rendered using two nested entities, if the text feature is enabled (it is by default):
- a
TiledObjectTextBoxchild, which represents the text box drawn in Tiled: itsAabbhas the size of the box. It also has aTiledObjectVisualOfcomponent: it is spawned again whenever theTiledObjectchanges. - below it, a
TiledObjectTextentity holding the actualText2dandTextFont. ItsAnchorand position inside the box follow the horizontal and vertical alignment set in Tiled.
To change the text content or style at runtime, query the TiledObjectText entity:
#![allow(unused)]
fn main() {
use bevy::prelude::*;
use bevy_ecs_tiled::prelude::*;
fn make_texts_red(mut texts: Query<&mut TextColor, Added<TiledObjectText>>) {
for mut color in texts.iter_mut() {
color.0 = Color::srgb(1., 0., 0.);
}
}
}
See the map_text example for a demo.
Images
An image layer has a single TiledImage child.
It holds the image handle, position, size and repeat settings, from which its Sprite is derived.
When the image is repeated along an axis, the sprite size is updated to cover what the camera can see.
Without the render feature, it has no image handle and no Sprite.
Physics Colliders
At the bottom of the hierarchy, you find physics colliders.
They are created for a source entity, either a TiledLayer::Tiles or a TiledObject, and are spawned again when their source changes (for instance when a tile of the layer is added, modified or removed).
Objects
The first collider of an object is inserted on the TiledObject entity itself, so you can directly add other physics components to it.
An object can have several colliders, for instance with TiledColliderStrategy::LineStrip: the next ones are spawned as children of the object entity.
Tiles layers
The tiles of a layer do not get their own colliders: the colliders of all the tiles of a TiledLayer::Tiles are created together, for the layer.
- By default, the first collider is inserted on the layer entity itself, and the next ones, if any, are spawned as children of the layer entity. They are all spawned again whenever a tile of the layer is added, modified or removed.
- When the colliders of the layer are split in chunks, the layer entity does not hold any collider.
Each chunk with at least one collider gets its own
TiledColliderChunkentity, child of the layer, which holds the first collider of the chunk; the next ones are children of the chunk entity. When a tile is modified, only the colliders of its chunk (and of the adjacent chunks) are spawned again.
In both cases, the layer entity remains the source of all these colliders.
Collider components
Each entity holding a collider carries:
- a
TiledColliderOfrelationship pointing to their source entity (possibly itself). On the other side, the source has aTiledColliderscomponent listing all its colliders. - a
TiledColliderSourcecomponent telling whether the collider comes from a tiles layer or from an object. - a
TiledColliderPolygonscomponent holding the raw geometry the collider was created from.
Since a source can have several colliders spread over several entities, add a rigid body to the source entity, given by TiledEvent<ColliderCreated>::source_entity(), rather than to each collider entity.
See the physics guide for more information.
Transform and Visibility Propagation
Bevy automatically propagates Transform and Visibility components down the entity hierarchy.
In practice:
If you change one of these components for a top-level entity (e.g., a layer), it will propagate down and apply to all child entities. For example:
- Adding
Visibility::Hiddento an object layer will hide all objects in that layer. - Moving an object layer will also move all objects it contains.
Special case for tiles:
Tiles are not rendered as individual entities, but as part of a “chunk” of several tiles for performance reasons.
Each individual tile entity does not have its own Transform or Visibility component.
Instead, these components are propagated down to the TiledTilemap, and bevy_ecs_tilemap handles updating the corresponding tile chunks.
Important:
Even though you technically could, you should not addTransformorVisibilitycomponents to individual tile entities:
- It will not have the intended effect—everything is handled at the tilemap level.
- It may hurt performance significantly.