| Message |
I am encouraging the use of modules with MUSHclient 3.80 Lua scripts.
The module feature, combined with the "require" keyword, simplifies the importing of commonly-used scripts.
Various scripts that used to part of the exampscript.lua file in the MUSHclient distribution are now supplied separately in the (new) "lua" subdirectory (under the directory where the MUSHclient executable resides).
The reason for putting them there is that one of the default places that "require" looks is a subdirectory called "lua" under the directory of the current executable, and thus they should be found automatically.
If you prefer to put your script modules somewhere else, all you need to do to have them incorporated automatically is to amend the package.path variable somewhere, for example in the Global Preferences Lua sandbox code.
Something like this might do the trick:
package.path = "C:\\mushclient\\lua\\?.lua"
You need 2 backslashes because it is inside a quoted string. What this example would do is look for packages in the lua directory under C:\mushclient.
If you want to look in a second place, simply use a semicolon, and then mention the next place to search, like this:
package.path = "C:\\mushclient\\lua\\?.lua;C:\\mushclient\\plugins\\lua\\?.lua;"
A simple way of doing that is to get the paths from MUSHclient itself, like this:
package.path = GetInfo (66) .. "lua\\?.lua" -- application directory
package.path = package.path .. ";" .. GetInfo (59) .. "\\?.lua" -- script files directory
package.path = package.path .. ";" .. GetInfo (60) .. "?.lua" -- plugins directory
This example would look in the lua subdirectory under the MUSHclient application, and also directly inside the script files directory, and the plugins directory.
If you are a plugin author you may want to require your scripts relative to where the current plugin is (GetPluginInfo (20)), so that the plugin will find the appropriate scripts, regardless of where the user has installed the plugin.
The following Lua scripts are being supplied with MUSHclient 3.80:
- addxml.lua - add triggers, timers, aliases, macros by supplying a table (and convert back to a table)
- check.lua - check a world function return code
- declare.lua - ensure variables in a function are declared
- getlines.lua - iterator to convert a block of text into lines
- pairsbykeys.lua - iterator to traverse a table, sorted by key order
- serialize.lua - serialize Lua variables into a string
- strict.lua - enforce use of local variables inside functions
- tprint.lua - table printer
- var.lua - use MUSHclient variables as if they are Lua variables
- wait.lua - for pausing scripts until time elapsed, or certain text arrives from the MUD
Once your package.path is set up correctly, you simply require the package without the .lua extension. For example:
require "var" -- use the var.lua package
If you like to have all of these packages available all the time, you could simply require them all as part of the global sandbox.
|
- Nick Gammon
www.gammon.com.au, www.mushclient.com | top |
|