Mages' Might unable to do things in a loop

We rewrite simple infinite loops that just use while True: to insert a one-frame yield if you don’t take any blocking action. So this:

while True:
    if hero.isReady():
        hero.cleave()

effectively becomes:

while True:
    if hero.isReady():
        hero.cleave()
    if <no time has passed since the start of the loop>:
        hero.wait(0.01)

We don’t do this when the while condition is something more complicated, because maybe you want to loop over an array or something. So time doesn’t pass inside those loops; you’d have to add the hero.wait(0.01) call yourself. wait is available on some of the wristwatches.

In Mages’ Might, though, you just don’t have that API, so you’d have to rewrite your while True: loop like this so that you can take advantage of automatic simple loop yielding:

hero.cast("fire-ball")
while True:
    missile = hero.findNearest(hero.findFriendlyMissiles())
    print(missile.pos.x)
    if hero.color == "red" and missile.pos.x > 50:
        break
    elif hero.color == "blue" and missile.pos.x < 50:
        break
print("End of loop")
2 Likes