How to do advanced callbacks in Construct 2

3

Attached Files

The following files have been attached to this tutorial:

.capx

advancedcallbacks.capx

Download now 194.84 KB

Stats

5,441 visits, 7,877 views

Tools

Translations

This tutorial hasn't been translated.

License

This tutorial is licensed under CC BY 4.0. Please refer to the license text if you wish to reuse, share or remix the content contained within this tutorial.

Published on 25 Jul, 2014. Last updated 19 Feb, 2019

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!

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.

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.

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.

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.

.CAPX

advancedcallbacks.capx

Download now 194.84 KB
  • 1 Comments

  • Order by
Want to leave a comment? Login or Register an account!
  • Hey there, you could resolve the spaces problem with the trim function. Simply trim(tokenat("t,e,s,t", loopindex, ",")) the stuff and you're safe on the rave.

    Cheers!