Object constructor question(s)

Discussion and feedback on Construct 2

Post » Fri May 31, 2013 1:29 am

Object-oriented programming utilizes "constructors" which at its basic level allow the programmer to instantiate a bunch of member (instance) variables upon creating the object.

Correct me if I'm wrong, but it appears Construct 2 doesn't have that built in.

For this simple example where we just set the position and scale of Ball on creation, are these the current alternatives:

Make the constructor simply just an On created trigger:

Problem: you can't elegantly send values to the constructor. This would be desired if the place where you create the object has contextual information from some other picked object, etc.

OR

Make a function as a constructor replacement, and this is where it gets confusing to me:

Why does this work? Notice the second action in the Create_Ball function. Shouldn't this pick all instances of Ball, since the event doesn't pick any instances of Ball? Somehow it picks the most recently created Ball, which works here. However, it doesn't seem right, so I don't want to rely on it.

I can't put an On Created trigger inside the function definition because they are both triggers.

Is there something I'm missing about this?

Thanks!CannedEssence2013-05-31 01:34:08
B
11
S
2
Posts: 54
Reputation: 805

Post » Fri May 31, 2013 7:02 am

[QUOTE=CannedEssence]

Make a function as a constructor replacement, and this is where it gets confusing to me:

Why does this work? Notice the second action in the Create_Ball function. Shouldn't this pick all instances of Ball, since the event doesn't pick any instances of Ball? Somehow it picks the most recently created Ball, which works here. However, it doesn't seem right, so I don't want to rely on it.

I can't put an On Created trigger inside the function definition because they are both triggers.

Is there something I'm missing about this?

Thanks![/QUOTE]

The create action picks the created instance, so you are only referring to that instance. So you could get rid both the function and on created, then do this:

create ball
set ball scale to 2

It would work just as well :) But yea, it's not really ideal to my workflow at some times either.vee412013-05-31 07:05:08
B
24
S
9
G
7
Posts: 756
Reputation: 7,292

Post » Fri May 31, 2013 10:01 am

Ashley specifically set out to create an event-based system so that non-programmers would be able to create games more easily.

The system is 'as it is' and I for one would not want it to become more programmer-based. The idea is you learn the system that is available, and not expect something to work as you think it should.

There are already plenty of systems out there that cater for the more programmer-minded individual.
If your vision so exceeds your ability, then look to something closer.
Moderator
B
129
S
29
G
78
Posts: 5,202
Reputation: 54,811

Post » Fri May 31, 2013 12:01 pm

Honestly as far as game logic goes, the need to perform some operation on all instances after the creation of one instance happens much less than the need to do something to the specific object instance when it is created.
Image ImageImage
B
167
S
49
G
155
Posts: 8,112
Reputation: 100,770

Post » Fri May 31, 2013 2:37 pm

You can have it both ways. 'Create object' or 'Spawn object' pick the instance, so you can initialise just that object in a constructor-like way. If you *do* want to do something to *all* instances after creating another instance (which sounds unusual but is possible), just put 'pick all' in a subevent and then you can affect all instances from there.
Scirra Founder
B
384
S
227
G
86
Posts: 24,144
Reputation: 190,816

Post » Fri May 31, 2013 5:45 pm

That answers my questions, thank you all!

@newt
No, I agree that referencing the instance you just created is more common than referencing all instances of the just-created instance. I prefer that functionality (it makes my second example constructor work =P). I just found it fishy that it worked, because as far as I knew, no action (just events) picked instances. I was just afraid that it was incidentally picking the most recent instance because it happened to be at the "top" of the SOL or something.
Good to know the create object action also picks the instance.

@zenox98
I agree with the essence of what you are saying: Construct 2 is designed to be accessible to non-programmers, shirking away lots of the scary syntax, boilerplate, etc, of code. However, there are reasons for why object-oriented languages were designed the way they were, it wasn't just job security.
Being able to clean up the on layout start event by logically grouping events into functions is smart. Notice how the Ghost Shooter example start layout event is a bit unruly? Having a constructor function to handle instantiating an object and its properties is good organization, not "programmer-minded."
That's the kind of stuff you see in the popular thread about best event practices in Construct 2: http://www.scirra.com/forum/programming-best-and-worst-practices_topic63136.htmlCannedEssence2013-05-31 17:48:04
B
11
S
2
Posts: 54
Reputation: 805

Post » Mon Nov 04, 2013 5:27 am

[QUOTE=Ashley] You can have it both ways. 'Create object' or 'Spawn object' pick the instance, so you can initialise just that object in a constructor-like way. If you *do* want to do something to *all* instances after creating another instance (which sounds unusual but is possible), just put 'pick all' in a subevent and then you can affect all instances from there.[/QUOTE]

This should probably make it into the manual (in the section where System Create Object is described)
B
3
Posts: 2
Reputation: 204


Return to Construct 2 General

Who is online

Users browsing this forum: No registered users and 7 guests