[SOLVED] Cloudrip Commander Assistance

# Summon some soldiers, then direct them to your base.

# Each soldier costs 20 gold.
while hero.gold > hero.costOf("soldier"):
    hero.summon("soldier")
    
soldiers = hero.findFriends()
soldierIndex = 0
# Add a while loop to command all the soldiers.
while True:
    soldier = soldiers[soldierIndex]
    hero.command(soldier, "move", {"x": 50, "y": 40})

# Go join your comrades!
hero.moveXY(51, 41)

Using this code, my hero moves only one soldier and then nothing happens, how do I fix this?

It shouldn’t be this way, instead, you need to loop over every soldier in your soldiers list.

hint:

while soldierIndex < len(soldiers):
     #Execute code
1 Like

What does the “len” mean?
edit: i’ve put in the command where the while loop is and my game just freezes

while hero.gold > hero.costOf("soldier"):
    hero.summon("soldier")
    
soldiers = hero.findFriends()
soldierIndex = 0
# Add a while loop to command all the soldiers.
while soldierIndex < len(soldiers):
    soldier = soldiers[soldierIndex]
    hero.command(soldier, "move", {"x": 50, "y": 40})

# Go join your comrades!
hero.moveXY(51, 41)

Is this where it needs to go? I feel like is isn’t since my game just freezes when I run it.
Tbh, i haven’t played the game for like 2 years so I have no clue what i’m doing

length, so how many indexes there are in the list.

Also, you should increment soldierIndex inside the while loop, otherwise it will just stay as this way soldierIndex += 1

You are trying to loop over the list of the soldiers you have over here, so you command soldier after soldier in this loop.

This line says, as long as soldierIndex is less than the length of the list of soldiers you have, so for instance, you summoned 5 soldiers, so as long as soldierIndex is less than 5, do something

Now this something you are trying to do, is specifying who do you want to command, the first time the while loop runs, soldierIndex equals to 0, and let’s say there are 5 soldiers, so soldier is the number or index that we currently have, which is 0 at the moment inside the list of soldiers. so soldier is 0th index in the soldiers list, as for the beginning, then you are commanding this soldier that you defined to move to a certain XY position.

Now the line that I told you to add of incrementing soldierIndex changes which soldier you are commanding, so when you add 1 to soldierIndex, the soldier that you are commanding changes. So now that we added one to soldierIndex, it becomes 1, so soldier becomes the 1st index in the soldiers list. So now you are commanding the second soldier in you list to move to an XY position. And so on the process continues until soldierIndex equals to the number of indexes in the soldiers list, which is in this example 5, so here the while loop breaks and you move on with your code.

Note: indexes in Python start at 0 not 1

And as for this yes, its place it fine.

Hope this helps a bit to clear things out. :slight_smile:

1 Like

It is isn’t it?

while solderIndex < len(soldiers):

If you mean put it inside the loop then what do I put for while ?

Inside it I meant under it and indented as well

while soldierIndex < len(soldiers):
    soldier = soldiers[soldierIndex]
    hero.command(soldier, "move", {"x": 50, "y": 40})
    #Add it here
1 Like

while soldierIndex < len(soldiers):
    soldier = soldiers[soldierIndex]
    hero.command(soldier, "move", {"x": 50, "y": 40})
    soldierIndex < len(soldiers)

Like this?

No you need to put the incrementing there.

Hint:

while soldierIndex < len(soldiers):
    soldier = soldiers[soldierIndex]
    hero.command(soldier, "move", {"x": 50, "y": 40})
    soldierIndex += 1
1 Like

Oh ok, i thought the increment was the 4 spaces after the loop, sorry

Fixed, thanks for your help!

1 Like

No problem (20chars)

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