Inheritence and OOP with PlayCanvas


#1

Hello everyone,

Here I have a base class / script

var DialogueBase = pc.createScript('dialogueBase');

DialogueBase.attributes.add('portrait', {
    type:'entity',
    title:'Portrait'
});

DialogueBase.attributes.add('landscape', {
    type:'entity',
    title:'Landscape'
});

and many child classes, for example:

var QuickNavigationDialogue = pc.createScript('quickNavigationDialogue');

QuickNavigationDialogue.attributes.add('portrait', {
    type:'entity',
    title:'Portrait'
});

QuickNavigationDialogue.attributes.add('landscape', {
    type:'entity',
    title:'Landscape'
});

and

var ProductDialogue = pc.createScript('productDialogue');

ProductDialogue.attributes.add('portrait', {
    type:'entity',
    title:'Portrait'
});

ProductDialogue.attributes.add('landscape', {
    type:'entity',
    title:'Landscape'
});

so I want both “ProductDialogue” and “QuickNavigationDialogue” to inherit from “DialogueBase”
such that I remove the same attributes mentioned in all classes.

I found this post:


which has some useful info, but still can’t figure out what to do.
Mainly the syntax is confusing, we don’t have a constructor.
Any hints or examples please


#2

PlayCanvas is more of a component based design: http://gameprogrammingpatterns.com/component.html

So rather than having a ‘base’, think of it as a collection of behaviours.

In this case, I would have the Dialogue as it’s own component with all the references and then have ProductDialogue keep a reference to the Dialogue internally. This will allow you to use common behaviour from the Dialogue while allowing for customised behaviour.

The orbit camera does something like this where the input controllers keep a reference to the Orbit Camera script: https://developer.playcanvas.com/en/tutorials/orbit-camera/