Inheritance and functions, will this be supported?

  • Hi there,

    This is a question directed to the developers. As I come from a Java background, I am used to being able to create inheritances for object types. I was reading the fabulous manual and noticed that right now, this functionality is not available in C2. Is there any likelihood that this will be implemented in the future? Being able to create inheritance trees will speed up the game development process greatly because this will allow me to create a parent object type of Enemy which has associated events which say perhaps checks for collisions. I can then simply create TrollEnemy and OgreEnemy as child classes which would automatically also inherit those events without having to write them again for each.

    Also, there's no chance of Constructors/Destructors or Functions being planned for development would there? They would surely improve the efficiency of game development with the wonderful C2.

    Having the functionality above will allow much larger games to be created much more easily, and you can make it available only for the paid version :)

    Thanks for your time!

  • Probably not with that name and/or the classic java inheritance scheme, but there is something similar sort of in place.

    We need functions (with callbacks) in order to get constructors/destructors (those are probably planned, callbacks being relatively easy to implement).

    For inheritance, we need families to group similar objects (Troll and Ogre are both part of the ENEMY family, for instance) - families already work.

    We also need a way to nest families, in order to get multiple inheritance working (for instance, ENEMY could be a subfamily of NPC, which itself would be a subfamily of ACTOR. They are also members of the MELEE family, which is a subfamily of the ATTACK family). I don't know what Ashley's thoughts on the subject are, but this would greatly improve code reusability.

    More important than all of that, IMO, is object grouping (called "containers", I believe), since it allows complex objects.

  • Thanks Fimbul, that certainly pointed me in the right direction!

    I had a look at families and that's exactly what I was looking for. What did you mean by object groupings? I found this topic which directed me back to families: scirra.com/forum/object-groups_topic45320.html

    I agree, some form of multi-inheritance would be handy. If the dev's don't want something as heavy handed as that, they could consider something more lightweight like java interfaces.

    By the way, does nesting of families (inheritance trees) work currently? (eg WorldObject -> Unit -> Player).

    Looks like I'll be needing my standard license soon so I can use those features! (Or if I could just conveniently win the Rotary Competition... :))

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • You basically can have multi-inheritance by simply having the object in multiple families. No nesting trees needed.

    Containers are for multi-part objects - like a tank sprite with a turret sprite, so when the tank is picked, the correct turret is picked as well.

    Functions are planned, though there is an unofficial function plugin in the plugins forum.

    What are constructors/destructors?

  • Thanks Arima! That cleared some things up a bit. So are family "trees" currently possible as well if there is no nesting?

    Constructors and destructors are functions that belong to a particular object class (in this case, an object type or family) that are called when an object is either created or destroyed respectively. This is convenient in that, for example, you can create a constructor for randomly placing a newly spawned enemy on the map, without having to specify the place to spawn the enemy each time you create an enemy. This also keeps the "code" neat by keeping the events together with the object types, rather than floating around on the events page.

  • Yes, it's possible to have the same functionality as a family tree, but you have to add each object individually to multiple families, instead of adding a family to another one (that would consequently add all the belonging objects automatically).

    According to your explanation about constructors and destructors, they seem like an equivalent to the conditions "On object creation" and "On object destruction". There's no such thing in C2 yet, but I must say that would be very useful indeed.

  • There's a way to fake constructors and destructors, which won't look as nice but works just as well. If you spawn an object with the variable "Created", initially zero, just test for "Object - Created = 0", and do whatever you want to do for the constructor, and at the end of it set Created to 1. As long as you never touch that variable again, those events will only trigger once when it's created.

    As for destructors, you could take the same variable and set it to 2 when you want to destroy it. Test for "Object - Created = 2", and do whatever you need for the destructor, and then destroy it at the end.

    Whether or not a certain feature is explicitly included, there's almost always a way to get the same functionality. <img src="smileys/smiley1.gif" border="0" align="middle" />

  • Well, it's not exactly a tree, more like a list, but would behave the same way. The sprite would be controllable through every family it's a part of. So if the hierarchy is something like this:

    Top tier - items

    Second tier - collectables/powerups/etc

    Third tier, branching from collectables - coins/stars

    Objects in coins - Coin 1, coin 2

    You would get the same functionality by simply putting the coin 1 and 2 sprites into the items, collectables and coins families. Does that make sense?

    As for constructors and destructors, construct doesn't have those as you're used to them. Instead, it works more like this:

    • sprite: create object sprite at set position to random(x), random(y)
    • sprite: set variable to 1

    Upon doing this the newly created sprite is the only instance of sprite selected, so only the created instance's variable is set. Any actions that are after that creation action in the same event or sub events of that event will affect it. I haven't used the unofficial function plug in yet, but in construct classic you can sort of simulate a "on created" event with a function:

    • sprite: create object sprite at wherever
    • function object: call function "sprite created" (remember picked objects)

    on function "sprite created"

    • sprite: set position to random(x), random(y)
    • sprite: set variable to 1

    Or you could simulate it with a variable instead:

    • sprite: create object sprite at wherever
    • sprite: set variable 'created' to 1

    sprite: is variable 'created' = 1

    • sprite: set position to random(x), random(y)

    Edit: Ninja'd

  • valkyriegames

    "Constructors and destructors" could be implemented by plugin. Do you need one?

  • valkyriegames

    "Constructors and destructors" could be implemented by behavior. Do you need one?

    There, fixed it for you.

  • valkyriegames

    @Fimbul

    SpriteExt

    I put it in my small behavior plugin -- sprite extension.

    Some sprite actions use combo box to select input value, it makes value assign a little difficult from a variable. So I make some direct assign actions.

    Well, I add constructor and destructor into condition.

    test capx

    <img src="http://i1081.photobucket.com/albums/j352/rexrainbow1/screen2-7.png" border="0">

  • I know I said this elsewhere <img src="smileys/smiley1.gif" border="0" align="middle" /> - family behaviors would greatly simplify things. I have to do all sorts of workarounds to emulate this.

    Another thing: have a way to tell the object type from a family object. For example, at some point I picked an Enemy object. I want to have two subevents, one for Ogre and the other for Troll. Right now I added a variable "objectType" and I filter by it, but it would be nice to have a built-in property "typeof" or something.

  • Thanks for all the helpful comments guys!

    Yes, it's possible to have the same functionality as a family tree, but you have to add each object individually to multiple families, instead of adding a family to another one (that would consequently add all the belonging objects automatically).

    It's a pity they don't allow adding a family to another - that would make things quite convenient! (Maybe the devs could think about doing it *hint hint* :D)

    @SullyTheStrange and Arima: The suggestions about simulating a constructor and destructor are fair alternatives, but I was hoping for a "language supported" way of doing it! Probably the most important thing that constructors and destructors allow is nice groupings of functionality to object types and makes the "code" much simpler and more readable.

    valkyriegames

    "Constructors and destructors" could be implemented by plugin. Do you need one?

    Would the plugin allow the "code" to be grouped together with the objects? If so, that would certainly be very useful!

    > valkyriegames

    >

    > "Constructors and destructors" could be implemented by behavior. Do you need one?

    There, fixed it for you.

    lol Fimbul.

  • rexrainbow

    Nice! I hadn't realized that you made a plugin already. It looks great, I'm going to check it out now thanks! :D

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