Hi, I’m not sure what’s wrong with my code. This code allows my character to summon 1 soldier, but he doesn’t do anything. What’s more, an error pops up. Error: ‘ArgumentError: command’s argument minion should have type unit, but got null’
Code:
while(true) {
// Move to the nearest coin.
// Use move instead of moveXY so you can command constantly.
var coin = hero.findNearestItem();
hero.move(coin.pos);
hero.say("I need coins!");
// If you have funds for a soldier, summon one.
if (hero.gold > hero.costOf("soldier")) {
hero.say("I should summon something here!");
}
var enemy = hero.findNearest(hero.findEnemies());
if (enemy) {
var soldiers = hero.findFriends();
var soldierIndex = 0;
var soldier = soldiers[soldierIndex];
// Loop over all your soldiers and order them to attack.
while (soldierIndex < 10) {
hero.summon("soldier");
hero.command(soldier, "attack", enemy);
}
// Use the 'attack' command to make your soldiers attack.
//hero.command(soldier, "attack", enemy);
}
}
Hi! You need to summon a soldier in a different place from where your doing it at the moment. At the moment your summoning a soldier when you don’t know if you have enough money to summon it. Look at this line:
At the moment your commanding a soldier that you defined before you summoned it. So when you defined the variable soldier, you didn’t have any, meaning it’s contents is null.
You also need to increment (increase by one) soldierIndex at the end of your while loop so that you command more than one soldier.
Danny
Hmm… I did what you said, but I still got the same results Code:
// Gather coins to summon soldiers and have them attack the enemy.
while(true) {
// Move to the nearest coin.
// Use move instead of moveXY so you can command constantly.
var coin = hero.findNearestItem();
hero.move(coin.pos);
hero.say("I need coins!");
// If you have funds for a soldier, summon one.
if (hero.gold > hero.costOf("soldier")) {
hero.say("I should summon something here!");
hero.summon("soldier");
}
var enemy = hero.findNearest(hero.findEnemies());
if (enemy) {
var soldiers = hero.findFriends();
var soldierIndex = 0;
var soldier = soldiers[soldierIndex];
// Loop over all your soldiers and order them to attack.
while (soldierIndex < 10) {
hero.command(soldier, "attack", enemy);
soldierIndex += 1;
}
// Use the 'attack' command to make your soldiers attack.
//hero.command(soldier, "attack", enemy);
}
}
Ok, there are still a few errors:
Firstly, you’re not defining soldier inside the while loop as well as outside it. The while loop only loops the code inside it, meaning soldier will always stay the same, if you increment soldierIndex or not.
Next:
Why < 10? Do you know how many soldiers you have? Trying to command a soldier that you defined using a soldierIndex higher than the number of soldiers you actually have will cause an error. (array.length returns the length of an array)
You’ll also have to remove the hero.say()s as they slow down your code significantly.
Danny
var soldier = hero.findFriends();
while(true) {
// Move to the nearest coin.
// Use move instead of moveXY so you can command constantly.
var coin = hero.findNearestItem();
hero.move(coin.pos);
// If you have funds for a soldier, summon one.
if (hero.gold > hero.costOf("soldier")) {
hero.summon("soldier");
}
var enemy = hero.findNearest(hero.findEnemies());
if (enemy) {
var soldiers = hero.findFriends();
var soldierIndex = 0;
var soldier = soldiers[soldierIndex];
// Loop over all your soldiers and order them to attack.
while (soldierIndex < soldiers) {
hero.command(soldier, "attack", enemy);
soldierIndex += 1;
}
// Use the 'attack' command to make your soldiers attack.
//hero.command(soldier, "attack", enemy);
}
}
My character collects all the coins and buys (or hires ) soldiers, but they don’t attack; so I end up getting killed.
Can someone help me with my code here i will show it
while True:
coin = hero.findNearestItem()
# Move to the nearest coin.
# Use move instead of moveXY so you can command constantly.
hero.move(coin.Pos)
hero.say("I need coins!")
# If you have funds for a soldier, summon one.
if hero.gold > hero.costOf("soldier"):
hero.say("I should summon something here!")
enemy = hero.findNearest(hero.findEnemies())
if enemy:
soldiers = hero.findFriends()
soldierIndex = 0
soldier = soldiers[soldierIndex]
# Loop over all your soldiers and order them to attack.
if soldier:
# Use the 'attack' command to make your soldiers attack.
#hero.command(soldier, "attack", enemy)zz
hero.command(soldier, "attack", enemy)
soldierIndex+=1