|
|
本帖最后由 贾维斯117 于 2023-9-19 10:48 AM 编辑
ni辅助教程,转换基础的wowbee脚本-LUACN论坛 - Powered by Discuz!看不懂的话先看这一篇。
使用方法
自定义函数能通过封装API和各种逻辑在一个函数里面,达到美化代码和便于重复写脚本的作用。
使用自定义函数有2种方法。
1.自定义函数写在模版abilities数组上面,要在调用前就定义。
如:
[Lua] 纯文本查看 复制代码 ---自定义函数区域---
--自定义函数要在abilities前面声明,不然无法调用函数
local function StringSplit(inputstring,delimiter)
local pos,arr=1,{}
for st,sp in function() return string.find(inputstring,delimiter,pos,true)end do
table.insert(arr,string.sub(inputstring,pos,st-1))
pos=sp+1
end
table.insert(arr,string.sub(inputstring,pos))
return arr
end
---自定义函数区域---
local abilities ={...}
2.新建一个lua文件如abc.lua,放在addon\Rotations\Data
abc.lua里面弄这个模版
[Lua] 纯文本查看 复制代码 local data = {};--先声明一个变量
data.example = function() ---用变量.函数 形式去生成自定义函数
return select(2, UnitClass("player"));
end;
return data;--返回整个变量
脚本里面这么调用
[Lua] 纯文本查看 复制代码 local aaa = ni.utils.require("abc");
if aaa.example() == "WARLOCK" then ---aaa就是abc.lua返回的data,然后调用里面的example函数
print("我是WARLOCK")
end
例子
[Lua] 纯文本查看 复制代码 function click(key)
local button = "MultiBar"
if string.find(key],"左动作条") then
button = button.."LeftButton"..string.match(key,"%d+")
elseif string.find(key],"右动作条") then
button = button.."RightButton"..string.match(key,"%d+")
else
print("click命令出错,确定是否输入左动作条1-12或右动作条1-12")
return
end
ni.player.runtext("/click "..button)
end
local function Touchnpc()--走过去互动npc
local a = ni.unit.distance("player", "target")
if ni.unit.ismoving("player") then
print("正在移动,距离是"..ni.unit.distance("player", "target"))
return false
elseif a > 1 then
ni.player.moveto("target")
print("移向目标")
return false
elseif a <= 1 then
print("互动npc,距离是"..ni.unit.distance("player", "target"))
if not ni.unit.isfacing ("player", "target") then --背对npc就望向npc
ni.player.lookat("target")
end
ni.player.interact("target")
return true
end
end
local abilities = {
["互动目标"] = function()
if enables["AutoNPC"] then
if UnitExists("target") then
Touchnpc()
end
end
end,
["暂停"] = function()
end,
["主流程"] = function()
if enables["AutoAttack"] then
click("左动作条1")
end
end,
}
这种都是很简单的,建议有伸手费的可以看看<ni循环刷怪脚本,可循环刷单个或多个副本或野外。-LUACN论坛 - Powered by Discuz!>,里面还是很多可以学习的地方,作者发的这个版本提供的功能不多,但自己完善下增加点功能,就能很好的用起来了。也算是体验一番自己弄挂机辅助。
wowbee移植
有的人会觉得还是wb好用,因为wb封装了不少易用性较高的函数,其实有兴趣可以自己也移植过来。在data新建个wowbee.lua
就拿BeeGroupMinScript举个例子吧
这是\interface\AddOns\WBE\WowBee.Functions.lua里面BeeGroupMinScript的代码
[Lua] 纯文本查看 复制代码 function BeeGroupMinScript(String,strReturn,group) --小队或者团队里最小的数值的人物信息
if not(group == "party" or group=="partypet" or group=="raid" or group=="raidpet" or group=="arena" or group=="arenapet" ) then
DEFAULT_CHAT_FRAME:AddMessage("group 参数不对")
return false
end
local vname="BeeGroupMinInfo";
BeeSetVariable(vname.."_Name",nil);
BeeSetVariable(vname.."_Class",nil);
BeeSetVariable(vname.."_Race",nil);
BeeSetVariable(vname.."_Spell",nil);
BeeSetVariable(vname.."_SpellCD",nil);
BeeSetVariable(vname.."_Guid",nil);
BeeSetVariable(vname.."_Unit",nil);
BeeSetVariable(vname.."_Value",nil);
if String==nil or strReturn == nil then
DEFAULT_CHAT_FRAME:AddMessage("String 或 strReturn 参数不能为空")
return false
end
local str ='function TEMP_BeeGroupMin(name,class,race,spell,unit,guid,spellcd) if ' .. String .. ' then return ' .. strReturn .. '; else return false; end end'
RunScript(str);
local name,class,race,spell,unit,spellcd,guid;
local Members ,minimum,temp_unit ;
local temp_n =nil;
if group == "party" or group=="partypet" then
Members =GetNumPartyMembers()+1 ;
elseif group=="raid" or group=="raidpet" then
Members =GetNumRaidMembers() ;
elseif group=="arena" then
Members =5;
elseif group=="arenapet" then
Members =5;
end
for i=1, Members do
if i==Members and group == "party" then
unit="player"
elseif i==Members and group == "partypet" then
unit="pet"
else
unit=group .. tostring(i);
end
if UnitName(unit)then
--bufflist = BeeUnitBuffList(unit);
name = UnitName(unit);
class = UnitClass(unit);
race = UnitRace(unit);
spell = BeeUnitCastSpellName(unit);
spellcd = BeeUnitCastSpellTime(unit);
guid = UnitGUID(unit);
minimum = TEMP_BeeGroupMin(name,class,race,spell,unit,guid,spellcd);
if minimum then
if temp_n == nil then
temp_n =minimum;
temp_unit = unit;
elseif minimum < temp_n then
temp_n =minimum;
temp_unit = unit;
end
end
end
end
抛去不重要的,你只需要关注这几个地方
[Lua] 纯文本查看 复制代码 function BeeGroupMinScript(String,strReturn,group) --小队或者团队里最小的数值的人物信息
local vname="BeeGroupMinInfo";
BeeSetVariable(vname.."_Name",nil);
local str ='function TEMP_BeeGroupMin(name,class,race,spell,unit,guid,spellcd) if ' .. String .. ' then return ' .. strReturn .. '; else return false; end end'
RunScript(str);
name = UnitName(unit);
class = UnitClass(unit);
race = UnitRace(unit);
spell = BeeUnitCastSpellName(unit);
spellcd = BeeUnitCastSpellTime(unit);
guid = UnitGUID(unit);
minimum = TEMP_BeeGroupMin(name,class,race,spell,unit,guid,spellcd);
其中UnitName、UnitClass这些是魔兽API就不看了,关键是BeeSetVariable、BeeUnitCastSpellName、BeeUnitCastSpellTime。以及str和minimum = TEMP_BeeGroupMin(name,class,race,spell,unit,guid,spellcd);这部分
先把前面几个bee函数转变一下
[Lua] 纯文本查看 复制代码 --wowbee函数移植
local wb = {}
WowBee = {} --留意这里,为什么会没加local开头,因为我要他变成全局变量。
WowBee.Variable={}
wb.IF = function (a,b,c)
if a then
return b;
else
return c;
end
end
wb.BeeSetVariable = function (variableName,value) --设定变量的值
WowBee.Variable[variableName]=value;
return WowBee.Variable[variableName]
end
wb.BeeGetVariable = function (variableName) --读取变量的值
return wb.IF(variableName, WowBee.Variable[variableName], nil);
end
wb.BeeUnitCastSpellName = function (unit,interrupt) --获得指定目标正在施放的法术名称,Interrupt 为非0 只返回可以打断的技能
unit= unit or "target"
local c,_,_,_,startTime,_,_,_,i = UnitCastingInfo(unit);
if c then
if not interrupt then
return c;
else
if not i then
return c;
end
end
else
c,_,_,_,startTime,_,_,i = UnitChannelInfo(unit);
if c then
if not interrupt then
return c;
else
if not i then
return c;
end
end
end
end
return false;
end
wb.BeeUnitCastSpellTime = function (unit) --获得指定目标正在施放的法术剩馀时间
unit= unit or "target"
if not UnitName(unit) then
return -1,-1,"";
end
local spell, _, _, _, startTime, endTime = UnitCastingInfo(unit)
if spell then
local finish = endTime/1000 - GetTime()
return tonumber(format("%.2f",finish) ),tonumber(format("%.2f",(endTime -startTime) /1000)),spell
end
local spellch, _, _, _, startTime, endTimech = UnitChannelInfo(unit)
if spellch then
local finishch = endTimech/1000 - GetTime()
return tonumber(format("%.2f",finishch) ),tonumber(format("%.2f",(endTimech -startTime) /1000)),spellch
end
return -1,-1,"";
end
然后就是把BeeGroupMinScript也变一下,注意里面引用的函数也要改为wb.开头
[Lua] 纯文本查看 复制代码 wb.BeeGroupMinScript = function (String,strReturn,group) --小队或者团队里最小的数值的人物信息
if not(group == "party" or group=="partypet" or group=="raid" or group=="raidpet" or group=="arena" or group=="arenapet" ) then
print('BeeGroupMinScript命令出错,请查阅wb.lua文件看注释')
return false
end
local vname="BeeGroupMinInfo";
wb.BeeSetVariable(vname.."_Name",nil);
wb.BeeSetVariable(vname.."_Class",nil);
wb.BeeSetVariable(vname.."_Race",nil);
wb.BeeSetVariable(vname.."_Spell",nil);
wb.BeeSetVariable(vname.."_SpellCD",nil);
wb.BeeSetVariable(vname.."_Guid",nil);
wb.BeeSetVariable(vname.."_Unit",nil);
wb.BeeSetVariable(vname.."_Value",nil);
if String==nil or strReturn == nil then
print('wb.BeeGroupMinScript命令出错,请查阅wb.lua文件看注释')
return false
end
local str ='function TEMP_BeeGroupMin(name,class,race,spell,unit,guid,spellcd) local wb = ni.utils.require("wowbee");if ' .. String .. ' then return ' .. strReturn .. '; else return false; end end' --留意,local wb = ni.utils.require("wowbee");
RunScript(str);
local name,class,race,spell,unit,spellcd,guid;
local Members ,minimum,temp_unit ;
local temp_n =nil;
if group == "party" or group=="partypet" then
Members =GetNumPartyMembers()+1 ;
elseif group=="raid" or group=="raidpet" then
Members =GetNumRaidMembers() ;
elseif group=="arena" then
Members =5;
elseif group=="arenapet" then
Members =5;
end
for i=1, Members do
if i==Members and group == "party" then
unit="player"
elseif i==Members and group == "partypet" then
unit="pet"
else
unit=group .. tostring(i);
end
if UnitName(unit)then
name = UnitName(unit);
class = UnitClass(unit);
race = UnitRace(unit);
spell = wb.BeeUnitCastSpellName(unit);
spellcd = wb.BeeUnitCastSpellTime(unit);
guid = UnitGUID(unit);
minimum = TEMP_BeeGroupMin(name,class,race,spell,unit,guid,spellcd);
if minimum then
if temp_n == nil then
temp_n =minimum;
temp_unit = unit;
elseif minimum < temp_n then
temp_n =minimum;
temp_unit = unit;
end
end
end
end
if temp_unit then
name = UnitName(temp_unit);
class = UnitClass(temp_unit);
race = UnitRace(temp_unit);
spell = wb.BeeUnitCastSpellName(temp_unit);
spellcd = wb.BeeUnitCastSpellTime(temp_unit);
guid = UnitGUID(temp_unit);
wb.BeeSetVariable(vname.."_Name",name);
wb.BeeSetVariable(vname.."_Class",class);
wb.BeeSetVariable(vname.."_Race",race);
wb.BeeSetVariable(vname.."_Spell",spell);
wb.BeeSetVariable(vname.."_SpellCD",spellcd);
wb.BeeSetVariable(vname.."_Guid",guid);
wb.BeeSetVariable(vname.."_Unit",temp_unit);
wb.BeeSetVariable(vname.."_Value",temp_n);
return temp_unit,name,class,race,spell,spellcd,guid,temp_n;
end
return false
end
return wb --别忘了返回wb
这里面唯一要留意的是local str,要在里面加一行local wb = ni.utils.require("wowbee");因为RunScript这个等于临时生成了一个新的lua,TEMP_BeeGroupMin里面是不能用这种局部的bee函数的。所以要再次引用一下。
然后在脚本里面这么引用,就可以了。
完了,后续靠大家开拓了,只是起个头。附件下不下都没关系,说的上面基本都说了
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?加入我们
x
评分
-
查看全部评分
|