Is it possibe to subclass families? Or add common functionality to many objecttypes

0 favourites
  • 4 posts
From the Asset Store
Make your horror game even more Scary with sound effects! :)
  • Hey all,

    I have no issue sub classing an object type, but it seems the same thing doesn't work for families, unless I am doing something wrong.

    I want to be able to define a common behavior (in this case, some custom physics stuff), and apply it to many objects.

    Currently, as I understand it, I can only subclass a particular object:

    //In familyExtend.js
    export default class TestInstance extends globalThis.InstanceType.Tester
    {
    	constructor()
    	{
    		super();
    		this._balls = 2;
    	}
    }
    
    //In familyExtend2.js
    export default class TestInstance2 extends globalThis.InstanceType.qCollider
    {
    	constructor()
    	{
    		super();
    		
    		//add properties
    		this._balls = 2;
    	}
    }
    
    //In main.js
    import TestInstance from "./familyExtend.js";
    import TestInstance2 from "./familyExtend2.js";
    
    runOnStartup(async runtime =>
    {
    	//extend classes like so
    	runtime.objects.Tester.setInstanceClass(TestInstance);
    	runtime.objects.qCollider.setInstanceClass(TestInstance2);
    	//...
    	runtime.addEventListener("beforeprojectstart", () => OnBeforeProjectStart(runtime));
    });
    

    In the above case, Tester is a family, and qCollider is a object. I can subclass the object, but not the family. I imagine this is expected -...

    But what I don't understand then, is how do I add this functionality to every object in my game, short of redefining a subclass for every object type?

    I understand I am essentially wanting functionality that multiple object types share, but adding it on as a subclass is backwards. Like defining rats and bats and then creating subclasses for mammals.

  • I think this amounts to multiple inheritence: you want classes to inherit from multiple locations - the Construct provided base class, and a separately written base class also inherited by other classes. If you search for JavaScript mixins you should find a few ways to do that.

    Another simpler way (and one I tend to prefer these days) is to use composition over inheritence - create a complex custom class that you want to be shared, and just add it as a property on multiple other classes.

  • Thanks Ashley ! I think you mentioned using mixins in the past and I forgot about that feature.

    Incidentally I was looking at the latter approach you mentioned. Thanks for the insight.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Another simpler way (and one I tend to prefer these days) is to use composition over inheritence - create a complex custom class that you want to be shared, and just add it as a property on multiple other classes.

    Just for clarification, you mean doing it like this?

    	//inside subclass
    this.plugin = new Plugin();
    
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)