API Reference › Query › Query.within
method

Query.within

Query.within(prop, pos, x, y, radius, x_field, y_field) -> []int

Returns the live entities carrying prop whose position is within radius of (x, y), as a []int of entity ids in ascending id order — index it with len / [i]. Position is read from property pos at x_field/y_field, exactly as in Query.nearest. The boundary is inclusive (distance <= radius), measured by exact integer distance. Use it for area-of-effect damage, proximity triggers, flocking neighbours, or "what's near the cursor".

Parameters

propthe property that filters which entities are considered
posthe property holding the coordinates
radiusthe inclusive radius

Example

program Demo {
  property Position { col: int = 0, row: int = 0 }
  property Unit { hp: int = 0 }
  model Soldier { Position, Unit }
  entry {
    spawn Soldier { Position { col: 1, row: 1 }, Unit { hp: 5 } }
    spawn Soldier { Position { col: 8, row: 8 }, Unit { hp: 5 } }
    let U  = World.prop_id("Unit")
    let P  = World.prop_id("Position")
    let cx = World.field_id(P, "col")
    let cy = World.field_id(P, "row")
    let hit = Query.within(U, P, 0, 0, 3, cx, cy)   # just the (1,1) soldier
    print(len(hit))                                  # 1
  }
}
← All symbols