BabylonJS / Babylon.js

Babylon.js is a powerful, beautiful, simple, and open game and rendering engine packed into a friendly JavaScript framework.
http://www.babylonjs.com
Apache License 2.0
23.1k stars 3.41k forks source link

Update control.ts #15116

Closed Debenben closed 4 months ago

Debenben commented 4 months ago

fix Uncaught TypeError: Cannot read properties of undefined (reading 'getScene')

When trying to use BabylonJS version 7.6.0 or later I get a crash, I believe this proposed change should fix it, if there is more to it let me know and I can share the complete setup to reproduce the crash.

bjsplat commented 4 months ago

Please make sure to label your PR with "bug", "new feature" or "breaking change" label(s). To prevent this PR from going to the changelog marked it with the "skip changelog" label.

bjsplat commented 4 months ago

Snapshot stored with reference name: refs/pull/15116/merge

Test environment: https://babylonsnapshots.z22.web.core.windows.net/refs/pull/15116/merge/index.html

To test a playground add it to the URL, for example:

https://babylonsnapshots.z22.web.core.windows.net/refs/pull/15116/merge/index.html#WGZLGJ#4600

Links to test babylon tools with this snapshot:

https://playground.babylonjs.com/?snapshot=refs/pull/15116/merge https://sandbox.babylonjs.com/?snapshot=refs/pull/15116/merge https://gui.babylonjs.com/?snapshot=refs/pull/15116/merge https://nme.babylonjs.com/?snapshot=refs/pull/15116/merge

To test the snapshot in the playground with a playground ID add it after the snapshot query string:

https://playground.babylonjs.com/?snapshot=refs/pull/15116/merge#BCU1XR#0

bjsplat commented 4 months ago

WebGL2 visualization test reporter:

https://babylonsnapshots.z22.web.core.windows.net/refs/pull/15116/merge/testResults/webgl2playwright/index.html

bjsplat commented 4 months ago

Visualization tests for WebGPU (Experimental) Important - these might fail sporadically. This is an optional test.

https://babylonsnapshots.z22.web.core.windows.net/refs/pull/15116/merge/testResults/webgpuplaywright/index.html

deltakosh commented 4 months ago

How do you repro the issue?

I have no problem with that fix but curious to see how it can happen

Debenben commented 4 months ago

The crash can be reproduced like this:

    var advancedTexture = BABYLON.GUI.AdvancedDynamicTexture.CreateFullscreenUI("ui");
    var control = new BABYLON.GUI.TextBlock("control");
    var panel1 = new BABYLON.GUI.StackPanel("panel1");
    panel1.addControl(control);
    advancedTexture.addControl(panel1);
    var panel2 = new BABYLON.GUI.StackPanel("panel2");
    //panel1 is added to texture, but not panel2
    panel2.addControl(control); // <-- crashes here

In my code I needed to rearrange controls in a stackpanel. There are probably better ways to do this, but at the time I used a temporary stack panel here: https://github.com/Debenben/babyele/blob/master/src/guimodeselection.ts#L206