API Reference › Math › Math.clamp
method

Math.clamp

Math.clamp(v, lo, hi) -> int

Returns v held inside the inclusive range lo..hi: below lo it returns lo, above hi it returns hi, otherwise v unchanged. It is the single-call form of Math.max(lo, Math.min(v, hi)) and works for int and fixed alike. Use it to keep a camera inside the world, a slider within its track, or a stat within its legal bounds. The bare form clamp(v, lo, hi) is kept as a convenience alias.

Parameters

vthe value to constrain
lothe lowest allowed result (inclusive)
hithe highest allowed result (inclusive)

Example

program CameraBounds {
  const WORLD_WIDTH: int = 320

  var camera_x: int = 0

  handler Follow phase Update {
    camera_x = Math.clamp(camera_x, 0, WORLD_WIDTH - 1)
  }
}
← All symbols