# How do I set a maximum variable value?

Get help using Construct 2

### » Thu Nov 26, 2015 4:15 pm

I am making an ammo system but when I put it to add 30 to the count it just keeps adding it... so how do I make my ammo variable have a value limit?
B
8
S
2
Posts: 55
Reputation: 654

### » Thu Nov 26, 2015 4:18 pm

ammo=min(ammo+how much you add, maximum amount you want it to be )
ammo=min(ammo+30,200)
What I know would fill a Book , what I don't know would fill a Library
B
37
S
12
G
29
Posts: 331
Reputation: 16,499

### » Thu Nov 26, 2015 4:21 pm

Would The clamp expression work for you?

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

https://www.scirra.com/manual/126/system-expressions
I told my dentist I had trouble with my teeth and asked her to fix it without looking in my mouth..
B
54
S
16
G
8
Posts: 6,160
Reputation: 19,775

### » Thu Nov 26, 2015 4:29 pm

How do I make these? And btw, I have a global variable for the ammo.
B
8
S
2
Posts: 55
Reputation: 654

### » Thu Nov 26, 2015 4:36 pm

These are expressions, you use them inside your event and/or actions..

system set ammo: ammo+30
or

you use

system set ammo: min(ammo+30,200)
or
system set ammo: clamp(ammo+30, 0, 200)

you could also use your normal way and add an every tick event setting ammo to clamp(ammo, 0, 200), that way it can never get below 0 or above 200..
I told my dentist I had trouble with my teeth and asked her to fix it without looking in my mouth..
B
54
S
16
G
8
Posts: 6,160
Reputation: 19,775

### » Thu Nov 26, 2015 4:38 pm

use either expression in an action after the triggering condition ;
player collides with ammo --->ammo=min(ammo+30,200)...............or........ammo=clamp(ammo+30,0,200)
What I know would fill a Book , what I don't know would fill a Library
B
37
S
12
G
29
Posts: 331
Reputation: 16,499

### » Thu Nov 26, 2015 4:39 pm

@LittleStain almost at the same time
What I know would fill a Book , what I don't know would fill a Library
B
37
S
12
G
29
Posts: 331
Reputation: 16,499

Thanks
B
8
S
2
Posts: 55
Reputation: 654

### » Thu Nov 26, 2015 4:48 pm

Now that I created a reserve ammo system, how do I do the same thing but so that it removes only 30 from the value?
B
8
S
2
Posts: 55
Reputation: 654

### » Thu Nov 26, 2015 4:56 pm

change the plus to a minus?

clamp(ammo-30, 0, 200), should work fine..
I told my dentist I had trouble with my teeth and asked her to fix it without looking in my mouth..
B
54
S
16
G
8
Posts: 6,160
Reputation: 19,775

Next