# LUA patches for pet levels in forest

**URL:** <https://discourse.codecombat.com/t/lua-patches-for-pet-levels-in-forest/9430>\
**Category:** Artisan\
**Created:** [October 22, 2016, 9:49pm UTC](https://discourse.codecombat.com/t/lua-patches-for-pet-levels-in-forest/9430 "2016-10-22T21:49:55Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Harry\_the\_Wanderer](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/harry_the_wanderer/32/5369_2.png) [@Harry\_the\_Wanderer](https://discourse.codecombat.com/u/Harry_the_Wanderer)\
**Post date:** [October 22, 2016, 9:49pm UTC](https://discourse.codecombat.com/t/lua-patches-for-pet-levels-in-forest/9430/1 "2016-10-22T21:49:55Z")

</div>

## Go Fetch

* * *

```python
-- 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:
            
        end
    end
end

-- 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)

```

* * *

Intro

Your pet knows how to fetch items!

```python
local item = hero:findNearestItem();
if item then
    pet:fetch(item)
end

```

* * *

---

<div class="post-metadata">

**Author:** ![Harry\_the\_Wanderer](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/harry_the_wanderer/32/5369_2.png) [@Harry\_the\_Wanderer](https://discourse.codecombat.com/u/Harry_the_Wanderer)\
**Post date:** [October 22, 2016, 10:11pm UTC](https://discourse.codecombat.com/t/lua-patches-for-pet-levels-in-forest/9430/2 "2016-10-22T22:11:43Z")

</div>

## 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”);  
pet.say(“Meow”);  
pet.say(“Meow Purr Meow Meow”);  
end  
– 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?”)

````python
<hr>

Introduction:

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!

```lua
local sayName = function(event)
    petLsay("lol")
end

pet:on("hear", sayName)

````

* * *
Overview:

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:

```python
local someFunction = function(event)
    pet:say("Ahhh")
    pet:say("Bbbbzzz")
end
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.

---

<div class="post-metadata">

**Author:** ![Harry\_the\_Wanderer](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/harry_the_wanderer/32/5369_2.png) [@Harry\_the\_Wanderer](https://discourse.codecombat.com/u/Harry_the_Wanderer)\
**Post date:** [October 22, 2016, 10:25pm UTC](https://discourse.codecombat.com/t/lua-patches-for-pet-levels-in-forest/9430/3 "2016-10-22T22:25:04Z")

</div>

## 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”

end

– 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…?”)

````python
<hr>
Overview:

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

```lua
local sayApplesauce = function(event)
    pet:say("Applesauce")
end

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?")

````

---

<div class="post-metadata">

**Author:** ![Harry\_the\_Wanderer](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/harry_the_wanderer/32/5369_2.png) [@Harry\_the\_Wanderer](https://discourse.codecombat.com/u/Harry_the_Wanderer)\
**Post date:** [October 22, 2016, 10:31pm UTC](https://discourse.codecombat.com/t/lua-patches-for-pet-levels-in-forest/9430/4 "2016-10-22T22:31:09Z")

</div>

## 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.

````python
<hr>

Overview:

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

Remember how to define a function:

```lua
local myFunctionName = function(myParameterName)
    -- do something here
end

````

---

<div class="post-metadata">

**Author:** ![Harry\_the\_Wanderer](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/harry_the_wanderer/32/5369_2.png) [@Harry\_the\_Wanderer](https://discourse.codecombat.com/u/Harry_the_Wanderer)\
**Post date:** [October 22, 2016, 11:03pm UTC](https://discourse.codecombat.com/t/lua-patches-for-pet-levels-in-forest/9430/5 "2016-10-22T23:03:52Z")

</div>

## 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:

```

end  
end  
pet:on(“spawn”, alarm);

```python
<hr>
```

---

<div class="post-metadata">

**Author:** ![Harry\_the\_Wanderer](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/harry_the_wanderer/32/5369_2.png) [@Harry\_the\_Wanderer](https://discourse.codecombat.com/u/Harry_the_Wanderer)\
**Post date:** [October 22, 2016, 11:18pm UTC](https://discourse.codecombat.com/t/lua-patches-for-pet-levels-in-forest/9430/6 "2016-10-22T23:18:15Z")

</div>

## 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.

```

end

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

hero:moveXY(78, 35)

```python

```

---

<div class="post-metadata">

**Author:** ![Harry\_the\_Wanderer](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/harry_the_wanderer/32/5369_2.png) [@Harry\_the\_Wanderer](https://discourse.codecombat.com/u/Harry_the_Wanderer)\
**Post date:** [October 22, 2016, 11:27pm UTC](https://discourse.codecombat.com/t/lua-patches-for-pet-levels-in-forest/9430/7 "2016-10-22T23:27:23Z")

</div>

## 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:

```

end  
end  
– 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(“Almost!”);  
hero:say(“One more lap!”);  
end

```python
<hr>
```

---

<div class="post-metadata">

**Author:** ![Harry\_the\_Wanderer](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/harry_the_wanderer/32/5369_2.png) [@Harry\_the\_Wanderer](https://discourse.codecombat.com/u/Harry_the_Wanderer)\
**Post date:** [October 22, 2016, 11:31pm UTC](https://discourse.codecombat.com/t/lua-patches-for-pet-levels-in-forest/9430/8 "2016-10-22T23:31:10Z")

</div>

## 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 %\>")  
end

```python

```
