How to improve your Construct 2 code with Callbacks

Attached Files

The following files have been attached to this tutorial:



3,574 visits, 7,480 views


This tutorial hasn't been translated.


Today, we will take a look at synchronous callbacks. Behind this expression lies a simple, yet powerful programming concept. Let us see how it can help us to build more efficient code in construct 2. And if you like what I'm doing, you can follow me on twitter and on facebook!

What is a callback?

According to wikipedia, "a callback is a piece of executable code that is passed as an argument to other code, which is expected to call back (execute) the argument at some convenient time." In other words, a callback is a bit of text that is read by your program and executed as a function.

In construct 2, let us use that concept to call functions based on text stored into our objects’ variables. In practice, we are going to apply callbacks to UI elements.

An example: the UI mess

When writing code for UI elements we generally want a specific button to call a specific function. For example, the pause button will pause the game. The mute button will mute the sound or the music… the list goes on. In construct 2, using brute force, we have to create a specific button touch check condition for each button, like that:

Here, all buttons are placed into a family. Yet, as we add more and more buttons, the code gets heavier. And harder to update.

Callbacks in Construct 2

The callback technique will permit us to have a single event that will determine the function to use for each unique button. First of all, we have to create a text variable in our family. We'll call it "callback".

Then, let us create the input event. When any object of the button type is touched, it will be filtered by construct. We then call a function called "callback" and send it our callback variable as parameter 0. Our callback function still doesn't exist. Let's create it as well. It will call another function using the Function.Call() method. We simply have to use the text contained in the parameter 0 of our function!

Now, we only have to create a function for each button, and type its name and parameters in the callback variable of our objects. For the pause button, we can create a "pause" function. The callback variable of the bPause object will be filled with the text "pause" (without the quote marks).

You may have noticed that we actually don't need the separate callback function here. We can actually call the function we want directly from the input event. Why bother creating a callback function then? Mainly for debugging purposes: if we want to check the content of the variable you are parsing to the callback function, for example. It permits us to check all of the faulty callback variables at specific times in the code, without having to check objects in construct 2's properties panel.

This is a key technique in programming that permits you to establish a modular workflow, and helps you scale your productions up more easily. In our example, we can add any buttons and pair them with any new function quickly! As your project grows, callbacks give you more and more back.

Going further

We have seen how callbacks could be used when a player touches a button on the screen. This is but one of many possible applications! I'll leave you with two slightly more elaborate examples:

1- Let's say we have a platformer game with multiple types of enemies. We want all of them to be contained inside a single family, for organisation's sake, but we want some to feature special abilities. Some die, and we want some others to resurrect after a while. We can simply create a deathCallback text variable and set it to call the functions "death", or "resurrect, timer", or even "explode, timer"

2- We can create a callbackMultipleFunctions function, and have it, for example, call the "setAnimation; death" function and "addScore, 50" at the same time. In our imaginary game, this would set the enemy to play the death animation and add 50 points to the player points pool. The good thing about that is that we can contain all the possible enemy deaths in a single line of code.

Last words

Do you have any idea on how to use callbacks in a clever way? Don't hesitate to leave it in the comments or share it with everyone on Construct 2's forums!

Want a specific tutorial, more information on pretty much anything you see here? Drop me a message! You can follow me and ask me questions directly on twitter, facebook and google plus.

You can find all of my articles and tutorials about game design directly on my website: !



Download now 226 KB

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