I had a hard time writing the classes in the new ESM scripts. I noticed, that in the editor in the list of the script parameters the script name is always listed with a lowercase first letter.
In the documentation I found that example for the Basic Script Structure
import { Script } from 'playcanvas';
export class MyScript extends Script {
/** @attribute */
speed = 10;
initialize() {
// Called once when the script starts
console.log('Script initialized!');
}
update(dt) {
// Called every frame
this.entity.rotate(0, this.speed * dt, 0);
}
}
When I copy the code and paste it in the editor into a script called "my-script.mjs” and check the parameters shown in the editor I see the name “myScript” listed. After some investigation I discovered, that when I create a new script in the editor the default code is different - it contains the line static scriptName = "myScript"
.
import { Script } from 'playcanvas';
/**
* The {@link https://api.playcanvas.com/classes/Engine.Script.html | Script} class is
* the base class for all PlayCanvas scripts. Learn more about writing scripts in the
* {@link https://developer.playcanvas.com/user-manual/scripting/ | scripting guide}.
*/
export class MyScript extends Script {
static scriptName = "myScript"
/**
* Called when the script is about to run for the first time.
*/
initialize() {
}
/**
* Called for enabled (running state) scripts on each tick.
*
* @param {number} dt - The delta time in seconds since the last frame.
*/
update(dt) {
}
}
When I add that line to my scripts, they work as expected. Please update the documentation.