Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

Codecombat - Double Gaps

Heres my code, I’m don’t quite understand the something part

# Get the hero and the peasant to the south.

# The function move your hero down along the center line.
def moveDownCenter():
    x = 40
    y = hero.pos.y - 12
    hero.moveXY(x, y)

# The function build a fence on the right of something.
def buildRightOf(something):
    # buildXY a "fence" 20 meters to something's right.
    hero.buildXY("fence", something.x+20, something.y)
    pass

# The function build a fence on the left of something.
def buildLeftOf(something):
    # buildXY a "fence" 20 meters to something's left.
    hero.buildXY("fence", something.x-20, something.y)
    pass

while True:
    ogre = hero.findNearestEnemy()
    coin = hero.findNearestItem()
    if ogre:
        buildLeftOf(ogre)
    if coin:
        buildRightOf(coin)
    moveDownCenter()

with the something.x / something.y
have you tried putting something.pos.x instead?

1 Like

@Deadly_Rice0769 is correct.

To explain what is happening with the function. In this case something is a variable that is only used within this function. It can have any name, but needs to be used throughout the function consistently

def buildRightOf(variable_for_function):
    # buildXY a "fence" 20 meters to something's right.
    hero.buildXY("fence", variable_for_function.pos.x + 20, variable_for_function.pos.y)
    pass

When you call the function in your main code, you will add the variable that you want to pass into the function.

ogre = hero.findNearestEnemy()
buildLeftOf(ogre)

Essentially, something = ogre in the function and takes the attributes and properties of the variable passed in to evaluate inside the function. What makes functions helpful, is you can pass any variable into the function which is why it will have it’s own local variable name.

coin = hero.findNearestItem()
buildLeftOf(coin) # now it will build left of the coin instead of ogre