Here is a way to dynamically create instances

Get help using Construct 2

Post » Thu Dec 26, 2013 7:15 am

After doing research in the forums I see that it is a common question...

"How do I create an instance of an object dynamically (by text name) and then get a reference to that instance to use it?"

Normally in computer languages this can be done synchronously (create it and immediately use it).

However this can also be done asynchronously (start the creation now...and later it will be finished).

<strike>
Construct 2 uses the asynchronous method.
This however causes complexity that seems out of place in Construct 2.
</strike>

In the tutorial there is an example of doing the same thing synchronously as well.

Many people have struggled with how to get a handle on this.

Plugins have been created to try to help.

Here is a tutorial that contains a capx (https://www.scirra.com/tutorials/788/asynchronous-callback-object-creation) that shows a method to handle this with what is known in the software development world as "Callbacks".

It is implemented entirely with Construct 2 Personal / Business features.

The way it works is described in detailed comments in the capx. Basically you create an object by calling a function called "New" and pass it some parameters via Dictionary objects that includes the text name of the object type, the name of the Callback function you want called after the object has been created, and any parameters you want passed to the Callback function.

The Callback function gets the UID of the new object (as Function.Param(0)) and all the additional parameters you specified (as Function.Param(1)).

With this you can do a Family > Pick instance with UID or Pick an instance of the specific type and then just do all the actions you would have normally done.
ggibson12013-12-29 05:03:29
B
21
S
5
Posts: 196
Reputation: 1,974

Post » Fri Dec 27, 2013 12:43 pm

Nice, make a tutorial!
B
46
S
16
G
79
Posts: 2,170
Reputation: 46,857

Post » Fri Dec 27, 2013 1:44 pm

Yes, it might get lost as a mere forum post.
B
77
S
28
G
32
Posts: 481
Reputation: 19,763

Post » Fri Dec 27, 2013 8:21 pm

Good idea you two...


Tutorial:

(https://www.scirra.com/tutorials/788/asynchronous-callback-object-creation)ggibson12013-12-27 20:22:41
B
21
S
5
Posts: 196
Reputation: 1,974

Post » Fri Dec 27, 2013 8:53 pm

I'm not sure I understand the necessity of this, since when you create a new instance of an object, it is automatically picked for the following actions within the same event. No need for a callback function to modify the new instance, it's available immediatly. Maybe I'm missing something, I'm a former software developper myself and I understand what a constructor is, but I don't see the point of making thing more complicated than they are when the point of construct is to make them simpler.
B
12
S
5
G
2
Posts: 132
Reputation: 2,627

Post » Fri Dec 27, 2013 8:59 pm

And if the name of the type you want to create is stored as a string of text in an Instance Variable or read in from JSON?

What you are talking about only works if you HARD CODE the type into the event... look through the forums and you will see many people asking how to do this exact thing.
B
21
S
5
Posts: 196
Reputation: 1,974

Post » Fri Dec 27, 2013 10:29 pm

well this is true, you cannot create an object dynamically by passing a string variable, and I agree it's too bad.
But in the end the solution you are offering is more or less hardcode too, it's a big SWITCH that handles each case (sprite 1, sprite 2, sprite 3 and so on). Yes you can now create new instance by just passing a string variable, as long as it's handled by your system. In order to introduce a new type of sprite, you still have to write the specific code for it, in two different places.
So in the end, it's pretty much the same as writting a constructor function for each sprite type, where in one place you create the instance and set its properties. Then you just call the right function when needed.
Instead, you added this asynchronous layer, which in my opinion is unecessary and only adds complexity.OlivierC2013-12-27 22:31:19
B
12
S
5
G
2
Posts: 132
Reputation: 2,627

Post » Fri Dec 27, 2013 11:14 pm

It definitely is not the same thing. Instead of putting switches all over your code and writing the same code over and over with slight variations with this method you have a single switch which acts as the object table you would normally have made for you automatically when you compile your application.

To add a new type you copy and paste one of the other switch cases (2 lines) and change the type to the new type... that is it... you do not need to do anything specific to that type else where.

From then on you can refer to the instance via Family (like is shown in the capx) ... at that point you have decoupled your type from your code so you can start making abstraction layers and reusable functions.

I really cannot teach 30 years of coding in a forum post, but if you learn OOP programming and go through the experience of making a few large applications that you have to maintain, upgrade, and reuse code across project to avoid bugs and wasted time then you will see the benefits of decoupling your types from your logic... this is why in OOP languages like C++ and C# you have things like Interfaces and Inheritance trees... It is an old problem that professional software engineers deal with all the time... it is something that new developers that are just working on small project never have to consider... this is why I added the tutorial to the "advanced" category ... it is not for new developers or hobbyists.
B
21
S
5
Posts: 196
Reputation: 1,974

Post » Fri Dec 27, 2013 11:28 pm

Another point I would like to make for those interested is how nice the idea of Family feature is in Construct 2 once you have decoupled your type from your logic... This gives you the equivalent of "multiple inheritance" in the OOP world. Many languages do not support multiple inheritance, but get around that with the ability to define multiple interfaces on your class and the ability to inherit from one parent class.

You can have a single object type inherit from several different Families and then "cast" it from one family type to another to reuse the functionality that is available in each family. In the capx you can see an example of this where one of the objects inherits the flash behavior when it is treated as one kind of Family type and then is "cast" to the fade family type and is faded... all without knowing which specific sprite type was being referenced at that moment.

Lots of opportunity there for code reuse.
B
21
S
5
Posts: 196
Reputation: 1,974

Post » Sat Dec 28, 2013 7:18 am

It's all probably very usefull, although it seems overcomplicated.

Just a small question though.
You state Construct2 has asynchronous callback, or something like that, but in a creation event when you use subevents automatically the just created instance is referenced, wouldn't that be called synchronous?

I told my dentist I had trouble with my teeth and asked her to fix it without looking in my mouth..
B
54
S
16
G
8
Posts: 6,160
Reputation: 19,775

Next

Return to How do I....?

Who is online

Users browsing this forum: brunopalermo, dand, oosyrag, tinhofiel and 25 guests