# Minimum/Maximum range expression?

Get help using Construct 2

### » Sun Dec 11, 2016 10:11 am

Is there a way of limiting an expression to having a maximum number? A bit like how you can use random(4,84) or something to pick any number between two numbers, no lower than, say, 4 and no higher than 84, can you limit the result to be no higher or lower via an expression?
B
41
S
12
G
14
Posts: 1,124
Reputation: 11,286

### » Sun Dec 11, 2016 10:18 am

Do you mean something like clamp?

clamp(x, lower, upper)
Return lower if x is less than lower, upper if x is greater than upper, else return x.
B
26
S
3
G
1
Posts: 480
Reputation: 2,290

### » Sun Dec 11, 2016 11:15 am

Asmodean wrote:Do you mean something like clamp?

clamp(x, lower, upper)
Return lower if x is less than lower, upper if x is greater than upper, else return x.

Ahh, clamp, yes that's the one, I knew there was one! I'm trying this:
Code: Select all
Pin offsetX: PlayerAnimations.Angle/40

and I want to limit it so it doesn't shift the X too far if the angle of the player is too extreme, clamp will work okay?
B
41
S
12
G
14
Posts: 1,124
Reputation: 11,286

### » Sun Dec 11, 2016 12:58 pm

Zebbi wrote:Ahh, clamp, yes that's the one, I knew there was one! I'm trying this:
Code: Select all
Pin offsetX: PlayerAnimations.Angle/40

and I want to limit it so it doesn't shift the X too far if the angle of the player is too extreme, clamp will work okay?

Yeap, I think it should.
B
26
S
3
G
1
Posts: 480
Reputation: 2,290

### » Mon Dec 12, 2016 11:13 am

If you only want an upper bound, min(x, limit) will not return more than the limit, effectively acting as only enforcing the upper bound. Likewise max(x, limit) will act as a lower bound.
Scirra Founder
B
413
S
244
G
92
Posts: 25,081
Reputation: 199,700