Play Home Contribute GitHub Join Us Discourse Staff Members GitHub Contribution Guides Team

LUA patches for pet levels in forest


Go Fetch

-- Youve been caught in a burl trap!
-- Send your pet to fetch the health potions!

local goFetch = function()
    -- You can use loops in a handler function.
    while true do
        local potion = hero:findNearestItem()
        if potion then
            -- Use pet.fetch() to have your pet fetch a potion:

-- When your pet is summoned, it triggers a "spawn" event.
-- This tells your pet to run goFetch() at the start of the level
pet:on("spawn", goFetch)


Your pet knows how to fetch items!

local item = hero:findNearestItem();
if item then


Buddy's Name

```lua -- We need to know the name of our new pet.

– This function is using as a callback for a pet.
local sayName = function(event)
pet.say(“Meow Purr Meow”);
pet.say(“Purr Purr”);
pet.say(“Meow Purr Meow Meow”);
– Use the pet:on(eventType, eventHandler) method
– Assign the sayName function to handle “hear” events.

– It must be after “pet:on”.
hero:say(“What’s you name, buddy?”)
hero:say(“Could you repeat it?”)



You have been given a predefined **event handler** function called `sayName`.

Use the `pet.on(eventType, eventHandler)` method to assign `sayName` as the event handler for `"hear"` events!

local sayName = function(event)

pet:on("hear", sayName)


An event handler is a function that will be executed when an event happens.

You use pet.on(eventType, eventHandler) to assign an event handler for an event type, like "hear".

The event handler can be any function you define. It should accept one parameter, the event’s data. You’ll learn more about the event data later.

For example:

local someFunction = function(event)
pet:on("hear", someFunction)

Note: You don’t use () after someFunction in pet.on("hear", someFunction). The () means that the function is immediately called.
Instead we are passing the function as an argument to .on() so that it can be called later, when a "hear" event happens.


Phd Kitty

```lua -- Teach your pet to answer questions!

– Luckily, all the answers are "Two"
local sayTwo = function(event)
– Use pet:say() to answer “Two”


– Use pet.on() to handle “hear” events with sayTwo

– Now relax and watch the show.
hero:say(“One plus one is…?”)
hero:say(“x^3 - 6x^2 + 12x - 8 = 0. What is x…?”)
hero:say(“How many moons does Mars have…?”)


Teach Kitty how to perform special tricks to astound! Preprogram Kitty with the correct answer and impress the onlookers by asking rigged questions.

local sayApplesauce = function(event)

pet:on("hear", sayApplesauce)

hero:say("What is made up of apples?")
hero:say("What is mashed up into a sauce?")
hero:say("What is made better with a little cinnamon?")

Tomb Raider - code help - what are functions - lua default code for level

Pet Quiz

```lua -- Lets ask your pet a little.

– Write an event handler function “petSay”.

-- The pet should say something in this function.

pet:on(“hear”, petSay)

hero:say("<%= q3 %>")
hero:say("<%= q1 %>")
hero:say("<%= q2 %>")
– Ask two more questions.



Create an event handler function for the pet to execute when they hear something!

Remember how to define a function:

local myFunctionName = function(myParameterName)
    -- do something here


Guard Dog

```lua -- You cant help the peasants across the river. -- But, your pet can! -- Teach your wolf to be a guard dog.

local alarm = function(event)
while true do
– Pets can find enemies, too.
local enemy = pet:findNearestEnemy()
– If there is an enemy:

        -- Then have the pet say something:

pet:on(“spawn”, alarm);



Long Road

```lua -- Move to the right side of the forest.

– This function should check for items near the pet and fetch them.
local fetchPotions = function(event)
– Complete this function:

    -- Don't forget to use a "while-true loop":
    -- If there's an item:
        -- Use pet:fetch() to fetch the item.


– Assign the function ‘fetchPotions’ for the pet event “spawn”.

hero:moveXY(78, 35)


Forest Jogging

```lua -- Your pet can use pet:moveXY()

local runAround = function(event)
while true do
– First, move to the left X mark:
local pet:moveXY(9, 24)
– Next, move to the top X mark:
local pet:moveXY(30, 43)
– Move your pet to the right X mark:

    -- Move your pet to the bottom X mark:

– Use pet:on() to handle the “spawn” event with runAround

– Cheer on your pet!
– Dont remove the commands below.
while true do
hero:say(“Good dog!”);
hero:say(“You can do it!”);
hero:say(“Run Run Run!”);
hero:say(“One more lap!”);



Forest Cannon Dancing

```lua -- Your pet should run back and forth on the marks. -- The hero should cheer the whole time!

– Write the event function “runBetween” for the pet.
– This function should make the wolf run back and forth:
– First, the pet should run to the right mark, then the left one:

pet:on(“spawn”, runBetween)
– Cheer up your pet. Dont remove this:
while true do
hero:say("<%= c1 %>")
hero:say("<%= c2 %>")