instanceof is returning false when the value is actually an instance

Viewed 55

Is there a way to check what instance is being used?

I am logging this to the console:

import { OrthographicCamera } from 'three';

// Later in the file:

console.log(camera instanceof OrthographicCamera, camera);

and It outputs false, but when I log the value of camera to the console, I get this:

Proof of OrthographicCamera

I am not sure why it is showing that the camera isn't an instance, so is there a way to get more information as to what it is actually looking at?

Here is where the camera is created:

export function Camera(options?: CameraOptions) {
  return function (target: new () => object) {
    return class GameCamera extends target {
      readonly camera!: Camera;  
      constructor() {
        super();
        const dim = this.cameraDimensions();

        // create the Three camera
        this.camera = new OrthographicCamera(dim.left, dim.right, dim.top, dim.bottom, options?.near ?? 0, options?.far ?? 100);
      }
    }
  }
}}

The camera gets created in the Decorator and added to the gameObjects array. Then when I get the activeCamera it is a reference to the above decorator that extends target.

Here is where I am doing the instanceof lookup:

@Injectable({ providedIn: 'root' })
export class Camera {
  get activeCamera() {
    return Engine.activeCamera;
  }

  mouseToWorldPoint(mousePoint: Vector3) {
    if (this.activeCamera) {
      // Here is the check:
      console.log(camera instanceof OrthographicCamera, camera);
    }
  }
}

This is how I get the camera:

export class Engine {
  static gameObjects: GameObject[] = [];
  static get activeCamera() {
    return this.gameObjects.find(i => i.gameObjectType === 'camera' && i.isActive === true) as GameCamera | undefined;
  };
}

I have broken the project up into multiple npm workspaces:

{
  "name": "game-engine",
  "version": "1.0.0",
  "scripts": {
    "start": "npm run start --workspace=test"
  },
  "workspaces": [
    "test",
    "packages/core",
    "packages/common",
    "packages/input",
    "packages/objects"
  ]
}

2 of the workspaces get information from the three module.

  • class GameCamera is apart of the packages/objects module (creates the camera).
  • class Camera is apart of the packages/common module (checks for instance).

Note: This been working like this for some time and just seems to have started happening.

1 Answers

To fix my issue what I did is in the packages/core package I added an export to export threejs

index.ts (@engine/core)

import * as Three from 'three';
export { Three };

Then when I need to use threejs in another package I just use the one in core like this:

@engine/objects/something.ts

import { Three } from '@engine/core';

export class CreateObject {
  constructor(){
    this.obj = new Three.Object3D();
  }
}
Related