Currently, entity scripts must be defined in separate files and loaded via file paths. This prevents users from writing complete games in a single Python file.
Proposed Solution:
Add a new ObjectScriptEntity subclass that accepts a script class directly instead of a file path. The class should implement the following:
- Accept a
scriptobj argument (type: ScriptProtocolType) instead of scriptfile
- Set
scriptfile to None internally
- Check which lifecycle methods exist on the script class at creation time and skip calling missing methods
Implementation Details:
-
Add ScriptProtocol and ScriptProtocolType to engine/core/types.py:
ScriptProtocol defines the structural contract (init, update, event methods)
ScriptProtocolType = type[ScriptProtocol] to accept the class itself
- Delete
ScriptProtocol after alias creation to keep public API clean
-
Create entity/core/ose.py with ObjectScriptEntity:
- Subclass
Entity
- Override
__new__ to instantiate Entity, set scriptfile=None, and store scriptobj
-
Update documentation with usage examples and clarify that self in script methods is the entity instance, not the script object
Acceptance Criteria:
- Users can write a complete game in one
.py file without external script files
- Type checkers recognize script objects via
ScriptProtocolType
- Documentation clarifies the
self → entity mapping
Currently, entity scripts must be defined in separate files and loaded via file paths. This prevents users from writing complete games in a single Python file.
Proposed Solution:
Add a new
ObjectScriptEntitysubclass that accepts a script class directly instead of a file path. The class should implement the following:scriptobjargument (type:ScriptProtocolType) instead ofscriptfilescriptfiletoNoneinternallyImplementation Details:
Add
ScriptProtocolandScriptProtocolTypetoengine/core/types.py:ScriptProtocoldefines the structural contract (init, update, event methods)ScriptProtocolType = type[ScriptProtocol]to accept the class itselfScriptProtocolafter alias creation to keep public API cleanCreate
entity/core/ose.pywithObjectScriptEntity:Entity__new__to instantiateEntity, setscriptfile=None, and store scriptobjUpdate documentation with usage examples and clarify that
selfin script methods is the entity instance, not the script objectAcceptance Criteria:
.pyfile without external script filesScriptProtocolTypeself→ entity mapping