How to do advanced callbacks in Construct 2

Favourite 40 favourites
Tutorial written by ValerienOriginally published on 25th, July 2014 - 1 revision

This tutorial is a follow up to my article “How to improve your Construct 2 code with callbacks”. Today, we will take a look at some techniques to achieve a more flexible and complex callback function for our projects. If you don’t know what callbacks are, I invite you to take a look at my previous tutorial first. And if you like what I'm doing, you can follow me on twitter and on facebook!
You can find an example capx containing the callback code with comments! (made with R175)

Back to the callback

Last time, we left off with a pretty simple function that takes a string, and calls a function. If you have tried to call a complex function with multiple parameters, you may have noticed it doesn’t work!

Our first, simple callback function

This may seem a little counter intuitive to developers out there. If your callback variable holds a value of "death, player", Construct 2 will consider the whole string to be the name of your function!

In order to call the right function back, we need to split our text variable into multiple sub-strings. Then, we have to feed them manually one by one as parameters into our function call action.

Splitting strings

In order to split our chain of characters, we will use tokens. A token is an atomic element or word used to analyze (or parse) a string. It can be anything we want: a comma, a semicolon, a dot… this is just a single character that the token related functions will search for. Construct 2 has two system expressions dedicated to tokens:

- tokencount(text, token) returns the number of tokens inside a string,
- tokenat(text, index, token) returns a bit of text situated before, after or between two tokens

We need to use both methods. We can create our callback function in three steps:

1- Count the number of parameters/bits of string to split with tokencount()
2- Extract the bits of string (loop through the tokens) and store them in a small array
3- Call the function using the stored parameters

Let us first add a text variable called “p” for our source parameters. It will be set to Function.Param(0), that is our initial callback string to split. We then have to create a local count variable in our Callback function, and set it to tokencount(p, “,”). The count gives us an idea of the number of parameters in our function, as well as the number of elements to split in our text.

Token Count example and p variable
We need an array to store our parameters. Create it and call it “aCallback”. In the properties panel, change its X dimensions to 5 or 6 (this is the maximum amount of parameters we will need, including the function name). Then, we can work on our for loop. We need it to run for count times. On each loop turn, we’ll run a tokenat(p, loopindex, “,”) and store it into our array at the *loopindex X index.

Loop and array example in Construct 2

Finally, using the variables stored into our array, we simply have to do a function call with the corresponding number of parameters. Depending on the value of our count variable, we can call a function that requires 2, 3 or more parameters.

Calling the function

Writing our callback variables

In order for our callbacks to work properly, it is very important to pay attention to our text variable. A callback variable should be written without spaces: "functionName,param1,param2...", not "functionName, param1, param2...". The latter example won't work properly, as the tokenat() function will keep the space in your split parameters!

To wrap this tutorial up, I'll leave you with an alternative, slightly more optimized version of this callback function. Instead of looping through the array, we can directly call the target function by manually feeding the value returned by tokenat() into the function parameters.

Last words

Do you want a specific tutorial? A capx example for any game system? 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: GDquest ! Finally, you can find all of my Construct 2 tutorials on this forum post.

The next tutorial about UI management will wrap up the current series on general Construct 2 techniques (coming out on Tuesday, 29th). Next up will be some introductory material on AI.

Unlock your full gamedev potential

Upgrade to the Personal Edition of Construct 2, it has way more features and won't holding back from making money and using your full creativity like the free edition does. It's a one off payment and all Construct 2 editor updates are free for life!

View deals

Plus, it's got a lot of additional features that will help you save time and make more impressive games!

Congratulations on finishing this tutorial!

Did you learn a lot from it? Share it now with your friends!

Share and Copy this Tutorial

You are free to copy, distribute, transmit and adapt this work with correct attribution. Click for more info.


DrKoala 1,016 rep
Friday, October 07, 2016 at 11:00:15 AM
NetOne 9,052 rep

.......bits of brain everywhere after head exploded........

Thursday, February 16, 2017 at 2:14:49 PM

Leave a comment

Everyone is welcome to leave their thoughts! Register a new account or login.