Typescript template for PlayCanvas using Browserify and Gulp.js

Hi, I created my own solution for using TypeScript with PlayCanvas. I am actively using it for ten months or so now, and decided to share.

Github repo: matgawin/playcanvas-typescript-template
Example: TestTypescript

It compiles and bundles all scripts using Browserify and gulp.js.
It uses playcanvas-sync to automatically synchronize with your project, but it also uses dotenv, so there is no need to copy any files anywhere, just copy your API key and project_id to .env.
Not only that, but it also uses decorators to simplify creating scripts. This solution was inspired by: Ciberusps - playcanvas-typescript-example.
I also created some quality of life scripts for automatic branch id checking, downloading builds, and starting local server with said build.

Just follow the steps in readme file.

In my own setup, I also use my own fork of playcanvas-sync that allows to skip branch check, so it works inside CI. I would recommend doing the same.

I hope it helps.

@attribute({ type: 'entity' }) public someEntity?: pc.Entity;

why i can’t set any to this attribute?
when click and select an entity, nothing happened.

This is due an issue with the Editor that we will be fixing soon:

Nice decorators. What license does your project hold? Do the decorators written by you? Can I take them?

I added license file. Feel free to use what you need.

I load the tween.js in the editor, and i can use it


but in typescript and vscode , i can’t call the tween function.
how to resovle it?

If you added tween.js to playcanvas project, then typescript has no information about type definitions.
You can install this library through npm: playcanvas-tween, or add your own definition.
Alternatively, you can just add this comment before lines where you use tween:

//@ts-expect-error Using external library

tween is not declared in pc.Entity. You have to do it yourself.

You can try to add something like this

import { Tween } from "tween.js"
interface TweenedEntity extends pc.Entity {
    tween: Tween

class YourScriptComponent extends pc.ScriptType {
    public entity: TweenedEntity


And run the next command in your repo npm i @tweenjs/tween.js @types/tweenjs (install types and optionally tween.js).

NOTE: I have not tested it.

maybe i should change the lib to typescript…

I experimented with the decorators. The main issue with them is they bring into a bundle another instance
of Playcanvas

Tried to fix it in my template.

1 Like

I use Playcanvas in all my scripts this way, and then get rid of it during build step.
Here: /gulp.js#L28

If you have different build process, then it will add pc to your bundle.

1 Like