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

Example of using your own custom types (classes) in your own pc.ScriptTypes #8

Closed yarpskendaya closed 1 year ago

yarpskendaya commented 1 year ago

This repo has been a great help, but I'm really struggling to get a good class hierarchy going where I can unit test my own class definitions, that I then use within pc.ScriptTypes. The file that you define a script in, like: class Foo extends pc.ScriptType requires to be a file that is not a module, because otherwise it complains:

'pc' refers to a UMD global, but the current file is a module. Consider adding an import instead.

This makes it impossible to import modules into this file, which stumps me, because I don't know how to use classes in other files without the import statement.

An example within this repo would be of great use! Any other pointers appreciated!

yaustar commented 1 year ago

This repo is meant to be for use with the Editor which doesn't use modules. So all class definitions are in global scope, there's no need to import them. You can see ES6 classes used in this Editor project: https://playcanvas.com/project/950571/overview/fps-pit

If you want an example of using the Engine without the Editor and with modules, the Model Viewer repo could help here; https://github.com/playcanvas/model-viewer

yarpskendaya commented 1 year ago

I got it working in playcanvas, like you said without modules, thanks for the clarification. Now I'm running into issues where the unit tests cannot find these global class definitions, but that's up to me to figure out.