Skip to content

feat: support custom Python EnvironmentObject subclasses#23

Merged
YJack0000 merged 1 commit into
mainfrom
feat/custom-python-objects
Feb 24, 2026
Merged

feat: support custom Python EnvironmentObject subclasses#23
YJack0000 merged 1 commit into
mainfrom
feat/custom-python-objects

Conversation

@YJack0000

Copy link
Copy Markdown
Owner

Summary

  • Add pybind11 trampoline class to enable Python subclassing of EnvironmentObject
  • Add generic add_object/remove_object methods to Environment
  • Custom objects participate in the simulation loop via postIteration() override
  • Custom objects are spatially indexed and appear in get_all_objects()

Usage Example (Python)

from simevopy import Environment, EnvironmentObject

class Obstacle(EnvironmentObject):
    def __init__(self):
        super().__init__(0, 0)
        self.damage = 10

    def post_iteration(self):
        pass  # obstacles don't move

class Trap(EnvironmentObject):
    def __init__(self):
        super().__init__(0, 0)
        self.triggered = False

    def post_iteration(self):
        pass

env = Environment(1000, 1000)
env.add_object(Obstacle(), 500, 500)
env.add_object(Trap(), 300, 300)

Discussion Points

  • Custom objects 會出現在 get_all_objects() 中,但不會出現在 get_all_organisms()get_all_foods()
  • 目前 custom objects 在 spatial index 中有位置,所以會被 organism 的 react()/interact() 看到。如果搭配 PR3 的 custom strategy,Python 端可以根據 object type 自定義互動行為
  • postIteration() 會在每個 tick 被呼叫,可以用來實作會隨時間變化的地圖物件(如消失的牆壁、移動的障礙物)
  • 如果 postIteration() 需要回呼 Python,目前的單線程 postIteration 迴圈是安全的

Test plan

  • All 3 existing Python tests pass
  • Custom Python EnvironmentObject can be created, added, and removed
  • Verify custom objects are visible in organism react/interact phase
  • Test postIteration override from Python

🤖 Generated with Claude Code

@YJack0000
YJack0000 force-pushed the feat/custom-python-objects branch from 809b32c to a23472d Compare February 24, 2026 19:08
Add PyEnvironmentObject trampoline class to enable Python subclassing
of EnvironmentObject with proper virtual method dispatch. Add generic
add/remove overloads for EnvironmentObject in Environment, with
corresponding add_object/remove_object Python bindings.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@YJack0000
YJack0000 force-pushed the feat/custom-python-objects branch from a23472d to de3348b Compare February 24, 2026 19:13
@YJack0000
YJack0000 merged commit 4df8151 into main Feb 24, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant