Expressions in Construct 2

0 favourites
From the Asset Store
Casino? money? who knows? but the target is the same!
  • Since there's not currently any UI to pick expressions, here's a list of all expressions currently supported in C2.

    General expression features

    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).

    System

    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

    Common object expressions

    Currently, Sprite and Text implement these.

    X, Y, Width, Height - as with 0.x

    Angle - (Sprite only) Angle in degrees

    Count - number of instances

    Other object expressions

    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!).

    Instance variables

    Access these by their name like this:

    MyObject.MyInstanceVariable

    You can't name an instance variable the same as an object expression.

  • 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.

  • 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.

  • 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/

  • Is it possible to add lerp here, please?

    I'm a coding newb, and it would be hugely helpful if this list was maintained

  • I've updated the list with expressions added in recent builds, including lerp.

  • Thank you very much!

    A printout of this makes a world of difference for me

  • [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

    Could someone explain this a bit further? I've never used any of that before.

  • 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"

  • Woah!! That's just brilliant!

  • Hi.. I'm currently experimenting with adding signposts within a platformer..

    When the player object is overlapping a signpost sprite, a text object appears above and displays the text contained within the signpost variable.

    player on collision with Signpost ---> Signposttext set text to signpost.signtext

    However it always displays the text from the *first* instance of the signpost object, rather than the one the player has collided with.

    I tried it with a number based instance variable, simply called "id" with '0' for the first signpost and '1' for the second signpost... that too, also displayed "0" no matter which instance was collided with.

    I tested it to see if it was because it was a variable at all.... I set it to display the signpost.x instead of the signpost.signtext variable, and it works, it picked the instance that had been collided with.

    So have I found a bug or am I missing something? D:

    thanks in advance! I'll post the full cap into the C2 Tests thread when i've got it working )

  • That is a bug.

    Its somehow not being saved in the editor.

    Testing shows you can set the variable instances at runtime using pick "nth", so that part works correctly.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Can you post a .cap of that not working properly on the tracker?

  • Bug added.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)