I don't understand how the soldiersIndex variable works

I want to know how the soldiersIndex variable works. I think that if I understand it, it will help me in the next couple of levels

hey, do you mind if you post the code that you want to make better? thank you

1 Like

I don’t really have code that

I just want to understand what it means in the cloudrip areas

I don’t know how to command all my soldiers at the same time.

image

hmm, it’s been a while since I used python but you need to use the for loop something like this:

soldiers = hero.findNearest(hero.findNearestFriends())
for soldier in soldiers:
enemy = soldier.findNearestEnemy()
    hero.command(soldier, action placeholder(move,attack,defend), "x coordinate,y 
    coordinate" or "enemy")

and put all of that into a loop(my python is a bit rusty as I started javascript some time ago so there might be some errors.).

What cheddarcheese said is right and you will be using it after a few levels in the mountain. But if you didn’t reach that yet, you probably are still using the while loop.

Details about while-loop
  • When you make a variable that holds a list, for example, soldiers = hero.findByType("soldier"), each and every soldier in that list has an index. In python, indexes start at 0.
  • After you have your list, they ask you to make soldiersIndex = 0
    This will act as a cursor or a pointer on what index you are dealing with.
  • After you have 2 variables (the list and soldiersIndex) you create a while loop that goes this way:
while soldiersIndex < len(soldiers):

This is technically telling you, as long as the value of soldiersIndex is less than how many indexes the list of soldiers have, do this.

  • Onwards, inside the while loop that first you should do is define your soldier, the unit you want to command. How do you get that? You say: soldier = soldiers[soldiersIndex] which really means that soldier is the unit in the list that hold the index of the same value of soldiersIndex. So if soldiersIndex equals to 1, then the unit you are targeting is the 2nd unit (because python starts with 0) in the list you have.
  • Now you know who are you dealing with, you put in the lines of code you want, for example commanding or checking health, based on what you need.
  • The last and most important thing to do so you wouldn’t get an infinite loop is to increment soldiersIndex this way: soldiersIndex += 1 So this thing, every time you do all the commands inside the while loop, so it can know if it wants to exit the loop or continue, it adds 1 to soldiersIndex.

A small example:

soldiers = hero.findByType("soldier")
soldiersIndex = 0
while soldiersIndex < len(soldiers):
    soldier = soldiers[soldiersIndex]
    if soldier:
        hero.command(soldier, "move", {"x": 3, "y":4}
    soldiersIndex += 1

At some point, the soldiersIndex will equal the length of soldiers, or the amount of indexes it has, so the code will break out of the while loop.

Hope this explanation helps :slight_smile:

1 Like

ok thank you so much

You’re welcome :slight_smile:

Would you mark solution so the topic would close please?

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