Flags issue in multiplayer

I’m currently trying the Multiplayer treasure grove.
Setting the flags (green or black) in live combat cause hero to stop dead and do nothing. What’s wrong here?

while True:
    item = hero.findNearestItem()
    flag = hero.findFlag()
    enemy = hero.findNearestEnemy()
    
    if flag:
        if flag == "green":
            hero.pickUpFlag(flag)
        elif flag == "black":
            if canCast == True:
                hero.cast("chain-lightning", enemy)
            else:
                hero.removeFlag("black")
         
    elif item:
        if enemy:
            distance = hero.distanceTo(enemy)
            if distance < 10:
                if hero.isReady("bash"):
                    hero.bash(enemy)
                else:
                    hero.attack(enemy)
            else:
                hero.moveXY(item.pos.x, item.pos.y)

Side question: I wonder if this bit is necessary:

if flag:
        if flag == "green":
            hero.pickUpFlag(flag)

Do I have to check if the flag exists before I can check its color, or can I check its color right away? I.e. is this enough:

#if flag:
    if flag == "green":
            hero.pickUpFlag(flag)

?

I tried your and found a few problems,
Firstly: canCast should be used with a hero on the start, e.g. hero.canCast("chain-lightning"), and it is not followed by a True, False statement It’s just:
if hero.canCast("chain-lightning"):

Secondly: I would strongly recommend not using flag = hero.findFlag() and then saying if flag == "green": instead I would define flagGreen and flagBlack with hero.findFlag("green") or hero.findFlag("black") and then say if flagBlack and the same for flagGreen

Thirdly: after the hero.cast("chain-lightning") in,

if canCast == True:
    hero.cast("chain-lightning", enemy)

You need a hero.removeFlag(flagBlack) otherwise your flag will stay there.

Fourthly: This is just a tip for better code, I find using hero.move(item.pos) is better than MoveXY and maybe you could do a bestCoin array to find a bestCoin.

Fifthly: another tip your only collecting coins if there’s also an enemy maybe also add an else at the end and an if item hero.move(item.pos) just if there dead so you don’t just wait there.

Hope this Helps!

1 Like

This helped, thanks!

Secondly: I would strongly recommend not using flag = hero.findFlag() and then saying if flag == "green": instead I would define flagGreen and flagBlack with hero.findFlag("green") or hero.findFlag("black") and then say if flagBlack and the same for flagGreen

I don’t understand why, but it worked. My flags are now working okay.

Thirdly: after the hero.cast("chain-lightning") in,

if canCast == True:
    hero.cast("chain-lightning", enemy)

You need a hero.removeFlag(flagBlack) otherwise your flag will stay there.

good call…

Fourthly: This is just a tip for better code, I find using hero.move(item.pos) is better than MoveXY and maybe you could do a bestCoin array to find a bestCoin.

Why does it work better?

Fifthly: another tip you’re only collecting coins if there’s also an enemy maybe also add an else at the end and an if item hero.move(item.pos) just if there dead so you don’t just wait there.

Also great advice. One of my elses was indented too far - disastrous effect as described by you.

1 Like

One last thing, when I place a green flag, I want my hero to immediately stop what he’s doing and go pickup the flag. Instead, he completes his current action (e.g. collect a faraway coin) and starts moving to the flag only after.
How can I fix that?

if flagGreen:
        hero.pickUpFlag(flagGreen)

is my first if-statement after defining the variables, so it should get the highest priority, right?

Umm… I’ve never been sure of that myself, Maybe using Move(item.pos) instead of MoveXY if you aren’t, because move(item.pos) can stop at a specific time, explained in a lot more detail in the first levels of the mountain, I think and answering your question in your other reply as well about why move(item.pos) is better, if the enemy gets the coin before you your hero will stop and go to another coin because you’re targeting the coin’s pos not the actual XY coordinates it was at at the time of calling it, and if you’re using bestCoin if you find another bestCoin you will change direction and get that coin. But about the flag again I’m not entirely sure, I haven’t had a huge amount of experience with flags myself, and I’ve encountered the problem you described where the hero won’t stop doing something else. i think that’s all I know about that. :grin:

1 Like