Native Expressions in Construct 2

Favourite 155 favourites
Tutorial written by pearsonlandsOriginally published on 14th, November 2011 - 16 revisions

Operators

You can use the following operators in expressions:

+ (addition)
- (subtraction)
* (multiplication)
/ (division)
% (modulo, remainder after division)
^ (raise to power, e.g. 5 ^ 2 = 25)
& (build strings, e.g. "Your score is: " & score)

There are also comparison operators =, <>, <, <=, >, >= for comparing values. They return 1 if the comparison is true or 0 if false.

& also doubles as logical AND, and | is a logical OR operator. These are useful combined with the comparison operators, e.g. score < 0 | health < 0, which also return 1 if true and 0 if false.

?: is a conditional operator, which allows you to test conditions in expressions. This is especially useful when used with the comparison operators and logical operators. It takes the form condition ? result_if_true : result_if_false
e.g. health < 0 | score < 0 ? "Game over!" : "Keep going!".
The condition counts as true if it is non-zero, and false if it is zero.

Strings (text)

Text is also known as strings in programming, and Construct 2 also sometimes uses this naming convention. Text in expressions should be surrounded by double-quotes, e.g. "Hello"

The double-quotes are not included as part of the text, so setting a text object to show the expression "Hello" will make it show Hello, without any double-quotes. To include a double-quote in a string, use two double-quotes next to each other (""), e.g. "He said ""hi"" to me" will return He said "hi" to me.

You can use & to build strings out of mixed text and numbers, e.g. "Your score is: " & score

Standard Mathematical Expressions

The full list of expressions can be found in Construct 2 itself: double-click the System object in the floating expressions panel when you're typing in an expression. However, some common ones are listed here.

sin (sine of an angle in degrees)
cos (cosine of an angle in degrees)
tan (tangent of an angle in degrees)
asin (inverse sine, returns degrees)
acos (inverse cosine, returns degrees)
atan (inverse tangent, returns degrees)
sqrt (square root)
abs (absolute value)
exp (exponent, e^x)
ln (logarithm to base e)
log10 (logarithm to base 10)

It's worth remembering Construct 2 always uses degrees, never radians. If you need to enter an angle anywhere in Construct 2, it will be in degrees.

Conversion Expressions

int(x) - convert x to integer
float(x) - convert x to float
str(x) - convert x to string

Don't forget you can use & to build strings, e.g. "Your score: " & score returns "Your score: 10" as a string if score is 10.

Other Expressions

Angle(x1, y1, x2, y2) - Returns the angle, in degrees, between the points (x1, y1) and (x2, y2), using atan2(y2 - y1, x2 - x1).

Ceil(number) - Round a number up (eg. ceil(6.1) returns 7).

Distance(x1, y1, x2, y2) - Returns the distance between the points (x1, y1) and (x2, y2).

dt - Delta-time. See Delta-time and framerate independence.

Floor(number) - Round a number down (eg. floor(6.7) returns 6).

fps - Current framerate in frames per second.

len(x) - return length of the string x

Lerp(a, b, x) - Linear interpolation: Calculates a + x(b - a), or linearly interpolates a to b by x%. Eg. lerp(a, b, 0.25) gives the value 25% of the way from a to b

LoopIndex - Current (top-level) loop index

LoopIndex("name") - Loop index of a loop of given name

ObjectCount - Total number of object instances in the game.

Max(a, b [,c,...]) - Retrieve the maximum of the given values. You can pass any number of values

Min(a, b [,c,...]) - Retrieve the minimum of the given values. You can pass any number of values

Newline - text string containing a line break character, e.g. "Hello" & newline & "World"

Random(N) - Returns a random number between 0 and N, not including N. The result includes fractions, so random(3) can return 2.5. Use floor(random(n)) to generate a random whole number up to but not including N, e.g. floor(random(3))* will return either 0, 1 or 2. Random(A, B) can also be used to generate a random number between A and B.

Round(value) - Rounds value to its nearest whole number, eg. Round(6.8) gives 7

