method
Query.nearest
Query.nearest(prop, pos, x_field, y_field, x, y) -> int
Returns the live entity carrying prop whose position is closest to (x, y), or -1 if none. Position comes from property pos at its integer fields x_field and y_field — pass the same id for prop and pos to query the coordinate component itself, or a separate tag for prop to mean "nearest Enemy". Distance is compared by exact squared integer distance (no square root), and ties go to the lower entity id, so the result is fully deterministic. The go-to for target acquisition, "interact with the closest thing", or nearest-neighbour AI.
Parameters
propthe property that filters which entities are consideredposthe property holding the coordinatesExample
program Demo {
property Position { col: int = 0, row: int = 0 }
property Enemy { hp: int = 0 }
model Slime { Position, Enemy }
entry {
spawn Slime { Position { col: 2, row: 0 }, Enemy { hp: 3 } }
spawn Slime { Position { col: 9, row: 0 }, Enemy { hp: 3 } }
let E = World.prop_id("Enemy")
let P = World.prop_id("Position")
let cx = World.field_id(P, "col")
let cy = World.field_id(P, "row")
let e = Query.nearest(E, P, cx, cy, 0, 0) # the enemy at col 2
print(World.get(e, P, cx)) # 2
}
}