Skip to main content

Getting Started

This guide covers the basic structure and concepts you need to understand when writing PlayCanvas scripts, whether you're using the modern ESM approach or the classic system.

What is a Script?

A script is a piece of JavaScript code that defines behavior for an Entity in your scene. Scripts are:

  • Reusable - The same script can be attached to multiple entities
  • Configurable - Use attributes to customize behavior per entity
  • Event-driven - Respond to lifecycle events and user interactions

Basic Script Structure

Every PlayCanvas script follows a similar pattern, regardless of which system you use:

import { Script } from 'playcanvas';

export class MyScript extends Script {
static scriptName = 'myScript';

/** @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);
}
}

Key points for ESM scripts:

  • Import the Script class from PlayCanvas
  • Export a class that extends Script
  • Use /** @attribute */ to expose properties to the editor
  • File must have .mjs extension

Core Concepts

Script Lifecycle

Scripts have several methods that are called automatically at different times:

  • initialize() - Called once when the script starts
  • update(dt) - Called every frame with delta time
  • postUpdate(dt) - Called after all updates complete
  • Event handlers for enable, disable, destroy

Learn more about the Script Lifecycle.

Attributes

Attributes let you expose script properties to the editor, making scripts configurable without code changes:

import { Color, Entity, Script } from 'playcanvas';

export class Configurable extends Script {
static scriptName = 'configurable';

/** @attribute */
speed = 5;

/** @attribute */
color = new Color(1, 0, 0);

/**
* @attribute
* @type {Entity}
*/
target;
}

Learn more about Script Attributes.

Accessing the Entity

Every script has access to the entity it's attached to via this.entity:

// Get the entity's position
const position = this.entity.getPosition();

// Find child entities
const child = this.entity.findByName('ChildName');

// Access components
const camera = this.entity.camera;
const rigidbody = this.entity.rigidbody;

Next Steps

  • Learn ESM Scripts: If you're starting fresh, check out ESM Scripts for the modern approach
  • Understand Lifecycle: Read about Script Lifecycle to understand when your code runs
  • Add Interactivity: Explore Events to make scripts communicate with each other
tip

Which system should I use?

For new projects, we recommend ESM Scripts as they offer better tooling, cleaner syntax, and modern JavaScript features. Classic scripts are still fully supported for existing projects.