Native Expressions in Construct 2

4

Stats

39,137 visits, 91,421 views

Tools

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 14 Nov, 2011. Last updated 19 Feb, 2019

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.

  • 0 Comments

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