API Reference › Builtin functions › abs
function

abs

abs(a) -> int

Returns the magnitude of an integer, dropping its sign — so abs(-7) and abs(7) both give 7. It is handy for distance-style checks where direction does not matter, such as measuring how far two grid cells are apart along one axis before deciding whether something is "close enough". Combine two axis distances to build a simple proximity test. It takes a single integer and returns an integer.

Parameters

athe integer whose magnitude you want

Example

program Proximity {
  property Position { column: int = 0, row: int = 0 }
  model Player { Position }
  model Enemy { Position }

  handler SpawnActors phase Start {
    spawn Player { Position { column: 5, row: 5 } }
    spawn Enemy { Position { column: 8, row: 6 } }
  }

  handler CheckAdjacency phase Update {
    for (PlayerPosition) in query [Position, {Player}] {
      for (EnemyPosition) in query [Position, {Enemy}] {
        let column_gap = abs(PlayerPosition.column - EnemyPosition.column)
        let row_gap = abs(PlayerPosition.row - EnemyPosition.row)
        if column_gap + row_gap <= 1 { print(1) }
      }
    }
  }
}
← All symbols