@@ -334,6 +334,8 @@ def __setitem__(self, key: ComponentKey[T], value: T) -> None:
334334
335335 if old_value is None :
336336 tcod .ecs .query ._touch_component (self .entity .world , key ) # Component added
337+ elif old_value == value :
338+ return
337339
338340 self .entity .world ._components_by_entity [self .entity ][key ] = value
339341 self .entity .world ._components_by_type [key ][self .entity ] = value
@@ -682,8 +684,15 @@ def __getitem__(self, target: Entity) -> T:
682684 def __setitem__ (self , target : Entity , component : T ) -> None :
683685 """Assign a component to the target entity."""
684686 world = self .entity .world
685- if target in world ._relation_components_by_entity [self .entity ][self .key ] is not None :
686- del self [target ]
687+
688+ old_value = world ._relation_components_by_entity [self .entity ][self .key ].get (target )
689+ if old_value is None : # Relation added
690+ tcod .ecs .query ._touch_relations (
691+ world , ((self .key , target ), (self .key , ...), (self .entity , self .key , None ), (..., self .key , None ))
692+ )
693+ elif old_value == component :
694+ return
695+
687696 world ._relation_components_by_entity [self .entity ][self .key ][target ] = component
688697
689698 world ._relations_lookup [(self .key , target )] = {self .entity }
@@ -716,6 +725,10 @@ def __delitem__(self, target: Entity) -> None:
716725 if not world ._relations_lookup [(..., self .key , None )]:
717726 del world ._relations_lookup [(..., self .key , None )]
718727
728+ tcod .ecs .query ._touch_relations (
729+ world , ((self .key , target ), (self .key , ...), (self .entity , self .key , None ), (..., self .key , None ))
730+ )
731+
719732 def __iter__ (self ) -> Iterator [Entity ]:
720733 """Iterate over the targets with assigned components."""
721734 by_entity = self .entity .world ._relation_components_by_entity .get (self .entity )
0 commit comments