fred basset
fred basset

Reputation: 10082

How to share code between Lua scripts

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?

Upvotes: 1

Views: 708

Answers (2)

ivan_pozdeev
ivan_pozdeev

Reputation: 36018

Modules are the simplest yet versatile way.

Upvotes: 1

Nicol Bolas
Nicol Bolas

Reputation: 473427

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.

Upvotes: 6

Related Questions