Change to subclassing in r362 - question

0 favourites
  • 5 posts
  • I just noticed that subclassing changed at some point, probably here construct.net/en/make-games/releases/beta/r362

    Ensure InstanceType namespace exists at runtime

    Now subclassing appears to reference the specific object the class is supposed to extend, as in this case the Player instance.

    class PlayerInstance extends globalThis.InstanceType.Player
    

    However it used to be like this (if the player is a sprite)

    class PlayerInstance extends ISpriteInstance
    

    This code seems to still work, is it fine to keep using it like that? I like being able to extend a generic sprite instead of the specific sprite of the player object. Or am I just misunderstanding something here?

    The reason is I do tend to further subclass my subclasses, so both the player and enemies might derive from a base class with shared functionality, e.g.

    	class Entity extends ISpriteInstance...
    	class Player extends Entity...
    	class Enemy extends Entity...
    
  • The change was made for TypeScript support. When using TypeScript, Construct generates TypeScript definitions for every object class including types for instance variables, behaviors and effects. As these are all different for every object class, it has to generate a separate base class for every type of object class, hence InstanceType.Player rather than a generic ISpriteInstance. However InstanceType.Player only has type definitions for TypeScript, and (assuming it's a Sprite object) itself derives from ISpriteInstance. So if you're using JavaScript there's basically no difference. The main reason to change it is to keep everything consistent, so if you change to TypeScript or back again you can continue to write code the same way. If it would be a pain to update all your code though, you can just leave it and it should keep working the same.

  • Thanks! I should try out typescript at some point. But would that mean in typescript I can't have a base class with subclasses like I used to? Or is there a different way to achieve what I'm trying to do?

  • That code will still work in TypeScript, but you will lose access to instance variables, behaviors and effects unique to the object classes, as those come from the type definitions on the class in the InstanceType namespace.

    It's still possible to have it work though - essentially this comes down to multiple inheritance (e.g. you want PlayerInstance to derive from both InstanceType.Player and Entity), and if you look up mixins you should find a couple of ways to approach that. Where possible I'd recommend composition over inheritence though - i.e. make an Entity a property of the class rather than a base class.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • I see. I have used both mixins and composition before. I found mixins to be kinda weird and awkward though, so composition is fine. Thanks :)

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