'Include' files in Lua

Posted by Nick Gammon on Sat 27 Nov 2004 11:55 PM — 1 posts, 19,685 views.

Australia Forum Administrator #0
We have had questions in the past about "including" standard scripts into a script file.

Lua provides a very simple function for doing that: dofile.

If you have some standard commands that you want to share, simply do this:


dofile "myfilename.lua"


You might want to prepend a path, for example the MUSHclient executable or worlds path. For example:


path = GetInfo (57) -- World files default path (directory)


In that case your include command may look like this:


dofile (GetInfo (57) .. "myfilename.lua")


There is also a "require" function that works similarly, but searches a path (which you can specify) and doesn't load the same file twice.