Scrollx, Scrolly - Current scroll position

Timescale - The current time scale. See Delta-time and framerate independence.

Time - Time in seconds since the game started.

TickCount - Number of ticks elapsed since the game started.

Don't forget to check the expressions panel in Construct 2 for the full list.

Useful expressions in other objects

Both the Mouse and Touch objects allow you to pass a layer parameter for the X and Y expressions. This is very useful if you scale or rotate layers.

e.g. Mouse.X("Layer 1") returns the mouse's X position on Layer 1, taking in to account its parallax, scale and angle. If you use only Mouse.X, it does not take in to account parallax, scale or angle, so the result can be wrong.

Object indexing in expressions

You can add a 0-based object index to get expressions from different object instances.

e.g. Sprite(0).X gets the first Sprite instance's X position, and Sprite(1).X gets the second instance's X position. You can also pass another expression for the index. Negative numbers start from the opposite end, so Sprite(-1).X gets the last Sprite's X position.

This can be applied to any object expression, not just Sprites.

Congratulations on finishing this tutorial!

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

Comments

9
sqiddster 26.4k rep

Very helpful tutorial! Especially for those of us who are not maths geniuses ;)

Tuesday, November 15, 2011 at 5:11:50 AM
4
gammabeam 12.5k rep

Great reference for the expressions!
You should add the "distance" expression, very useful!

distance(Obj1.X, Obj1.Y, Obj2.X, Obj2.Y)

Wednesday, November 16, 2011 at 1:46:51 PM
7
Ashley 111.9k rep

Some of the info was out of date since that forum post you had linked to so I made a few edits, hope you don't mind!

Wednesday, November 16, 2011 at 4:31:08 PM
2
mammoth 7,953 rep

Thank you I really needed this :)

Wednesday, November 16, 2011 at 10:36:26 PM
5
newt 22.4k rep

Added some quotes for named loops.
loopindex("a") rather than loopindex(a)

Wednesday, November 16, 2011 at 11:59:44 PM
1
Wastrel 10.9k rep

Nice tutorial. It is really helpful!

Friday, November 18, 2011 at 9:45:23 PM
1
Aron 4,036 rep

Really useful guide, I was trying to work out how to do carriage returns - turns out the function was there all the time... "newline" :)

Saturday, November 19, 2011 at 3:19:31 PM
2
davidjmcclelland 2,814 rep

This is a great start - thank you.

Consider adding coverage of magic keywords like "words", "chars", and how they relate to "At", "Count", "CurValue", "CurX, Y, Z", "Depth", "Height", "Width", "UID" would also be helpful.

I am trying to make a word game and probably didn't need to insert delimiters into the content strings if I can get word and char counts in a 2D loop without them.

Saturday, November 19, 2011 at 6:23:20 PM
2
Crea 3,286 rep

Oh thank god, I've been looking for these!

Wednesday, November 23, 2011 at 7:17:44 AM
1
AngeloFernandes 4,333 rep

This is gold :)
Thanks, I have no knowledge about this stuff and this way I can alway take peek when needed.

Wednesday, November 23, 2011 at 9:02:02 PM
1
TiAm 4,192 rep

Hey thanks pearsonlands; I was a little rusty on my coding and I couldn't remember how to mix fixed text and variables in a score readout; After reading thru the tut here, I got everything up and running fine.

Saturday, November 26, 2011 at 7:23:56 PM
3
ayzhong 4,566 rep

I have found the C2 treasure chest :D

Sunday, November 27, 2011 at 9:51:46 PM
3
Vinians 4,272 rep

Nice tutorial, but we need some string functions to decompose some string...

Thursday, December 08, 2011 at 9:59:45 PM
0
7Soul 10.8k rep

Wow, i never knew I could use comparison and && or || in an expression, this changes everything! Thx

Saturday, December 24, 2011 at 1:09:04 AM
1
UberDark 4,741 rep

Handy resource!
Favorited..

Wednesday, February 08, 2012 at 1:28:23 PM

Leave a comment

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