Skip to content

Implement proper scene cloning#311

Merged
waynemwashuma merged 7 commits into
wimaengine:devfrom
waynemwashuma:scene-cloning
Apr 4, 2026
Merged

Implement proper scene cloning#311
waynemwashuma merged 7 commits into
wimaengine:devfrom
waynemwashuma:scene-cloning

Conversation

@waynemwashuma
Copy link
Copy Markdown
Collaborator

Objective

  • Introduce type-safe cloning of via TypeRegistry
  • Fix incorrect static polymorphism in clone() implementations
  • Implement cloning semantics into the scene system

Solution

1. Incorrect this.copy usage in static context

Using this.copy() inside static methods is unsafe under inheritance. It may resolve to the wrong constructor or break when methods are detached and no context binding for this.

Fix:

  • Replaced all:

    static clone(target) {
      return this.copy(target)
    }

    with:

    static clone(target) {
      return ClassName.copy(target)
    }

Why:
Ensures deterministic dispatch and avoids reliance on dynamic this binding in static context.

2. Centralized cloning via TypeRegistry

Problem:
Scene instantiation previously reused component references (no cloning), leading to:

  • Shared mutable state
  • Broken scene isolation
  • Undefined behavior in instancing

Changes:

  • Resolve clone methods via TypeRegistry
  • Clone each component explicitly
  • Log warnings for non-cloneable components

Additional behavior:

  • Automatically injects Parent if missing → guarantees hierarchy integrity.

Why this approach:

  • Avoids hardcoding clone semantics per component

Showcase

  • New demos:

    • multiple_instances (2D & 3D)

      • Demonstrates:
        • Scene instancing
        • Scene instance isolation from other instances

Migration guide

scene.toWorld(world, instance)

Now requires:

scene.toWorld(world, instance, typeRegistry, instanceEntity)

Custom components

If you want your component to be scene-compatible:

  • Ensure it is registered in TypeRegistry
  • Provide:
static copy(source, target) {}
static clone(target) {}

Notes

  • Components without registered clone will be skipped with a warning
  • Parent linkage is now enforced automatically during instancing

Checklist

  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.

`clone` required context binding but that beats
the purpose of static methods
Previous version of scening did a copy by reference thus
destroying the uniqueness of scenes causing a change in
one scene to affect the other
@waynemwashuma waynemwashuma self-assigned this Apr 4, 2026
@waynemwashuma waynemwashuma added the type:enhancement New feature or request label Apr 4, 2026
@github-actions github-actions Bot added mod:render mod:window mod:physics mod:transform mod:movable mod:audio mod:tween This PR/issue affects the `tween` package mod:hierarchy mod:animation This PR/issue affects the animation system mod:time This PR/issue affect the time package mod:scene This affects the scene package mod:emitter This PR/issue affects emitter package labels Apr 4, 2026
@waynemwashuma waynemwashuma merged commit fd50101 into wimaengine:dev Apr 4, 2026
7 checks passed
@waynemwashuma waynemwashuma deleted the scene-cloning branch April 4, 2026 08:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

mod:animation This PR/issue affects the animation system mod:audio mod:emitter This PR/issue affects emitter package mod:hierarchy mod:movable mod:physics mod:render mod:scene This affects the scene package mod:time This PR/issue affect the time package mod:transform mod:tween This PR/issue affects the `tween` package mod:window type:enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant