sansouci |
2019-02-20 16:38 |
MushClient一个防止重复触发的例子
首先感谢grimlock建议,现在改成了两个版本。
直接上lua代码:
代码:
--第一个版本,封装一个临时timer,使连续相同触发时只有第一个触发有效
function ExecFirstTriCmd(cmd,tName,sTime)
--[[
cmd:string,待执行的mud指令字符串
sTime:float,最小0.5,禁止重复触发的时间长度,单位:s
tName:string,timer名称
--]]
if not sTime then sTime = 2 end
if not tName then tName = "abcdeTimer" end
if not cmd then
print("Error: cmd不能为空!看到该信息表明未来sTime秒或2秒内什么也不会执行!")
cmd = ''
end
local _,s = GetTimer(tName)
if not s then
AddTimer(tName, 0, 0, sTime, '', timer_flag.Enabled + timer_flag.OneShot, "")
SetTimerOption(tName, "send_to", 12)
Execute(cmd)
print('This cmd will not be executed again in '..sTime..' seconds.')
end
end
--第二个版本,封装一个临时timer,使连续相同触发时只有第一个触发有效
function ExecFirstTriScript(script,tName,sTime)
--[[
script:string,待执行的脚本
sTime:float,最小0.5,禁止重复触发的时间长度,单位:s
tName:string,timer名称
--]]
if not sTime then sTime = 2 end
if not tName then tName = "abcdeTimer" end
if not script then
print("Error: script不能为空!看到该信息表明未来sTime秒或2秒内什么也不会执行!")
script = ''
end
local _,s = GetTimer(tName)
if not s then
AddTimer(tName, 0, 0, sTime, '', timer_flag.Enabled + timer_flag.OneShot, "")
SetTimerOption(tName, "send_to", 12)
DoAfterSpecial(0.5,script,12)
print('This script will not be executed again in '..sTime..' seconds.')
end
end
当然由于上面的函数参数不是(name, line, wildcards, styles)格式,不适合纯脚本模式,要做成纯脚本模式需要做一些取舍与修改。
欢迎各种技术上的砖头~
|