# If the peasant is damaged, the flowers will shrink!
def summonSoldiers():
if hero.gold >= hero.costOf("soldier"):
hero.summon("soldier")
# Define the function: commandSoldiers
def commandSoldiers():
friends = hero.findByType("solider")
for friend in friends:
enemy = friend.findNearestEnemy()
if enemy:
hero.command(friend, "attack", enemy)
# Define the function: pickUpNearestCoin
def pickUpNearestCoin():
coin = hero.findNearestItem()
if coin:
hero.move(coin.pos)
while True:
summonSoldiers()
commandSoldiers()
pickUpNearestCoin()
soldiers are not moving? what have I missed? thanks
This is my code:
// If the peasant is damaged, the flowers will shrink!
function summonSoldiers() {
if (hero.gold >= hero.costOf(āsoldierā)) {
hero.summon(āsoldierā);
}
}
// Define the function: commandSoldiers
function commandSoldiers() {
var friends = hero.findByType(āsoldierā);
for (var i = 0; i < friends.length; i++) {
var enemy = hero.findNearestEnemy();
if (enemy) {
hero.command(enemy, āattackā, enemy);
}
}
}
// Define the function: pickUpNearestCoin
function pickUpNearestCoin() {
var coin = hero.findNearestItem();
hero.move(coin.pos);
}
var peasant = hero.findByType(āpeasantā)[0];
while(true) {
summonSoldiers();
commandSoldiers();
pickUpNearestCoin();
}
Whenever I run the code I get and argument error. Can I get some help?
}```
// If the peasant is damaged, the flowers will shrink!
function summonSoldiers() {
if (hero.gold >= hero.costOf("soldier")) {
hero.summon("soldier");
}
}
// Define the function: commandSoldiers
function commandSoldiers() {
var friends = hero.findByType("soldier");
for (var i = 0; i < friends.length; i++) {
var enemy = hero.findNearestEnemy();
if (enemy) {
hero.command(enemy, "attack", enemy);
}
}
}
// Define the function: pickUpNearestCoin
function pickUpNearestCoin() {
var coin = hero.findNearestItem();
hero.move(coin.pos);
}
var peasant = hero.findByType("peasant")[0];
while(true) {
summonSoldiers();
commandSoldiers();
pickUpNearestCoin();
}
function summonSoldiers() {
if (hero.gold >= hero.costOf("soldier")) {
hero.summon("soldier");
}
}
// Define the function: commandSoldiers
function commandSoldiers() {
var friends = hero.findByType("soldier");
for (var i = 0; i < friends.length; i++) {
var enemy = hero.findNearestEnemy();
if (enemy) {
hero.command(friend, "attack", enemy);
}
}
}
// Define the function: pickUpNearestCoin
function pickUpNearestCoin() {
var coin = hero.findNearestItem();
if (coin) {
hero.move(coin.pos);
}
}
var peasant = hero.findByType("peasant")[0];
while(true) {
summonSoldiers();
commandSoldiers();
pickUpNearestCoin();
}
You are likely getting a āvariable not defined errorāā¦āfriendā is not defined. However, you do have the array āfriendsā, which is the list of all your identified friends. You just need to state which āfriendsā you are commanding.
You are iterating thru your friends array, using the āforā loop. Each time the loop cycles, āiā increments by oneā¦āiā is the counter (or pointer); it indicates which element of the array is currently being examined. Therefore, friends[0] would point to the first friend in the list, friends[7] would point to the 8th, etc etc.
In your command statement, simply state which of your friends you are commanding.
You are on track, but not quite thereā¦using [0] means you will always be commanding friends[0] and never any of the others. Instead, try using your counterā¦remember, the loop is constantly changing, as it cycles through the list of friends.