This template allows for the use of TypeScript in Playcanvas projects. It can be used to write ScriptComponents in either TypeScript or JavaScript, and it works as a one-way pipeline from Git to the Playcanvas Editor. Git and Playcanvas Editor branches are synced based on their names. If you are in different branches then sync won't be done.
import { createScript } from "../utils/createScript";
@createScript({
stringAttribute: {
type: "string",
},
numberAttribute: {
type: "number",
default: 0,
}
})
export class MyAwesomeScript extends pc.ScriptType {
public stringAttribute?: string;
public numberAttribute: number = 0;
public initialize() {}
public postInitialize(): void {}
public update(dt: number) {}
public postUpdate(): void {}
public swap(): void {}
}
The limitations of the Editor include the following:
- No offline coding, so you cannot work in your own environment.
- No ability to use your favorite IDE, and the included version of VS Code has limitations.
- Only pure JavaScript is supported. You have to write code according to your .
- No type safety or TypeScript support.
- No MR pipeline, which means:
- No reviews.
- The ability to overwrite code without anyone noticing.
- Potentially difficult conflicts and unexpected code overwriting.
- It is easy to accidentally modify the global scope, which can lead to:
- Name collisions.
- High coupling.
- No modules or require/import/export statements, so:
- You cannot use the NPM ecosystem or ScriptComponents written by the Playcanvas team.
- You have to break the DRY principle by copying and pasting repeating logic/utils/libs.
- No unit tests or linting.
- The possibility that someone else may change your code without you or anyone else knowing.
src/
--- source code. Write your code here. (ScriptComponents, ScriptSystems, utils, React/Vue/Svelte and so on)src/components/
--- ScriptComponents.src/index.ts
--- entry point. You should import all your code here.
dist/
--- compiled code. The directory is uploaded to your Playcanvas project. This folder is ignored by git.pcconfig.sample.json
--- sample config file. Copy it topcconfig.json
and fill in the values.pcconfig.json
is ignored by git. Read more about pcconfig in Local Install.script/
--- scripts for syncing with Playcanvas Editor.
- TypeScript/Modern JavaScript.
- Git and Playcanvas Editor branches are synced based on their names.
- Github Actions.
- ESLint.
- Github Codespace (TODO: improve configs).
Since
- Github allows to have only one fork per account
- it is suggested to have one git repo for one Playcanvas project.
you can create a fork repo manually:
- Create a new empty repository on Github
git clone https://github.com/<username>/<repo>.git
--- clone the new repositorycd <repo>
--- navigate to the repositorygit remote add upstream https://github.com/querielo/playcanvas-typescript-template.git
--- add the upstream repositorygit pull upstream main --allow-unrelated-histories
--- pull upstream main- Resolve any conflicts that may have arisen from the previous command
git push origin main
--- push origin main- Change license in
LICENSE
file if needed.
- Fork and clone the repository
- Copy
pcconfig.sample.json
and rename topcconfig.json
:- PLAYCANVAS_API_KEY: Copy API key (API token) from your PlayCanvas account page (playcanvas.com/account). Paste it into pcconfig.json as PLAYCANVAS_API_KEY.
- PLAYCANVAS_PROJECT_ID: Open your project in Playcanvas Editor. Open the browser console. Copy
config.project.id
. Paste it into pcconfig.json as PLAYCANVAS_PROJECT_ID. - NOTE:
pcconfig.json
is in.gitignore
, so it won't be committed to the repository.
- Run
npm ci
. - Run
npm run watch
. Start development.
npm run watch
--- watch for changes insrc/
and compile them todist/
. It also syncs the code with the Playcanvas Editor. It is recommended to run this command while developing.npm run watch:dev
--- similar tonpm run watch
, but it builds the code insrc/
todist/
without minification. It's useful for debugging.npm run build
--- compile the code insrc/
todist/
.npm run push
--- push the code indist/
to the Playcanvas Editor.npm run lint
--- run ESLint.npm run lint:fix
--- run ESLint and fix errors.
To have your code automatically updated by GitHub Actions after committing and merging on GitHub, you can set the following secrets for your project:
- PLAYCANVAS_API_KEY
- PLAYCANVAS_PROJECT_ID
- Go to your project's settings on GitHub.
- Under "Secrets" -> "Actions", click "New repository secret".
- Type in a name for the secret and the value of the secret.
- Click "Add secret" to save the secret.
It is highly recommended to use this feature as it can help automate your workflow and ensure that your code in Playcanvas Editor is always up-to-date. Read more about Billing for Github Actions. Free accounts have about 2000 CI/CD minutes/month (read here)
- Create a fork of the master branch in the Playcanvas Editor.
- In git, create a fork of the main branch with the same name as the fork in the Playcanvas Editor.
- Run
npm run watch
to begin development. - Once development is complete, create a merge request (MR) into the main branch in Github. Once the MR is approved, merge the branch in the Playcanvas Editor into master, and then merge the corresponding branch in Github into the main branch. Go to the step 1.