{Backwoods Forest} - Help with Friend and foes

Hi all,

Level objective : Orges must die. I don’t see any !
Code says : “Now find the nearest enemy and tell them to go away.” There are no enemies !

Appreciate your help.

Could you give some screenshots? And it can help if you show what you see in dev-console for your browser.

Sure, I post image.
Sorry, I don’t understand " dev-console ".

Thanks for your help.

Ok. The blue guys are ogres. Use findNearestEnemy and use their names (id) to say them “go away”.

I got it. Thanks a lot :slight_smile:

Could one of you guys please explain the code you used. When I use mine(below) the peasants all get ready for battle and then the hero never says anything to the peons.

while True:
friend = hero.findNearestFriend()
enemy = hero.findNearestEnemy()
if friend:
hero.say("To battle, " + friend.id + “!”)
# Now find the nearest enemy and tell them to go away.
if enemy:
hero.say(“Go away,” + “enemy.id”)

Could you format your code (use triple ` to wrap it)? Maybe the problem is indents, but it’s hard to say while your code example look like a text.

Oh, I see the error. Two errors.

First, try to remove else. Because if your hero sees a friend, then s/he says “To Battle” and “else” parts doesn’t work. Don’t forget about indents.

Second: You use
hero.say("Go away," + "enemy.id") and as the result "enemy.id" is a string and your hero say "Go away,enemy.id". Use enemy.id without quotes (")

while True:
    friend = hero.findNearestFriend()
    enemy = hero.findNearestEnemy()
    if friend:
        hero.say("To battle, " + friend.id + "!")
        if enemy:
            hero.say("Go away," + enemy.id)

Okay so I fixed the enemy thing. What would I substitute in for else? Another if?

1 Like

Solved it. I completely removed the else. I still don’t understand why it would read the “if enemy:” clause because the “if friend:” clause was before and it isn’t like the friends were no longer visible.

Thank You.

Lemme know if you or anyone else can explain my question above.

Why do you think so? Peasants transformed in soldiers but they are still “friends”.

What I meant is that the “if friend:” clause would infinitely repeat itself right, because it would keep seeing the friends and therefore never get a chance to see if there were any enemies.

Yes. When you removed “else”, you solved that problem.

Thanks for helping me! I had the same issue as you and when i removed the else it worked! :grin:

Please do not revive dead threads. This one has been left alone for 11 months.
If you discover a post that seems interesting or helpful to you, you can just use the Like (:heart:) button at the bottom right hand corner of the post. You can reply if the thread is not dead as that avoids any unnecessary returning of the topic to the top of the list, but if you have any related issues that have not already been mentioned, you can post even if the thread is dead. (This is from my experience, but things could be different.)

Sorry…I won’t try to do this again! :pensive:

I am so sorry for reviving this chat but i need help

--# Peasants and peons are gathering in the forest.
--# Command the peasants to battle and the peons to go away!
while true do
    local friend = self:findNearestFriend()
    if friend then
        self:say("To battle, " + friend.id + "!")
--    # Now find the nearest enemy and tell them to go away.
local enemy = hero:findNearestEnemy()
if enemy then
    hero:say("Go away, " + enemy.id + "!")

the hero just does the to battle part without saying go away

I use Python and I’m not familiar with the language you’re using but it looks like a structure problem. It appears that the second half of your code is not inside the while true loop so it will never iterate through the array of enemies.

1 Like

I think that is on purpose as it says to just find a single enemy, not all enemies

1 Like

I stand corrected. Oops. Apparently it does want you to call all enemies and friends

1 Like