Asynchronous Callback Object Creation

Attached Files

The following files have been attached to this tutorial:

.capx

newobjectswithcallback.capx

Download now 238.46 KB
.capx

createspritebynamesync.capx

Download now 197.14 KB

Stats

1,661 visits, 2,563 views

Translations

This tutorial hasn't been translated.

Tools

One thing you will be doing a lot of is creating objects, modifying those objects in some way and then setting them lose into the game world.

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

See this capx for an example of the Sync method: "CreateSpriteByNameSync.capx"

And here is a screen shot of the events from that capx:

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

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.

Then "later" (usually the next tick) you will get "called back" when your Callback function gets executed.

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.

So the workflow goes like this...

1) You decide you want to create a new sprite on the screen (e.g. a bullet)

2) You also want to do something with this sprite right after it gets created (e.g. set Instance Variables on it).

3) So you create a Function that will act as the callback and does whatever you want to do with the sprite after it is created (e.g. set Instance Variables on it). This Function needs to expect to get the UID of the new object as Param(0) and a string of JSON text that you load into a Dictionary object which you can see in the capx is called objFunctionParams. objFunctionParams will contain all the values you passed to the New function in the Dictionary object called objCallbackParams.

4) In the event where you want to create a new object load a dictionary object with the parameters the New function expects (Type, Layer, X, Y, CallbackFunction). Then load another dictionary with the params you want to get passed to the callback Function.

5) Call the New function.

6) Wait one tick (or until Construct 2 engine starts executing the Event Sheet that contains the Async Callback Processor events).

7) Now the callback Function will execute.

How to get started?

Open the capx and your project. Then in your project recreate the dictionary objects (objCallbackParams, objFunctionParams, objNewParams), the array object (objQueuedCallbacks), and create three sprites called (Sprite1, Sprite2, Sprite3) that you can see in the capx included with this tutorial. Also make sure you have added the Function object to your project.

Now you can copy the events from the tutorial capx in the Event Sheet called "CoreSystem" into your project. These events are in two Groups labeled

"Async Callback Processor"

and

"New Function".

Finally in the New function (you can find it in these events you just copied over) you make a copy of one of the events for Sprite1 or Sprite2 or Sprite3 and replace those references (e.g. Sprite3) with any sprite in your project you want to be able to create with the New function.

For example you can see in the included capx that the New function is setup to be able to create three different types of objects (Sprite1, Sprite2, Sprite3).

Even more technical info for those who are interested or are software developers themselves:

The Group in the events that get copied over to your project labeled "Async Callback Processor" contains an event that gets called each tick. Each call to create a new object with the New function queues the callback request into the array called "objQueuedCallbacks". So each tick the Async Callback Processor will loop through all the rows in this array and for each calls the callback functions (passing along the params also stored in this array).

Then it will remove those callbacks from the array. It does this in a way that allows the callback functions themselves to make more calls to the New function without losing those callbacks when the Async Callback Processor removes those callbacks it is currently processing.

Any calls to New during a callback will schedule another callback, but it will not get processed until the next tick. So you will not get caught in an infinite loop by creating a callback during a callback.

.CAPX

newobjectswithcallback.capx

Download now 238.46 KB
.CAPX

createspritebynamesync.capx

Download now 197.14 KB
  • 0 Comments

  • Order by
Want to leave a comment? Login or Register an account!