Shejo284
Shejo284

Reputation: 4771

Unusual LUA function syntax

I'm a new LUA user, version 5.3 and I came across a function statement that is a bit confusing. I've tried to find some explanation in the reference manual but to no avail.

function myfunc.execute()
   print("Hello")
end

I don't understand this syntax. Does this mean to add ".execute()"?

Below is a example file that fails to print within the table myfunc.

local myfunc = {}
myfunc.version = "2.0"

function myfunc.execute()
   print("Hello World!")
end

return myfunc

When I run this i get no print out: "Hello World!". I'm trying to understand how lua works here when executing the above script in a file. Why does the print statement not work?

Upvotes: 2

Views: 309

Answers (1)

brianolive
brianolive

Reputation: 1671

In your example you are simply defining the execute method that is in the myfunc table. Remove the return myfunc line and just call your function like this:

myfunc.execute()

There is nothing special about the execute word. It is not a Lua keyword. Its just the name you are giving to a function.

Upvotes: 3

Related Questions