Understanding parts of the function Blackwoods Fork

Hello everybody,

first of all I want to say that I do not need help to solve the level.

It’s just that I don’t understand the logic behind the introduced and hinted function in the level Blackwoods Fork.

So the description when you start the level and also the hints page say:

# This function has 1 parameter: 'target':
def checkAndAttack(target):
    # 'target' is a predefined variable.
    if(target):
        hero.attack(target)

I see the

def checkAndAttack(target):

like a tool, lets say a screwdriver.
The function it self

checkAndAttack():

is the handle. I determine if it’s green or red etc.

The parameter on the other hand, the text inside the ( and )

target

is the tip of the screwdriver and I determine if it’s a Philip’s or a flat-tip one.

So far so good.

Now in the example/hint we predefine the ‘target’ variable as

if(target):
    hero.attack(target)

And this is what I don’t understand.

We say

hero.attack(target)

IS the meaning of ‘target’ (the variable) but with ‘(target)’ we crate something that makes no sense.

To me this says somethinge like “Tomato is Tomato”. This describes not WHAT a Tomato is.
A description of Tomato would be more like “A tomato is the fruit of a plant that contains its seeds and various nutrients, as well as water.”

So how does the game know the difference between

checkAndAttack(**target**):

and

hero.attack(**target**)

and why doesn’t create this an infinit loop or something?

I hope this makes any sence to you and somebody understands the way I think and why I’m curious about that :smiley:

Cheers
lynx85

1 Like

Hi Lynx85. Welcome to the CodeCombat discourse.
How I like to think about arguments (an argument is something that goes within the brackets of a function. In your case checkAndAttack() has one “argument”, which is target.) is by imagining them more as place holders, rather that “predefined variables” as it calls them in the comments of the level.

def say(message): # here the argument of the say function is "message"
    hero.say(message) # this will say whatever is entered into the brackets of the function when it is called.

# something that's very important to learn about arguments, and functions in general, >
# >is that what you put in the brackets of the function when you call it, doesn't have to be the same as what you used as a place holder when writing the function:

whatIWantToSay = "Hello @Lynx85" # this is storing the string "Hello @lynx85" in the variable whatIWantToSay
# now I can call the say function:
say(whatIWantToSay) # your hero would say:

~~~"Hello @Lynx85"

Another example:
def cleaveWhenReady(ogre):
    if hero.isReady("cleave"):
        hero.cleave(ogre)
enemy = hero.findNearestEnemy()
cleaveWhenReady(enemy) # imagine what would happen if you called this when there weren't any enemies?
# you would get an error saying something like: "fix your code: which ogre? (use if)" and would highlight this area:
~~hero.cleave(ogre)~~

# what I'm trying to say is that when you run a function, if there's a problem in your function concerning the argument it still comes up.

I hope this helps,
:lion: :lion: :lion:

2 Likes

Hi @lynx85
I am guessing that The check and attack function checks before it attacks.
so they would constantly check if there is a enemy.
It is like a Infinite loop but and if function to it.
Hope this helps :grinning:

1 Like