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

0 favourites
  • 5 posts
From the Asset Store
A collection of various zombie characters sprites for creating a 2D platformer or sidescroller game
  • 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.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • 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.

  • 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();
    
  • WackyToaster

    half a year later... Yes, I think so.

    Though that still means you have to subclass every object type you have needs to add the functionality to... which is repetition I hate, especially when it also means you have to remember to do that when making changes to the project...

    I am currently solving in another way. During an objects initialization, I have a "onCreate" event for the family I need my behavior on. In that event, I call a javascript function that stores a reference to the created object, and creates a new instance of a custom javascript class (I'll call it a component now).

    The components I add to objects do various things, such as manage buffs/debuffs and character behavior states (like walking, running, rolling, climbing, etc)...

    In this way, I have wrapped up it up in JS for performance sake, but now when I create a new objectType and add it to the family of consequence, I don't have to do anything else, or remember to subclass it. It's a little more fool proof and less prone to programer error in the future... I also made it so the system interacts with editor side instance variables, and linked dictionaries for dynamic data, so I also don't need to worry about interfacing with it via JS if I don't want to.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)