feat: add injectable Python behavior strategies for Organism#22
Merged
Conversation
YJack0000
force-pushed
the
feat/python-behavior-system
branch
from
February 24, 2026 18:59
7461285 to
22634bf
Compare
Add ReactionStrategy and InteractionStrategy callbacks that allow Python callers to override organism behavior without modifying C++. Strategies are propagated to offspring via reproduce(). Multi-threading in handleReactions is preserved when all organisms use default C++ strategies; falls back to single-threaded when any organism has a custom strategy to avoid GIL deadlocks. Includes custom_behavior.py example demonstrating herbivore/predator species with distinct Python-defined behaviors. Co-Authored-By: Claude Opus 4.6 <[email protected]>
YJack0000
force-pushed
the
feat/python-behavior-system
branch
from
February 24, 2026 19:11
22634bf to
25f518a
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
ReactionStrategyandInteractionStrategyas injectablestd::functiontypes onOrganismreproduce()Usage Example (Python)
New Python API
organism.set_reaction_strategy(fn)fn(organism, objects) -> (dx, dy)organism.set_interaction_strategy(fn)fn(organism, objects) -> Noneorganism.add_life_span(amount)Discussion Points
handleInteractions和handleReactions改為 single-threaded 以避免 Python callback 的 GIL deadlock。未來如果要恢復多線程,需要在 C++ 端做 GIL release/acquire 管理nearby_objects包含所有EnvironmentObject(Food + Organism),Python 端需要用hasattr或isinstance判斷類型。考慮是否要提供 typed accessor 來簡化Test plan
🤖 Generated with Claude Code