• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Lua What is a callback function?

Status
Not open for further replies.

Digital

Learning C++
Joined
Jul 15, 2018
Messages
57
Solutions
1
Reaction score
12
How do callback functions work in scripts and is the behavior the same for each type of script?
 
Solution
http://javascriptissexy.com/understand-javascript-callback-functions-and-use-them/

A JavaScript resource, but callbacks are a common concept in interpreted languages, like LUA.


Callback Functions in Lua

Better explanation of what is possible with Lua specifically.

It's just a function called within a function, however given the power of this + closures, there are a lot of capabilities and uses for callbacks in our code. callbacks can be predefined or anonymous functions, which is possible because of the lambda calculus property.

A common example is in NPC scripts:

Code:
keywordHandler:addGreetKeyword({'hi'}, StdModule.say, {npcHandler = npcHandler, text = 'Hello, |PLAYERNAME|! I have already heard about you...
http://javascriptissexy.com/understand-javascript-callback-functions-and-use-them/

A JavaScript resource, but callbacks are a common concept in interpreted languages, like LUA.


Callback Functions in Lua

Better explanation of what is possible with Lua specifically.

It's just a function called within a function, however given the power of this + closures, there are a lot of capabilities and uses for callbacks in our code. callbacks can be predefined or anonymous functions, which is possible because of the lambda calculus property.

A common example is in NPC scripts:

Code:
keywordHandler:addGreetKeyword({'hi'}, StdModule.say, {npcHandler = npcHandler, text = 'Hello, |PLAYERNAME|! I have already heard about you! As our newest member you are eligible to be granted a weapon of your {choice}?'}, checkStorageNotSet  )

In this case, checkStorageNotSet is a locally defined function that returns if the desired storage location has a value other than -1, and if it doesn't, output this line.

Basically the result of checkStorageNotSet determines if I output the NPC text.
 
Last edited:
Solution
Status
Not open for further replies.
Back
Top