Godot Version
v4.3.stable.official [77dcf97d8]
QuestionI’m trying to implement the range function below from https://www.redblobgames.com/grids/hexagons/#range in GDScript.
var results = []
for each -N ≤ q ≤ +N:
for each max(-N, -q-N) ≤ r ≤ min(+N, -q+N):
var s = -q-r
results.append(cube_add(center, Cube(q, r, s)))
The function is given in a psuedocode, and I’m not sure I have implemented it in GDScript (below) correctly (as the function isn’t giving the results I’m expecting).
static func return_coordinates_in_range(center: Vector3i, range: int) -> Array:
var n: = range
var q: = center.x
var r: = center.y
var results: Array = []
for counter_a in range:
if -n <= q and q <= n:
for counter_b in range:
if max(-n, -q-n) <= r and r <= min(n, -q+n):
var s = -q-r
var result: Vector3i = cube_add(center, Vector3i(q,r,s))
results.append(result)
return results
Have I implemented the redblob range function into GDScript syntax correctly?