playcanvas / playcanvas-editor-ts-template

A simple TypeScript template for PlayCanvas that can also sync with your playcanvas.com project
MIT License
21 stars 9 forks source link

Referencing other scripts through entities gives error #13

Closed battall closed 1 month ago

battall commented 8 months ago

For example;

// vehicle-wheel.js
class VehicleWheel extends pc.ScriptType {
  /** @type {boolean} */
  isFront;
}
pc.registerScript(VehicleWheel, "VehicleWheel");
// vehicle.js
class Vehicle extends pc.ScriptType {
  /** @type {pc.Entity[]} */
  wheels;
  /** @type {number} */
  maxEngineForce;
  /** @type {number} */
  maxBrakingForce;
  /** @type {number} */
  maxSteering;

  initialize() {
    this.wheels.forEach((wheel) => {
      if (!wheel.script) throw ""; // adding !wheel.script.VehicleWheel gives error too
      wheel.script.VehicleWheel; // Property 'VehicleWheel' does not exist on type 'ScriptComponent'.ts(2339)
    });
  }
}
pc.registerScript(Vehicle, "Vehicle");

note: i am a typescript noob so

battall commented 8 months ago

i think i made it work but dont know if its a good practise or not, updated global.d.ts; before i was getting error like "Subsequent variable declarations must have the same type", and removed const pc : typeof _pc;

import * as pc from "playcanvas";

import VehicleWheel from "./Scripts/Vehicle/vehicle-wheel.js";

declare global {
  namespace pc {
    class ScriptComponent {
      VehicleWheel?: InstanceType<typeof VehicleWheel>;
    }
  }
}

export {};
ysftulek commented 2 months ago

I was also getting this error:

Cannot redeclare block-scoped variable 'pc'.ts(2451) playcanvas.d.ts(40514, 21): 'pc' was also declared here.

and removing const pc : typeof _pc; this line helped me to get rid of the error.

willeastcott commented 1 month ago

If you pull latest, this is now fixed. Feel free to reopen if you still have problems.