subtractToRef is not a function in Event Handler

Registering my event:

window.addEventListener('isClosed', reDrawFloorplan);

Once I trigger it:

    () => window.dispatchEvent(new Event('isClosed')
), 500);

I get:
my code at line 17:

corners[1].subtractToRef(corners[0], line);

Line 17 is the first line that uses Babylon.js code.

Why is this happening? Should I be using an Observable instead or its not related?

This is impossible to tell without a repro. It might be and is more likely just a typo. We need to see how and when the array is initialized filled in and so on.

Could you repro in a playground ?

I was saving whole meshes in my array, not the positions.

It works here

Fixed it but now I get a different error:

My array looks fine though.

Any idea what it could be?

This means the output of subtractToRef is undefined in some cases e.g. at some point in your code line is undefined.

Found the problem. I didnt assign BABYLON.Vector3.Zero() in the begin.

    let line; // = BABYLON.Vector3.Zero();

    cornersPos[1].subtractToRef(cornersPos[0], line);

Why do I have to assign Vector Zero first?

API both parameters have to be vectors Vector3 - Babylon.js Documentation

Does not have to be a zero vector but is a simple way of making sure it is a Vector3

1 Like

This prevents GC snd ensures you can reuse at max from frame to frame.

1 Like