Skip to content

ObjectScriptEntity - Inline script definitions for single-file games #16

Description

@Natuworkguy

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:

  1. 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
  2. Create entity/core/ose.py with ObjectScriptEntity:

    • Subclass Entity
    • Override __new__ to instantiate Entity, set scriptfile=None, and store scriptobj
  3. 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions