Expressions in Construct 2

Discussion and feedback on Construct 2

» Fri Feb 04, 2011 11:16 pm

Since there's not currently any UI to pick expressions, here's a list of all expressions currently supported in C2.

[size=150:1o1zlee0]General expression features[/size:1o1zlee0]
Javascript does not actually support an integer datatype, but support is emulated by the runtime by rounding floats. However, any operator involving a float returns a float (so e.g. 5 + 0.5 returns 5.5 like you expect) so this probably isn't going to be an issue for anyone. Also, division always returns a float, so 5 / 2 = 2.5 (as with 0.x), if you want integer rounded division, explicitly specify it with int(5 / 2).

There are no expression shortcuts like in 0.x: you can't type .X as a substitute for Self.X. However, the Self keyword works.

You can specify specific instances in object expressions: Sprite(0).X gets the first picked Sprite's X co-ordinate, Sprite(1).X the second, and so on. Sprite(N) when there are N sprites will wrap-around and return the first Sprite's X again, Sprite(N+1) the second, and so on. Sprite(-1).X will return the last picked Sprite's X co-ordinate, Sprite(-2).X the second last, and so on. You can use any expression for the instance to pick, you don't have to just enter a number.

The usual arithmetic operators are supported: + - * / % ^
There's a new unary minus operator, so you can write -Sprite.X instead of 0 - Sprite.X.
The conditional a ? b : c operator was a bit buggy in 0.x - it should be working correctly in C2. Example: Sprite.X < 100 ? -1 : 1

The & operator is string concatenation as in 0.x, or logical AND when both operands are numbers. The | operator is logical OR (and has no special behavior with strings).

[size=150:1o1zlee0]System[/size:1o1zlee0]
int(x) - convert x to integer
float(x) - convert x to float
str(x) - convert x to string
len(x) - return length of the string x
random(x) - return a random float from 0 (inclusive) to x (exclusive) - note this differs from Construct 0.x where random would return an integer if you passed it an integer. Use int(random(x)) to reproduce this behavior.
random(a, b) - return a random float from a (inclusive) to b (exclusive).
sqrt(x) - compute square root of x.
abs(x) - absolute (positive) value of x.
round(x) - round x to the nearest whole number.
floor(x) - round x to the lowest next whole number.
ceil(x) - round x to the highest next whole number.
sin(x), cos(x), tan(x), asin(x), acos(x), atan(x) - trigonometric functions, which work in degrees
exp(x) - e raised to the power x
ln(x) - log to base e of x
log10(x) - log to base 10 of x
lerp(a, b, x) - linearly interpolate a to b by x
distance(x1, y1, x2, y2) - distance between two points
angle(x1, y1, x2, y2) - angle between two points (degrees)
loopindex - current (top-level) loop index
loopindex(name) - loop index of 'for' loop of given name
max(x, y [, z...]), min(x, y [, z...]) - returns maximum/minimum of any number passed - like 0.x you can pass any number of parameters here.
newline - text string containing a line break character.
dt - new name for 'timedelta' which is the standard used in games programming (it's actually usually referred to as 'delta time', so 0.x got this the wrong way round!). Currently always returns a fixed value (1/60th of a second) because Javascript does not have a high-resolution timer. You should still use it so you're future-proofed in case dt is ever modified to actually be a time measurement, and when implemented, timescale will modify this value.
timescale - always 1.0 at this time.
time - time in seconds since the game started.
tickcount - number of ticks elapsed since the game started.
objectcount - total number of object instances in the game.
fps - current framerate in frames per second.
scrollx, scrolly - current scroll position

[size=150:1o1zlee0]Common object expressions[/size:1o1zlee0]
Currently, Sprite and Text implement these.

X, Y, Width, Height - as with 0.x
Angle - (Sprite only) Angle in degrees
Count - number of instances

[size=150:1o1zlee0]Other object expressions[/size:1o1zlee0]
There are no longer MouseX or MouseY system expressions - insert the Mouse object and use Mouse.X and Mouse.Y for the mouse position in the layout, or Mouse.AbsoluteX and Mouse.AbsoluteY for the position on the canvas.

The text object also has a 'Text' expression (surprise!).

[size=150:1o1zlee0]Instance variables[/size:1o1zlee0]
Access these by their name like this:

MyObject.MyInstanceVariable

You can't name an instance variable the same as an object expression.
Scirra Founder
B
387
S
230
G
87
Posts: 24,249
Reputation: 192,240

» Sat Feb 05, 2011 2:00 am

have not finished reading yet, but this is making me elated:
"You can use any expression for the instance to pick, you don't have to just enter a number."

Instancing was such a huge problem for me ever since mmf2, i'm so glad that someone is finally making a simple system for this. This will double the spectrum of games i can make, and halve the time of most.
B
4
S
1
G
2
Posts: 14
Reputation: 903

» Sun Feb 06, 2011 2:28 am

Tried messing around with sprite(instance), and my head is spinning now(not in the good way ).
Found out tho that text doesn't accept expressions, unless you use str(expression).
Not sure if that's a bug, or a "willbe", as in will be added.
B
168
S
50
G
163
Posts: 8,221
Reputation: 105,061

» Sun Feb 06, 2011 4:11 am

Someone needs to work on an Ipad/Iphone touching plugin.
I may give it a go, but don't count on it.

http://www.sitepen.com/blog/2008/07/10/ ... he-iphone/
B
2
S
2
G
4
Posts: 156
Reputation: 1,762

» Thu Feb 17, 2011 12:21 am

Is it possible to add lerp here, please?

I'm a coding newb, and it would be hugely helpful if this list was maintained
B
32
S
3
G
2
Posts: 57
Reputation: 3,692

» Thu Feb 17, 2011 12:28 am

I've updated the list with expressions added in recent builds, including lerp.
Scirra Founder
B
387
S
230
G
87
Posts: 24,249
Reputation: 192,240

» Thu Feb 17, 2011 6:49 am

Thank you very much!
A printout of this makes a world of difference for me
B
32
S
3
G
2
Posts: 57
Reputation: 3,692

» Sat Feb 19, 2011 3:40 pm

[quote:sd9x7jjw]The conditional a ? b : c operator was a bit buggy in 0.x - it should be working correctly in C2. Example: Sprite.X < 100 ? -1 : 1[/quote:sd9x7jjw]

Could someone explain this a bit further? I've never used any of that before.
B
8
S
2
G
5
Posts: 744
Reputation: 3,288

» Sat Feb 19, 2011 4:10 pm

Scirra Founder
B
160
S
41
G
34
Posts: 4,386
Reputation: 53,509

» Sat Feb 19, 2011 4:12 pm

It works like this:

<some condition> ? <what to return if condition true> : <what to return if condition false>

So you can put conditions directly in expressions. For example:

+ Sprite X < 100
> Set text to "Left"

+ Sprite X >= 100
> Set text to "Right"

is equivalent to

+ Every tick
> Set text to Sprite.X < 100 ? "Left" : "Right"
Scirra Founder
B
387
S
230
G
87
Posts: 24,249
Reputation: 192,240

Next