Mages' Might unable to do things in a loop

Currently in Mages’ Might, I’m trying to get the program to wait until the projectile gets to a certain position using this code:

hero.cast("fire-ball");
timeForSpell = False;
while not timeForSpell:
    missiles = hero.findMyMissiles();
    print(missiles[0].pos.x);
    if missiles[0].pos.x > 50:
      timeForSpell = True;
print("End of loop");

However, the program is unable to move past the loop and continually prints out the same position. Does anyone know why this is happening?

while not does work in the campaign levels, but I don’t think it is supported in Mages’ Might.

You could try and use while true or for loops

well in the code its while the timeforspell is not true. If you were to use while true it would change to code to while the timeforspell is true.

I believe that THIS is the right code:


hero.cast("fire-ball");
timeForSpell = True;
while timeForSpell:
    missiles = hero.findFriendlyMissiles();
    print(missiles[0].pos.x);
    if missiles[0].pos.x > 50:
      timeForSpell = False;
print("End of loop");

Unfortunately, using while instead of while not still doesn’t work for me.

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

Ok, so I have to break the loop to end it rather than have a variable controlling it.

I am having trouble with my code, here it is.
hero.cast(“fire-ball”)
while True:
missile = hero.findMyMissiles()
print(missile.pos.x)
if hero.color == “red” and missile.pos.x > 50:
break
print(“End of loop”)
if you could help me fix it I would appreciate it

welcome to the forum justin!

I believe your problem lies in the fact that hero.findMyMissiles() returns an array but you’re treating it as a single object.

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.