Script inheritance

So I did some research and finally understood how to get inheritance to work in Javascript :sweat_smile: It’s great, since it works for whole scripts too:

/////////////////////////////////// BASE CLASS

var Lipsync_Base = pc.createScript('lipsync_Base');

Lipsync_Base.prototype.playVoiceLine = function(key) {
    // Do some generic stuff...
    this.setMouthShape();
}

Lipsync_Base.prototype.setMouthShape = function(key) {
    console.error('Not implemented');
}

/////////////////////////////////// SPECIFIC CLASS

var Lipsync_Mesh = pc.createScript('lipsync_Mesh');

Lipsync_Mesh.prototype = Object.create(Lipsync_Base.prototype);
Lipsync_Mesh.prototype.constructor = Lipsync_Mesh;

Lipsync_Mesh.prototype.setMouthShape = function(key) {
    // Do some specific stuff for Meshes
}

Now the only thing I don’t like is that all the descendant classes need to be in the same file as it’s ancestor. Files tend to get bloated easily this way. But if I don’t do that, the editor can’t parse the scripts.
I tried changing the order in which the files should get loaded, but that only helps to resolve circular dependencies and such at the launch, not in the editor.

Is there a way to import a specific file at the top of another script, so it knows all about the ancestor?

3 Likes

Well done, thanks for sharing that.

Regarding your question, I don’t think that is possible or if there are plans to support it (@will).

Here is a related question: