Hi everyone,
with the introduction of ESM scripts and the recommendation of using those in new projects. I have a few questions regarding extending default playcanvas behaviour.
- Lets start with the tweening library:
It seems like the preferred way of using it now is to import tweenEntity and supply it with the entity I want to tween and tween itself. (I didn’t look into tweening a custom object yet, not sure how to do that)
import { tweenEntity, SineOut } from './tween.mjs'
tweenEntity(entity, entity.getLocalPosition()).to({x: 10}, 1.0, SineOut);
There is still the option to augment Entity and AppBase with the tween function by calling addTweenExtensions and use it the old way.
import { addTweenExtensions, SineOut } from './tween.mjs'
import * as pc from 'playcanvas'
// Adds .tween() to Entity and Application
addTweenExtensions(pc);
entity.tween(entity.getLocalPosition()).to({x: 10}, 1.0, SineOut);
Altough this works, the editor will be unhappy and will throw an error, because the tween function isn’t defined at compile time.
- pc.extend
I have a custom SceneManager which handles scene switching/loading. I took inspiration from the old TweenManager setup and set it up the same way.
What is the recommended way to update it to the new system? I guess do it like the new tween system?
- Extending prototypes
I also have a few extensions on different playcanvas classes. For example helper functions on the GraphNode:
(function () {
// Search the graph node and all of its ascendants for the first node that has a script component with the specified type.
pc.GraphNode.prototype.findScriptInParent = function(type){
var entity = this.findOneInParent(function (node) {
return node.script && node.script[type];
});
return entity && entity.script[type];
};
})();
This will continue to work but will also create errors in the editor as those functions aren’t defined at compile time.
I guess the correct way would be to define custom util/helper esm and use those instead of augmenting prototypes?
I’d love to hear suggestions from all of you on how to best approach this.