Don't Subtract Global Value Below Zero

Get help using Construct 2

Post » 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
22
S
4
G
1
Posts: 153
Reputation: 1,938

Post » 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)
Paradox2014-01-28 02:31:37
B
240
S
62
G
33
Posts: 903
Reputation: 40,589

Post » Tue Jan 28, 2014 2:30 am

wut

How would I enclose it?
B
22
S
4
G
1
Posts: 153
Reputation: 1,938

Post » 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
A big fan of JavaScript.
B
76
S
20
G
74
Posts: 2,251
Reputation: 46,476

Post » 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
61
S
20
G
56
Posts: 1,077
Reputation: 35,991

Post » 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
240
S
62
G
33
Posts: 903
Reputation: 40,589

Post » Tue Jan 28, 2014 2:35 am

[QUOTE=Colludium]

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

Me too!
B
61
S
20
G
56
Posts: 1,077
Reputation: 35,991

Post » Tue Jan 28, 2014 2:39 am

Thanks everybody, I used Colludium's solution and it works like a charm.
B
22
S
4
G
1
Posts: 153
Reputation: 1,938

Post » 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
90
S
18
G
9
Posts: 2,455
Reputation: 15,038


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 30 guests