• You've discovered RedGuides, an EverQuest multi-boxing and scripting community 🧙‍♀️⚙️. We want you to play several EQ characters at once, come join us and say hello! 👋

  • A TLP without truebox has thawed (Very Vanilla ready)
    Frostreaver

Lua - How to use include file?

Rare Spawn

Well-known member
Joined
Oct 10, 2022
RedCents
3,282¢
Hello, I have a collection of Lua functions I used for multiple scripts... right now I have copies of several pages of functions at the top of ALL my scripts... its very inefficient when I fix or improve a function, I have to copy/paste it to all my other scripts.

How in Lua, do I create a single file for just all my functions... aka an include file, or a library, that I can then reference with my other scripts? I have tried 'require' and also 'dofile', I am not doing something right as neither is working. Can I get a push in the right direction? -- thanks
 
[CODE lang="Lua" title="utils.Lua"]-- Create a table which will hold everything we want to be exported from this Lua file
local utils = {}

-- Add a function to the utils table under the key "perform_some_task"
function utils.perform_some_task()
print('successfully performed some task')
end

-- Return the utils table containing our function
return utils[/CODE]

[CODE lang="Lua" title="main.Lua"]local utils = require('utils')

utils.perform_some_task()[/CODE]

Code:
lua/
  myscript/
    main.lua
    utils.lua
 
Lua - How to use include file?

Users who are viewing this thread

Back
Top
Cart