Inheritance (programming)

Hi there,

I’m also new with PlayCanvas but I’m very familiar with Javascript

Javascript is a dynamic language (i.e you can add more fields/props into an object even after you have new-ed it) so there are many ways to do “inheritance” with Javascript

If you notice that when a new script is added with default template, you have YourClass.prototype, this is sort of extend/inherit the YourClass class. Prototype just adds more functionality to an existing class

Virtuals and overrides are just basically recreate the parent functions in children, pretty much you can search for the net for it, here is one quite good on stackoverflow

-> http://stackoverflow.com/questions/8031467/override-a-base-class-function

But with PlayCanvas, I think we can make these work in PlayCanvas way i.e:

  1. Create a bunch of “reusable” components such as FollowMouse.js, SelfRotate.js, MoveToward.js etc. you can call them as base classes if you want
  2. Attached them to the same working entity, then on the main script, we just go this.entity.script.FollowMouse.DoStuff() for example, where DoStuff() is an “inherited” function from the “base” FollowMouse class

Hope these help!

Cheers,
H

2 Likes