Whole file
benmoran56/esper
The author described this change as “FIX: removing last component deletes entity as well (#83)”. It counts as a record because the checks below fail on the code as it stood at 730b92940 and pass on 226d876e0, with nothing else changed between the two runs.
Projectbenmoran56/esper
Fix saved2023-05-10
Sharing licenceMIT · LICENSE
Change size+5 −8
What the code was meant to do, written into the code itself as a save note
FIX: removing last component deletes entity as well (#83)
The change
| 1 | + | import inspect as _inspect | |
| 1 | 2 | import time as _time | |
| 2 | 3 | ||
| 3 | 4 | from types import MethodType as _MethodType | |
| 4 | 5 | ||
| 6 | + | from typing import cast as _cast | |
| 5 | 7 | from typing import Iterable as _Iterable | |
| 6 | 8 | from typing import List as _List | |
| 7 | 9 | from typing import Optional as _Optional | |
| ⋯ | |||
| 317 | 319 | self._entities[entity][component_type] = component_instance | |
| 318 | 320 | self.clear_cache() | |
| 319 | 321 | ||
| 320 | - | def remove_component(self, entity: int, component_type: _Type[_C]) -> int: | |
| 322 | + | def remove_component(self, entity: int, component_type: _Type[_C]) -> _C: | |
| 321 | 323 | """Remove a Component instance from an Entity, by type. | |
| 322 | 324 | ||
| 323 | - | A Component instance can be removed by providing its type. | |
| 325 | + | A Component instance can only be removed by providing its type. | |
| 324 | 326 | For example: world.delete_component(enemy_a, Velocity) will remove | |
| 325 | 327 | the Velocity instance from the Entity enemy_a. | |
| 326 | 328 | ||
| ⋯ | |||
| 332 | 334 | if not self._components[component_type]: | |
| 333 | 335 | del self._components[component_type] | |
| 334 | 336 | ||
| 335 | - | del self._entities[entity][component_type] | |
| 336 | - | ||
| 337 | - | if not self._entities[entity]: | |
| 338 | - | del self._entities[entity] | |
| 339 | - | ||
| 340 | 337 | self.clear_cache() | |
| 341 | - | return entity | |
| 338 | + | return self._entities[entity].pop(component_type) | |
| 342 | 339 | ||
| 343 | 340 | def _get_component(self, component_type: _Type[_C]) -> _Iterable[_Tuple[int, _C]]: | |
| 344 | 341 | entity_db = self._entities | |
The check that tells the two apart
fail→pass·tests/test_world.py::TestRemoveComponent::test_remove_component_returns_removed_instance
fail→pass·tests/test_world.py::TestRemoveComponent::test_remove_last_component_leaves_empty_entity
Check file tests/test_world.py, taken without changes from the fix and copied onto the older code, so the exact same check runs against both versions.
Origin and history
The code before it730b92940a5572aabb2948a01738072d5ea97792
Broken version dated2023-05-09
Moduleesper.__init__
Units changedWorld
Fingerprint2503a099d31533ee
Checked2026-08-18 by goldset/0.1
Every field above is generated by our program. None of it is written by hand.