[SOLVED] Ring Bearer/Hunter and Prey "command" doesn't work

I have been struggling mightily today with the hero.command(…) method. I keep getting "argument error: Hero placeholder needs something to command.

Here’s my code for the simpler one, Ring Bearer. (I tried to separate the archers and soldiers into separate arrays for Hunter and Prey, so it’s even more confusing.)

What am I missing in terms of issuing commands?

function findSoldierOffset(soldiers, i) {
    var soldier = soldiers[i];
    var angle = i * 360 / soldiers.length;
    return radialToCartesian(5, angle);

// This function does the math to determine the offset a soldier should stand at.
function radialToCartesian(radius, degrees) {
    var radians = Math.PI / 180 * degrees;
    var xOffset = radius * Math.cos(radians);
    var yOffset = radius * Math.sin(radians);
    return {x: xOffset, y: yOffset};

var peasant = hero.findByType("peasant")[0];
while(true) {
    var soldiers = hero.findByType("soldier");
    for (var i=0; i < soldiers.length; i++);
        var soldier = soldiers[i];
        var offset = findSoldierOffset(soldiers, i);
        var XOffset = xOffset;
        var YOffset = yOffset;
        // Command the soldier to move to the new offset position.
        hero.command(soldier, "move", {x: peasant.pos.x + XOffset, y: peasant.pos.y + YOffset});
    // The hero should keep pace with the peasant!
    hero.move({x: hero.pos.x + 0.2, y: hero.pos.y});

Maybe you should check if the soldier exist before commanding it.

I can try that, but I don’t understand why they wouldn’t “exist”. There are soldiers provided as each scenario opens - indeed in Ring Bearer, I don’t even summon soldiers- just use the ones provided.

I define them in the first couple of lines of the while true loop, before giving any instructions.

Update, I tried “if (soldiers.length > 0)” immediately after defining var soldiers, and it didn’t fix the problem.

That said, I really appreciate the ideas to help. If you have any others, please don’t hesitate to share them. :slight_smile:


Sometimes the game just wants you to put in

if (your var) {

(im not really good at js :frowning: )

OH! I got it. When I get that issue, I realize I forgot to define the soldiers.

ive never been more confused

It’s ok. I actually know what the issue was, but I ended up referencing some other code in order to be able to just move on with it.

Thanks for all the help. :wink:


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