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

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


not sure if this is where I need to put this post but i am stuck.

my code is

local function attackEnemy()
if enemy then

while true do
local enemy = hero:findNearestEnemy()

however “attackEnemy()” functions wont activate and I dont know why.


Are you attacking a door named "Door" or are you trying to attack a door represented by the variable Door?


the door is named “Door”

the level is

however the instructions are in Java and I am using Lua (a problem witch seems prevalent in almost all the levels I have done recently)


@LordMaddog I have been working on updating levels to support LUA better.

I would recommend writing the functions for lua this way:

local attackEnemy = function()

Here is a LUA function help that I wrote since it was not yet implemented:

This way you can call the function later just by writing attackEnemy(). With the way that it is being written now you would most likely need to change your line of code to local a = attackEnemy() which I am guessing is not what you wan to do.

  1. The "Door" with quotation marks " means a door named “Door”. The Door without quotation " marks means to reference the value stored in a local variable named Door.
    As in local Door

  2. Your end statements may be out of place, double check them as well.

Finally if there are a bunch of levels without LUA please let me know, I have been adding them to a todo list to start updating to add in new code and hint files.


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.

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

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

while true do
    local nearestTree = hero:findNearestTree()
    local food = hero:harvest(nearestTree)

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"

while true do
    local enemy = hero:findNearestEnemy()

does this help now?

LUA code patch for - Forest - Tomb Raider

I thought I understood but

local attackEnemy = function()
if enemy then

while true do
local enemy = hero:findNearestEnemy()

code still don’t work


@LordMaddog its almost there.

Remember your code block constructs. A “do” - “end” sequence encapsulates the code within it attacking that code to the concept.

while true do
    -- some code here


``` local attackEnemy = function() -- some code here end ```


if true then
    -- some code
   -- some other code

The function, while, and if statements all need an end but be careful that they are in the right place!



If you look at the code patch content I posted above you will see that this is in there. Now interestingly enough because of scope the code will work without it if you directly reference the local variable enemy. Though I would recommend the = function(target) route you mentioned.

1 Like

@Harry_the_Wanderer Yeah, I’m still a bit confused but that is due to feeling embarrassed. Which is why I deleted my post in the first place. After I hit reply I went and reread the code LordMaddog had wrote and compared to your help and realized I was just cluttering, rather than helping. Still, appreciate the explanation. I’m just going to read and learn on the topic as to avoid this problem again.


no problem. I figured I would clarify because it is something someone might ask when reading this post in the future. You question will actually help them too!

1 Like

@LordMaddog I updated the rest of the levels in that path of the forest. Once you finish this level feel free to check out the forum and you will find introductions / Overviews / and Default Code for each of those levels:

And if you are still having trouble with your code please just ask. I can review a few more tricks with you.


thank you!

I got it now that I mostly understand functions

however I do have 2 questions

local AaD = function() – what dose () do and why do I need them on the end of function


while true do
local enemy = hero:findNearestEnemy()
AaD() --why did I need to add the () on when local refers to AaD as just AaD without.


@LordMaddog ah, that is a good question.

You see, computers, although capable of doing amazing things, aren’t intuitive like you and I. So in programming we have to be very exact about what we do.

by using () we are telling the computer that the word that came before it was the name of a function.

findNearestEnemy by itself should refer to a variable, in other words it stores something.

findNearestEnemy() is a function so it does something.

if you are good in linguistics this is where I could say that findNearestEnemy is a noun and findNearestEnemy() is a verb.

does that make more sense?

Again as computers are not very smart, we need to give structure to everything we write. So when we declare a function

local AaD = function()
    -- some code

We need to tell the computer that yes this is a function and no we are not sending anything to it, empty ( ).

if this function was able to take an object and do something with it then we could declare it as follows:

local cleanAndScrub = function( theDishes )
    -- theDishes is an array of things to wash
   local firstDish = theDishes[0]

Functions can manipulate things just like you or I can. We can wash the dishes and help out (and ought to) just like the function shown above. This is how functions were chosen to be created. Because it is useful to work on things.

So the parenthesis are our structure, like a persons hands, they hold objects inside that were sent to the function to work on. By the way these objects we are talking about are referred to as arguments in programming.

Does that help a bit?

If you didn't know, you can use the forum text editors "Formatted Text" button after highlighting your code blocks to make the pretty like mine are. Shown below:




Ah I see so


is just a prewritten function and hero or self defineds who is running said function.

That helps me understand a lot I was wondering how thees nicely named bits of code exist.

whats the findNearestEnemy() script actually look like?
Dose it refer to many other function?
And if so what’s the base code look like? I guess it’s built on a GUI of some sort that refers all the way down to the operating systems code witch goes all the way to binary.

That’s so mind blowing I “Knew” it worked that way but never understood it until now


on level/deja-brew I use

self:say("Take", numToTakeDown + " down, pass it around!")

on the watch screen it shows the hero singing with the rest perfectly just like he should but I get a syntax error from this line and I don’t pass the level

nvm just removed the function and said 1 like you should tho it seemed like it should have worked at least I was positive I seen my hero singing along now thought after reset he dont when using said code.


Nice explanation of what () do
but he eventually comes to pets
To Event handlers
and for sure will be confused
Why no () in this case


So if we look at this line of code closely how is it similar or different from the one given? hero:say(potionsOnTheWall + " potions of health on the wall!")

And do you know what the difference is between:

someFunction( “one” + “two” + “three” )


someFunction( “one” , “two” , “three” )



ok duh I need to use + instead of , but what dose , do?

it worked in

pet:say("hear", speak)

for that matter what dose “hear” do? Because the pet don’t actually say hear.


@LordMaddog if we have the following function:

local someFunction = function( firstArgument, secondArgument, thirdArgument )
    if firstArgument then
        hero:say( firstArgument )
        hero:say("There is no first argument")

    if secondArgument then
        hero:say( secondArgument )
        hero:say("There is no second argument")

    if thirdArgument then
        hero:say( thirdArgument )
        hero:say("There is no third argument")

What will the following pieces of code do?

someFunction( “one” + “two” + “three” )

someFunction( “one” , “two” , “three” )


I believe you mean

pet:on("hear", speak)

The pet.on() function is predefined as well. It takes 2 arguments. the first being an indicator about what action took place and the second is the actual name of a function to execute when this occurs.

It is like saying in English:

Pet, when you “hear” something perform the “speak” functions action.

The list of actions that can be used to trigger this to happen are limited by a predefined list. "hear" was chosen for when characters in the game speak.

1 Like