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

#6

I thought I understood but

local attackEnemy = function()
if enemy then
hero:attack(enemy)
else
hero:attack(“Door”)
end

while true do
local enemy = hero:findNearestEnemy()
attackEnemy()
end
end

code still don’t work

#7

@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
end 

and


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

and


if true then
    -- some code
else
   -- some other code
end

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

#9

@Xector64

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

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

#11

@Xector64
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
#12

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

#13

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
–cut
else
–cut
end

end

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

#14

@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
end

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]
   clean(firstDish)
   scrub(firstDish)
end

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:

 

-HW

#15

Ah I see so

 hero:findNearestEnemy()

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

#16

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.

#17

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

#18

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

and

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

?

#19

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.

#20

@LordMaddog if we have the following function:

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

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

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

What will the following pieces of code do?

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

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

#21

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

I see I din’t know you could define a word in " to be a function tho.

so from what I am seeing , dictates target (either function or .id) and + is add.

Is that correct?

#23

@LordMaddog perhaps this illustration will help?

The commas separate things that you send to a function. So for instance if we use our previous example of the function that cleanAndScrub()'s dishes this is one way we could send many dishes to be cleaned:

local dish1 = "A dirty Dish"
local dish2 = "A grimy Dish"
local dish3 = "A filthy Dish"
cleanAndScrub( dish1, dish2, dish3 )

remember that this is just an example and the “dishes” we are talking about are really just strings.

in the pet example we are sending two things to the function on(). A string "hear" and the name of a function speak.

speak would need to be defined in your code:

local speak = function()
    pet:say(" I am talking ")
end

And if you are asking why are we using the word argument for programming and wondering why we should be fighting with our functions and not having a constructive discussion here is a nice read:
1 Like
#24

Thank you.

I am learning a lot and I really like you game its amazing!

#25

You are welcome. And I enjoy their game as well. I am just a community member like yourself.

@LordMaddog if it is alright with you, I am going to update the title of your post to help the rest of the community who might have similar questions.

#26

How so ? I mean can you clarify with an example.