Inheritance and functions, will this be supported?

Discussion and feedback on Construct 2

Post » Tue Feb 28, 2012 2:09 am

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!
B
19
S
5
G
4
Posts: 85
Reputation: 4,925

Post » Tue Feb 28, 2012 2:54 am

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.
B
35
S
8
G
8
Posts: 532
Reputation: 6,868

Post » Tue Feb 28, 2012 3:42 am

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: http://www.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... :))
B
19
S
5
G
4
Posts: 85
Reputation: 4,925

Post » Tue Feb 28, 2012 4:08 am

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?
Moderator
B
87
S
32
G
33
Posts: 3,005
Reputation: 27,397

Post » Tue Feb 28, 2012 4:23 am

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.
B
19
S
5
G
4
Posts: 85
Reputation: 4,925

Post » Tue Feb 28, 2012 4:48 am

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.
Scirra Employee
B
129
S
45
G
15
Posts: 705
Reputation: 15,413

Post » Tue Feb 28, 2012 4:52 am

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. SullyTheStrange2012-02-28 04:53:05
B
20
S
9
G
6
Posts: 607
Reputation: 6,112

Post » Tue Feb 28, 2012 5:15 am

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'dArima2012-02-28 05:16:01
Moderator
B
87
S
32
G
33
Posts: 3,005
Reputation: 27,397

Post » Tue Feb 28, 2012 10:39 am

@valkyriegames

"Constructors and destructors" could be implemented by plugin. Do you need one?
B
97
S
22
G
177
Posts: 4,111
Reputation: 103,525

Post » Tue Feb 28, 2012 11:18 am

[QUOTE=rexrainbow] @valkyriegames

"Constructors and destructors" could be implemented by behavior. Do you need one?[/QUOTE]
There, fixed it for you.
B
35
S
8
G
8
Posts: 532
Reputation: 6,868

Next

Return to Construct 2 General

Who is online

Users browsing this forum: Artpunk, R0J0hound and 10 guests