engine icon indicating copy to clipboard operation
engine copied to clipboard

The triggerleave event doesn't work as expected after reloading scene

Open albertvanveen opened this issue 2 years ago • 3 comments

The triggerleave event doesn't work anymore after reloading the scene, when the trigger entity is inside a rigidbody entity.

A workaround is to enable the trigger entity (or collision component) a frame later, but I don't think this is possible in all situations.

I think this bug can easily break projects that use a lot of trigger entities.

Here is a project with the issue: https://playcanvas.com/editor/scene/1893502

Here is a project with a workaround: https://playcanvas.com/editor/scene/1894025

albertvanveen avatar Nov 06 '23 15:11 albertvanveen

Workaround does not work for me. ENTER still loops.

defektu avatar Nov 06 '23 23:11 defektu

I assume this issue is happening because you are loading the same scene with same entities. Just try duplicating the scene and switch to that. New entities will trigger. changeScene does not destroy hierarchy by default.

This will possibly solve your issue for same scenes too. Destroying root children leads to new hierarchy loads with new collisions Loading Scenes

Also here is an example project: https://playcanvas.com/editor/scene/1894442

Possibly a flaw with collision objects' "others" list? This is not propagating because they are not destroying themselves while changeScene

defektu avatar Nov 06 '23 23:11 defektu

Found the issue, onBeforeAddHierarchy is not destroying scene within https://github.com/playcanvas/engine/blob/ae26d8dfb1346cad9cd6ddcdcd1239112b7de6e8/src/framework/scene-registry.js#L412

Setting 412 to this._loadSceneHierarchy(sceneItem, null, callback); and just calling the function before that solves the issue. But gives an error about sceneItem.data being undefined

Here is the changed code that works project with issue

    changeScene(sceneItem, callback) {
        const app = this._app;

        const onBeforeAddHierarchy = (sceneItem) => {
            // Destroy all nodes on the app.root
            const { children } = app.root;
            while (children.length) {
                children[0].destroy();
            }
            // app.applySceneSettings(sceneItem.data.settings)
        };

        onBeforeAddHierarchy(sceneItem)
        this._loadSceneHierarchy(sceneItem, null, callback);
    }

defektu avatar Nov 07 '23 02:11 defektu