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.
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)
wut
How would I enclose it?
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...!
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
Use compare two values instead of compare Global Variable. You can enter the variable there with the function and parentheses around it.
Develop games in your browser. Powerful, performant & highly capable.
Me too!
Thanks everybody, I used Colludium's solution and it works like a charm.
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)