一 package & os

1.判断是否是Linux或Windows平台

亦可用于判断是Windows还是手机运行脚本。

local DIR_SEP = package.config:sub(1,1) 
local IS_WINDOWS = DIR_SEP == '\\'

至少不用这样死板了…

2.更改require路径

可用于mod\script内的*.lua相互调用。
全部替换:

package.path = getDiceDir() .. "/mod/modname/script/?.lua"

替换局部:

package.path = getDiceDir() .. "/mod/modname/script/?.lua;"..package.path

二 pcall & error

1.预处理require模块

当prequire的模块不存在时不会报错而是返回nil。

local function prequire(...)
   local ok, mod = pcall(require, ...)
   if not ok then return nil, mod end
   return mod 
end

三 string & table

1.我喜欢这样输出日志

msg:echo(string.format("[%s] %s", os.date("%Y.%m.%d %H:%M:%S"), 'baka'))

2.string2table

假装str是你要转换的文本,那么你可以这样来做到文本转换为lua的表

return load("return " .. str)()

四 coroutine & metamethod

pass

五 Regex

1.我喜欢这样取文件后缀

多用于判断文件类型

return str:match(".+%.(%w+)$")

2.取文件名

return path:match(".+/(.+)%..+$")

六 Some Jokes

只有天才才知道是为什么

return tostring('е' == 'e') --false
- return #'e' --1
+ return #'е' --2

to be continue…

简律纯 将标题更改为 「【教程向】我写脚本时会使用的一些干货奇淫巧技」。
简律纯 将标题更改为 「【教程向】我写脚本时会使用的一些奇淫巧技」。
/***
 *
 *   █████▒█    ██  ▄████▄   ██ ▄█▀       ██████╗ ██╗   ██╗ ██████╗
 * ▓██   ▒ ██  ▓██▒▒██▀ ▀█   ██▄█▒        ██╔══██╗██║   ██║██╔════╝
 * ▒████ ░▓██  ▒██░▒▓█    ▄ ▓███▄░        ██████╔╝██║   ██║██║  ███╗
 * ░▓█▒  ░▓▓█  ░██░▒▓▓▄ ▄██▒▓██ █▄        ██╔══██╗██║   ██║██║   ██║
 * ░▒█░   ▒▒█████▓ ▒ ▓███▀ ░▒██▒ █▄       ██████╔╝╚██████╔╝╚██████╔╝
 *  ▒ ░   ░▒▓▒ ▒ ▒ ░ ░▒ ▒  ░▒ ▒▒ ▓▒       ╚═════╝  ╚═════╝  ╚═════╝
 *  ░     ░░▒░ ░ ░   ░  ▒   ░ ░▒ ▒░
 *  ░ ░    ░░░ ░ ░ ░        ░ ░░ ░
 *           ░     ░ ░      ░  ░
 */
8 个月 后
25 天 后
说点什么吧...