math.Clamp

From Garry's Mod
Jump to: navigation, search
 math.Clamp( )

Contents

Description

Clamps a number between a minimum and maximum value

Arguments

number input

The number to clamp.

Arguments

number min

The minimum value, this function will never return a number less than this.

Arguments

number max

The maximum value, this function will never return a number greater than this.

Returns

number

The clamped value.

Examples

Example

Demonstrates what this function does.

print( math.Clamp( 10, 0, 5 ) ) -- 10 is greater than 5 so returns 5
print( math.Clamp( 3, 0, 5 ) ) -- 3 is greater than 0 and less than 5, so returns 3
print( math.Clamp( -1, 0, 5 ) ) -- -1 is less than 0, so returns 0

Output:

5

3

0

Examples

Example

Heals player "ply" by 10 health, but won't let their health go above 100.

ply:SetHealth( math.Clamp( ply:Health() + 10, 0, 100 ) )


Personal tools
Navigation