[Feature] Deliver physics backend events to subscribers and USC scripts - #1139
Merged
untoldengine merged 1 commit intoAug 12, 2026
Merged
Conversation
PR C of the physics plugin readiness plan (discussion untoldengine#1116): - PhysicsEvents subscription hub in the FrameEvents style: onContact/ onTrigger/onActivation return EventSubscription tokens; handlers run on the frame thread during the coordinator's post-step drain, in backend delivery order. droppedEventCount surfaces backend buffer overflow as a counter, never an allocation mid-step. - PhysicsEventDispatchSink replaces the coordinator's inert sink: fans events out to subscribers and fires USC script events — contactBegan fires the previously never-fired OnCollision (plus OnCollision:<name> when the other entity is named, matching the builder's tag form), and trigger enter/exit fire OnTriggerEnter/OnTriggerExit the same way. - With no external backend installed nothing is dispatched — the hub is dormant and runtime behavior is unchanged. 9 new tests with an event-emitting mock backend: subscription dispatch and cancellation, dormancy without a backend, drop-counter accumulation, USC OnCollision (untagged, tagged, phase-filtered), trigger enter/exit, and gameMode gating.
4 tasks
untoldengine
approved these changes
Aug 12, 2026
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
PR C of the narrowed phase-1 plan from discussion #1116 (follows #1123 and #1129): event delivery. Backends already buffer contact/trigger/activation events internally and hand them to the coordinator's sink on the frame thread after each fixed substep (
drainEvents, from PR A's threading contract) — this PR makes that drain go somewhere. With no external backend installed nothing is ever dispatched: the built-in integrator has no collision detection, so the whole path stays dormant and runtime behavior is unchanged.Sources/UntoldEngine/Physics/PhysicsEvents.swift— subscription hub in theFrameEventsstyle:onContact/onTrigger/onActivationreturn the existingEventSubscriptiontoken; handlers run on the frame thread during the post-step drain, in backend delivery order; subscriptions can be created/cancelled from any thread (snapshot-under-lock dispatch, same reentrancy semantics asFrameEventDispatcher).droppedEventCountsurfaces backend fixed-capacity buffer overflow as a running counter — never an allocation or error mid-step.PhysicsEventDispatchSinkreplaces the coordinator's inert placeholder sink and also wires USC:contactBeganfires the previously never-firedOnCollisionon both entities, plus the builder's tagged formOnCollision:<name>when the other entity has an explicit name;triggerEntered/triggerExitedfireOnTriggerEnter/OnTriggerExitthe same way (both names already appear inUSCInstruction's event-handler documentation). Firing respectsgameMode, via the existingUSCSystem.triggerEventpath — no USC internals changed.OnCollision(untagged, tagged-by-name, and phase-filtered — persisted/ended must not re-fire), trigger enter/exit scripts, andgameModegating.The only files touched are in
Physics/plus the two physics test files. Follow-up per the plan: PR D (raycast facade) completes phase 1.Test plan
swift test --filter PhysicsEventsTests— 9/9 passPhysicsCoordinatorTests+PhysicsBackendRegistryTests— 30/30 passUntoldEngineTestsmodule: 1093 tests, no new failures (only the known env-dependentExternalRenderExtensionPackageTests)make lintwith pinned SwiftFormat 0.60.1 — clean