The overview should say:
It’s time to fill in the body of a function and use it to make your code nice and clean!
Remember that a parameter
is just a way of passing information into a standalone function. It is a predefined variable containing whatever was when between the ()
when the function was called
.
‘Calling’ a function is when the code is actually peformed. See the examples below as to how functions are called:
-- This is 'defining' a function:
local moveUpAndDown = function()
hero:moveUp() -- This is calling the 'moveUp' function.
hero:moveDown() -- This is calling the 'moveDown' function.
end
hero:say("I'm saying something!") --This is 'calling' the 'say' method.
moveUpAndDown() -- This is 'calling' the custom defined 'moveUpAndDown' method.
Just use the target like you would any variable!
local checkAndDefend = function(target)
if target then
hero:say("I see an enemy! I should beat them up.")
end
end
The intro should say
Remember that a parameter
is a way of passing information into a function. It is a predefined variable of whatever the argument is when called!
local checkAndEat = function(target)
if target.type == "fruit" then
hero:eat(target)
else
hero:toss(target)
end
end
while true do
hero:moveUp()
local nearestTree = hero:findNearestTree()
local food = hero:harvest(nearestTree)
checkAndEat(food)
end
And the following default code block should have been provided:
-- A forgotten tomb in the forest!
-- But the ogres are hot on your heels.
-- Break open the tomb, while defending yourself from the munchkins.
-- This function should attack an enemy if it exists, otherwise attack the door!
local checkToDefend = function(target)
-- Check if the target exists
-- If so, attack the target
-- Use an else to do something if there is no target
-- Otherwise attack the "Door"
end
while true do
local enemy = hero:findNearestEnemy()
checkToDefend(enemy)
end
does this help now?