Throwing the towel and asking for help been trying to setup some tests for one of my classes as a proof of concept. but can seem to get over the initialization step.
Context
Want to add some CI to my project without having to ditch the editor. So i use the api rest tools to download the project. then with mocha i init pc globally and create and app using placanvas mock. then in test file:
can’t seem to be able to get attributes to populate, get a undefined error when accessing serverCalculated no matter what i do. The script is correctly registered in the app and the attribute is there if i do EntityRotation.attributes.get(‘serverCalculated’). Any and all help is greatly appreciated.
Hey yaustar, thanks for reply. thats how i originally had it, added the method call () out of desperation after doing a inspection of instance. Sadly neither of them worked. O i also tried passing the attributes as args to new, but no luck maybe I have the wrong schema for attrs args. i’ll add the trace for more info.
SideNote: In editor there is deprecation notice to not extend from pc.ScriptType. But extending from pc.Script doesn’t quite work. wondering what should be the drop in replacement here?
Side SideNote: If i could use export statement in the editor it would have made setting up tests so much easier.
Trace
1) EntityRotation
should initialize with serverCalculated = false:
TypeError: Cannot read properties of undefined (reading 'serverCalculated')
at EntityRotation.get (file://playcanvas_server/node_modules/playc
anvas/build/playcanvas/src/framework/script/script-attributes.js:166:29)
at EntityRotation.initialize (file:///playcanvas_server/public/bsm_
build/files/assets/189372713/1/entityRotation.js:9:15)
at Context.<anonymous> (file://playcanvas_server/pc_tests/entityRo
tation.test.js:29:14)
at process.processImmediate (node:internal/timers:483:21)
2) EntityRotation
should initialize with serverCalculated = true:
TypeError: Cannot read properties of undefined (reading 'serverCalculated')
at EntityRotation.get (file:///playcanvas_server/node_modules/playc
anvas/build/playcanvas/src/framework/script/script-attributes.js:166:29)
at EntityRotation.initialize (file://playcanvas_server/public/bsm_
build/files/assets/189372713/1/entityRotation.js:9:15)
at ScriptComponent._scriptMethod (file:///playcanvas_server/node_mo
dules/playcanvas/build/playcanvas/src/framework/components/script/component.js:180:17)
at ScriptComponent.create (file://playcanvas_server/node_modules/p
laycanvas/build/playcanvas/src/framework/components/script/component.js:354:13)
at Context.<anonymous> (file://playcanvas_server/pc_tests/entityRo
tation.test.js:39:42)
at process.processImmediate (node:internal/timers:483:21)
The source of my pain seems to always start in scrip-attributes.js no matter my changes. It like _attributes is always undefined.
You were using PlayCanvas 2.0.0 which is a major breaking change version and has updated Script Type system and is why you were getting the errors about undefined attributes. The Editor is on 1.73.4. See Engine 2.0.0 (Root ticket) · Issue #6666 · playcanvas/engine · GitHub for more details. TLDR for now, you should be using PlayCanvas v1.
When you create a script type on a script component, if it’s set to be enabled, it will call initialize immediately.
On a separate note, the PlayCanvas team are working on getting ES6 module support in the Editor which will help with this. They are currently in alpha testing with this in the Editor and Engine v2