[Unity Toolkit / Babylon engine] Not able to access engine from TS or JS

This is not specifically a Unity toolkit question but more of a generic question on how to reference the engine to access the enterPointerlock and exitPointerlock functions.

When I try and access it from my TS class it outputs

            this.scene.onPointerDown = function () {
                if (!document.pointerLockElement) {
                    this.engine.enterPointerlock();
                }
                else {
                    this.engine.exitPointerlock();
                }
            };

But as you can see it’s undefined here:
image

I’m assuming this is specifically a question of the engine not in scope of the function any longer. Does anyone know how I can either access the engine using the toolkit so I can fix my issue or how to handle it from JS and what I’m doing wrong?

Hey what about this.scene.getEngine() ?

1 Like

I honestly have no idea why I’m having trouble with this…

Can you make sure you are on the very latest version?

Unfortunately I don’t know if I have access to that using the Toolkit, I know when speaking to Macky he was saying he needs to upgrade.
image

let’s ping the master then @MackeyK24

1 Like

Can you send me that script file so I can look at it

I think it’s scope… you are using the function()

In a function… this refers to the local scope of function…

Use ()=>{} instead

Also … the engine.enterPointerLock may not be in Babylon Version 4.0.3

Try and update the Babylon.js … hopefully the legacy scene manager still work with 4.1.x or Babylon Engine

Otherwise you gotta wait for new GLTF version of the toolkit

1 Like

Can confirm,
the standalone enterPointerlock and exitPointerlock functions are coming with Babylon 4.1

Is this possible with the Unity Toolkit?

Yep… just replace the Babylon JavaScript and TypeScript declaration file in the templates folder

I just hope it’s compatible with that scene manager version…

I had to fix some lightmap and terrain textures so I will try update classic edition to 4.1.x

I just want to make sure I’m doing this correctly, I replace the Assets\Babylon\Library\babylon.bjs with this: https://github.com/BabylonJS/Babylon.js/blob/master/dist/babylon.js

and the Assets\Babylon\Library\babylon.d.ts with this: https://github.com/BabylonJS/Babylon.js/blob/master/dist/babylon.d.ts

?

Yep… looks right

I replaced Assets\Babylon\Library\babylon.bjs with this: Babylon.js/babylon.js at master · BabylonJS/Babylon.js · GitHub

and Assets\Babylon\Library\babylon.d.ts with this: Babylon.js/babylon.d.ts at master · BabylonJS/Babylon.js · GitHub

and it built just going to test the cursor now.

So it builds but I can’t access the functions with this code:

            this.scene.onPointerDown = ()=>{
                if (!document.pointerLockElement) {
                    scene.getEngine().enterPointerlock();
                }

Produces this error:

TypeError: getEngine().enterPointerlock is not a function
    at t.scene.onPointerDown (f:\Projects\Unity\myPro\Export\scenes\myPro.babylon.js:1790:24)
    at e._processPointerDown (f:\Projects\Unity\myPro\Export\scripts\babylon.js:16:253661)

Just bumping this to see if there’s a solution to it.