# Don't Subtract Global Value Below Zero

Get help using Construct 2

### » Tue Jan 28, 2014 2:24 am

Hi Construct Community,

How do I make a global number variable only natural numbers (0,1,2,3....) and exclude negative numbers? I'm making an ammo system, and I don't want the ammo count to go below zero.

Thanks.
B
27
S
6
G
1
Posts: 153
Reputation: 2,263

### » Tue Jan 28, 2014 2:29 am

Enclose it in ABS(variable)

from the manual:
abs(x) Absolute value of x e.g. abs(-5) = 5

Or you might want Clamp(), or Max()

Clamp locks it to a range, CLAMP(variable,0,200)
or Max picks the highest number, MAX(Variable,0)
B
253
S
65
G
34
Posts: 907
Reputation: 41,565

### » Tue Jan 28, 2014 2:30 am

wut

How would I enclose it?
B
27
S
6
G
1
Posts: 153
Reputation: 2,263

### » Tue Jan 28, 2014 2:32 am

Only subtract whole numbers from your Ammo_Total global variable. Then have an every tick sub-event somewhere convenient:

If Ammo_Total < 0 then Ammo_Total = 0.

Edit - I see I'm too slow at typing again...!Colludium2014-01-28 02:33:22
Learning other engines, but C2 still rocks.
B
80
S
22
G
95
Posts: 2,413
Reputation: 57,663

### » Tue Jan 28, 2014 2:33 am

or if you don't understand the above suggestion, you can just make a simple event like if variable is less then 0 set value to 0
B
62
S
20
G
57
Posts: 1,077
Reputation: 36,531

### » Tue Jan 28, 2014 2:33 am

Use compare two values instead of compare Global Variable. You can enter the variable there with the function and parentheses around it.
B
253
S
65
G
34
Posts: 907
Reputation: 41,565

### » Tue Jan 28, 2014 2:35 am

[QUOTE=Colludium]

Edit - I see I'm too slow at typing again...![/QUOTE]

Me too!
B
62
S
20
G
57
Posts: 1,077
Reputation: 36,531

### » Tue Jan 28, 2014 2:39 am

Thanks everybody, I used Colludium's solution and it works like a charm.
B
27
S
6
G
1
Posts: 153
Reputation: 2,263

### » Tue Jan 28, 2014 3:29 am

@awmace5
You also have the option and my growing favorite.

CLAMP( newvalue, minimum, maximum )
example:
Action: set global variable ammo with the line clamp( ammo - 1, 0, 100)
B
94
S
18
G
9
Posts: 2,457
Reputation: 15,177