1

What is the preferred way of sharing code between scripts in Lua? E.g. are you supposed to put the code to be shared into a library, or is there some other mechanism?

2 Answers 2

6

Lua, as a matter of design, dictates functionality, not policy. That's for you to decide. So however you wish to share code between scripts is up to your code and your needs.

That being said, if you're writing code for the Lua interpreter, that is intended to be used by people who aren't you, the general way this is done is by building a Lua module. Users require the module, which causes the system to find and execute your Lua script. That script will return a table that contains functions (or nested tables with functions) that the user will use. Optionally, your module can also register those functions globally, though the prevailing wind among Lua library writers seems to be against that.

Sign up to request clarification or add additional context in comments.

Comments

1

Modules are the simplest yet versatile way.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.