How to write this kind of loop in GDScript

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By pospathos

Does anyone knows how to write this loop in GDScript:

for (var ix = 0, iy = 0; ix < nx || iy < ny;)?

Code example:

function walk_grid(p0, p1) {
var dx = p1.x-p0.x, dy = p1.y-p0.y;
var nx = Math.abs(dx), ny = Math.abs(dy);
var sign_x = dx > 0? 1 : -1, sign_y = dy > 0? 1 : -1;

var p = new Point(p0.x, p0.y);
var points = [new Point(p.x, p.y)];
for (var ix = 0, iy = 0; ix < nx || iy < ny;) {
    if ((0.5+ix) / nx < (0.5+iy) / ny) {
        // next step is horizontal
        p.x += sign_x;
        ix++;
    } else {
        // next step is vertical
        p.y += sign_y;
        iy++;
    }
    points.push(new Point(p.x, p.y));
}
return points;

}

Thanks!

What language is that?

woopdeedoo | 2018-11-05 02:53

Looks like JavaScript to me.

omggomb | 2018-11-05 10:35

:bust_in_silhouette: Reply From: woopdeedoo

At a glance it seems to be a short form of two nested for loops, but I think it could more easily be done with a while loop in GDScript, like so:

var ix = 0
var iy = 0
while ix < nx || iy < ny:
	if ((0.5+ix) / nx < (0.5+iy) / ny):
		# next step is horizontal
		p.x += sign_x
		ix += 1
	else:
		# next step is vertical
		p.y += sign_y
		iy += 1