Here is my code:
loop:
coin = self.findNearest(self.findItems())
enemy = self.findNearest(self.findEnemies())
friends = self.findFriends()
if coin:
self.move(coin.pos)
if self.gold >=20:
self.summon("soldier")
for friend in friends:
if friend.type is "soldier":
self.command(friend, "move", {'x':58, 'y':43})
elif enemy:
if friends:
for friend in friends:
if friend.type is "soldier":
self.command(friend, "attack", enemy)
However, for some reason, when I command my soldiers to move, I am only able to command the ones that I summoned perviously. For example, say I summon one soldier. He is not commanded. I collect gold, then summon another soldier. When the command is used again, only the first soldier that I summoned obeys, and so on. What am I doing wrong?
The explanation and solution is at the end. You should also do something when you find no coin!
Letâs play your code through:
Current situation:
You (Whatever hero, doesn't matter)
Helgen (Soldier)
Sofia (Soldier)
Gold : 20
----------------------------------------------------
No coin ->
Do nothing... This really should not happen...
----------------------------------------------------
We found a coin!
We found no enemy!
We got all friends ( == [Helgen, Sofia] )
Move to the coin.
We have more than 20 Gold...
Summon Kira (new soldier)
For every friend in friends ( == [Helgen, Sofia] ) order them to move.
As you can see, your problem is that you donât update friends
after you summon a soldier, and you ever only command your soldiers to move to (58, 43)
after you summoned a soldier. Conclusion: The newest soldier will not move until you spawn a new one.
Solve this by putting friends = self.findFriends()
just before the for-loop.
1 Like
Please post the correct code so I can see what is different 
@NeedHelpQuick Giving away correct code is frowned upon, as copy pasting isnât a skill that requieres a lot of thoughts. If you have specific question, please formulate it.
The âgimme da anserâ attitude doesnât trigger much altruism in people. At least not in me.
Sorry I just need help on how to use for loops
Still amazingly vague. Whatâs the code youâre using, what do you understand, what canât you make working ? What language ?
Here is some informations about for loops in JavaScript.
sorry posted on the wrong level!
loop:
# Collect gold.
golds = self.findItems()
gold = self.findNearest( golds )
self.moveXY( gold.pos.x , gold.pos.y)
# If you have enough gold, summon a soldier.
if self.gold > self.costOf("soldier"):
self.summon("soldier")
# Use a for-loop to command each soldier.
# For loops have two parts: "for X in Y"
# Y is the array to loop over.
# The loop will run once for each item in Y, with X set to the current item.
for friend in self.findFriends():
if friend.type == "soldier":
enemy = friend.findNearestEnemy()
# If there's an enemy, command her to attack.
if enemy:
self.command( friend, "attack", enemy )
# Otherwise, move her to the right side of the map.
else:
pos = {'x':82, 'y':33}
self.command( friend, "move", pos )
But sometimes it doesn`t work, because solider cannot get the position of {âxâ:82, âyâ:33} before the enemy appear & attack the peasant.
I think it is random when the ogre appear.
Itâs not quite random. If your soldier canât move there in time, have her move a shorter distance. That should fix the problem.
while(true) {
var coin = hero.findNearestItem();
hero.moveXY(coin.pos.x, coin.pos.y);
if (hero.gold >= hero.costOf("soldier")){
hero.summon("soldier");
var friends = hero.findFriends();
for(var friendIndex = 0; friendIndex < friends.length; friendIndex++) {
var friend = friends[friendIndex];
if(friend.type == "soldier") {
var enemy = friend.findNearestEnemy();
// If there's an enemy, command her to attack.
// Otherwise, move her to the right side of the map.
if (enemy) {
hero.command(soldier, "attack", enemy);
}
else {
hero.command(friend, "move", {x: 76, y: 47});
}
}
}
}
}
For some reason its saying âsoldierâ is undefined when I command a soldier to attack.
(This is Javaascript)
Itâs because you havenât checked whether there is a soldier, so it doesnât know who youâre trying to command because itâs chosen a specific index of an empty array (friends).
Also you indentation is not correct, am I right in thinking that that problem arose when posting?
I believe it should be:
while(true) {
var coin = hero.findNearestItem();
hero.moveXY(coin.pos.x, coin.pos.y);
if (hero.gold >= hero.costOf("soldier")){
hero.summon("soldier");
}
var friends = hero.findFriends();
for(var friendIndex = 0; friendIndex < friends.length; friendIndex++) {
var friend = friends[friendIndex];
if(friend.type == "soldier") {
var enemy = friend.findNearestEnemy();
// If there is an enemy, command her to attack.
// Otherwise, move her to the right side of the map.
if (enemy) {
hero.command(soldier, "attack", enemy);
}
else {
hero.command(friend, "move", {x: 76, y: 47});
}
}
}
}
Make sure that in future you post your code as you have it in the level otherwise it can lead to people thinking your code has problems which it doesnât.
Thanks

Double check to see which variable you are using to command. You canât command a soldier, because you donât have a variable named soldier in your code. Your second command will give you the answer.
Also, reconsider what type of move command you are using for collecting your coin. MoveXY
prevents you from commanding your friends until you pick up the coin, while move
allows you to command your friends every step.
1 Like