Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

- Relation components were incorrectly loaded from pickled data as pointing in the wrong direction.

## [5.5.0] - 2026-01-14

### Changed
Expand Down
2 changes: 1 addition & 1 deletion tcod/ecs/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def _relations_lookup_from(
for component_key, target_components in components.items():
for target in target_components:
relations_lookup[(component_key, ...)].add(origin)
relations_lookup[(component_key, origin)].add(origin)
relations_lookup[(component_key, target)].add(origin)
relations_lookup[(origin, component_key, None)].add(target)
relations_lookup[(..., component_key, None)].add(target)

Expand Down
22 changes: 22 additions & 0 deletions tests/test_ecs.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,28 @@
# ruff: noqa: D103


@pytest.mark.parametrize("component_key", [int, ("distance", int)])
@pytest.mark.parametrize("protocol", [2, 4, 5])
def test_pickle_component_relation_targets(component_key: type[int] | tuple[str, type[int]], protocol: int) -> None:
world = tcod.ecs.Registry()
moon, planet = world["moon"], world["planet"]
distance = 10
moon.relation_components[component_key][planet] = distance
moon.relation_tag["LandedOn"] = planet
assert set(world.Q.all_of(relations=[(component_key, planet)])) == {moon}

restored: tcod.ecs.Registry = pickle.loads(pickle.dumps(world, protocol=protocol)) # noqa: S301
moon, planet = restored["moon"], restored["planet"]

assert moon.relation_components[component_key][planet] == distance
assert set(restored.Q.all_of(relations=[(component_key, planet)])) == {moon}
assert set(restored.Q.all_of(relations=[(component_key, moon)])) == set()
assert set(restored.Q.all_of(relations=[(component_key, ...)])) == {moon}
assert set(restored.Q.all_of(relations=[(moon, component_key, None)])) == {planet}
assert set(restored.Q.all_of(relations=[(..., component_key, None)])) == {planet}
assert set(restored.Q.all_of(relations=[("LandedOn", planet)])) == {moon}


def test_world() -> None:
world = tcod.ecs.Registry()
with pytest.warns():
Expand Down