Ring Bearer trouble


#1

This is my code for Ring Bearer:

# You must escort a powerful magical ring back to town to be studied.
# The goal is to escape, not fight. More ogres lurk in the surrounding mountains!
# Make a circle of soldiers around the peasant!
# We give you two functions to help with this:

# findSoldierOffset figures out the position a soldier should stand at in relation to the peasant.
# The first argument 'soldiers' should be an array of your soldiers.
# The second argument 'i' is the index of the soldier (in soldiers) you want to find the position for.
def findSoldierOffset(soldiers, i):
    soldier = soldiers[i]
    angle = i * 360 / len(soldiers)
    return radialToCartesian(5, angle)

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

peasant = self.findByType("peasant")[0]

self.findByType("soldier")
loop:
    for soldier in soldier:
        def findSoldierOffset(soldiers, i):
        soldier = soldiers[i]
        angle = i * 360 / len(soldiers)
        return radialToCartesian(5, angle)
        moveTo = {x: peasant.pos.x + offset.x, y: peasant.pos.y + offset.y}
        self.command(soldier, "move", moveTo)

    # The hero should keep pace with the peasant!
        self.move({"x": self.pos.x + .3, "y": self.pos.y})

what am i doing wrong? the soldiers don’t move until Hector’s dead.


#2

You should call the method findSoldierOffset, not define it. Also it is easier to use a index in a for-loop (the way your rest is coded.

change

for soldier in soldier:
        def findSoldierOffset(soldiers, i):
        soldier = soldiers[i]
        angle = i * 360 / len(soldiers)
        return radialToCartesian(5, angle)

to

for i in range(0, len(soldiers)):
        offset = findSoldierOffset(soldiers, i)

#3
self.findByType("soldier")      # 1
loop:
    for soldier in soldier:          # 2
  1. you call findByType but never store it in anything (?soldiers?)
  2. for “single guy” in “single guy” (soldier in ?soldiers?)

#4

same thing happened

# You must escort a powerful magical ring back to town to be studied.
# The goal is to escape, not fight. More ogres lurk in the surrounding mountains!
# Make a circle of soldiers around the peasant!
# We give you two functions to help with this:

# findSoldierOffset figures out the position a soldier should stand at in relation to the peasant.
# The first argument 'soldiers' should be an array of your soldiers.
# The second argument 'i' is the index of the soldier (in soldiers) you want to find the position for.
def findSoldierOffset(soldiers, i):
    soldier = soldiers[i]
    angle = i * 360 / len(soldiers)
    return radialToCartesian(5, angle)

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

peasant = self.findByType("peasant")[0]

self.findByType("soldier")
loop:
    for i in range(0, len(soldiers)):
        offset = findSoldierOffset(soldiers, i)
        moveTo = {x: peasant.pos.x + offset.x, y: peasant.pos.y + offset.y}
        self.command(soldier, "move", moveTo)

    # The hero should keep pace with the peasant!
    self.move({"x": self.pos.x + .3, "y": self.pos.y})

#5

you didn’t do my fix, meaning #1


#6

same thing

# You must escort a powerful magical ring back to town to be studied.
# The goal is to escape, not fight. More ogres lurk in the surrounding mountains!
# Make a circle of soldiers around the peasant!
# We give you two functions to help with this:

# findSoldierOffset figures out the position a soldier should stand at in relation to the peasant.
# The first argument 'soldiers' should be an array of your soldiers.
# The second argument 'i' is the index of the soldier (in soldiers) you want to find the position for.
def findSoldierOffset(soldiers, i):
    soldier = soldiers[i]
    angle = i * 360 / len(soldiers)
    return radialToCartesian(5, angle)

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

peasant = self.findByType("peasant")[0]
loop:
    self.findByType("soldier")
    for soldier in soldier: 
        offset = findSoldierOffset(soldiers, i)
        moveTo = {x: peasant.pos.x + offset.x, y: peasant.pos.y + offset.y}
        self.command(soldier, "move", moveTo)

    # The hero should keep pace with the peasant!
    self.move({"x": self.pos.x + .3, "y": self.pos.y})

#7

While it makes sense to store the peasant, what Vlevo actually ment is storing the soldiers in a variable.

Als you broke the for-loop again.


And your move commands on the last line are not inside the loop.


#8

Error in Help for this Level

To calculate where the soldier should move to, you add the offset to the peasant’s position like this:

moveTo = {x: peasant.pos.x + offset.x, y: peasant.pos.y + offset.y}

command(soldier, “move”, moveTo)

Correct is
moveTo = {“x”: peasant.pos.x + offset.x, “y”: peasant.pos.y + offset.y}


#9

Got it, thanks! In an ideal world, we’d split up codeLanguage and spokenLanguage in the guides, too, so we could get any code snippets right in all languages, but it adds a lot of complication to the translations and the code for that, so we have been trying to make fairly code-language-ambiguous guides so far.


#10
    soldiers = this.findByType("soldiers");
// Use findByType to get an array of your soldiers.
loop {
    // Use a for-loop to iterate over your array of soldiers.
for(i=0;i<soldiers.length;i++){

offset = this.findSoldierOffset(soldiers,i);
offset.x = offset.x +peasant.pos.x;
offset.y = offset.y +peasant.pos.y;
this.command(soldiers[i], "move",offset);
    
}

Where is the problem?


#11

sorry, you need to be more specific.

What error are you getting? What is it doing wrong? What is it not doing that you think it should? Is that ALL the code? if not then, pfft…


#12

I put in the code. Me and Hector start to move. Hector dies, THEN soldiers move.


#13
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 = this.findByType("peasant");
soldiers = this.findByType("soldiers");
// Use findByType to get an array of your soldiers.
loop {
    // Use a for-loop to iterate over your array of soldiers.
for(i=0;i<soldiers.length;i++){

offset = this.findSoldierOffset(soldiers,i);
this.say(offset);
offset.x = offset.x +peasant.pos.x;
offset.y = offset.y +peasant.pos.y;
this.command(soldiers[i], "move",offset);
    
}
    // Find the offset for a soldier.
    // Add the offset.x and offset.y to the peasant's pos.x and pos.y.
    // Command the soldier to move to the new offset position.

    // The hero should keep pace with the peasant!
    this.move({x: this.pos.x + 0.2, y: this.pos.y});
}

This is the whole code and pikanaw description is also correct.


#14

Hmm, could it be the say? Don’t use say here after you’re done using it to debug. It takes one second, so your code will be delayed instead of running every frame.


#15

yeah I know it.

The problem was soldiers = this.findByType(“soldiers”); :smile:

Some debugging to find the point of error helped me…