LUA脚本打开/关闭脚本
我正在用LUA/logitech脚本API编写脚本。脚本应执行以下操作:LUA脚本打开/关闭脚本,lua,logitech,logitech-gaming-software,Lua,Logitech,Logitech Gaming Software,我正在用LUA/logitech脚本API编写脚本。脚本应执行以下操作:鼠标键4打开/关闭脚本鼠标键5从一个功能切换到另一个功能(强制移动和自动攻击) 代码如下: forceMove = false on = false function OnEvent(event, arg) --OutputLogMessage("event = %s, arg = %s\n", event, arg); if IsMouseButtonPressed(5) then forc
代码如下:
forceMove = false
on = false
function OnEvent(event, arg)
--OutputLogMessage("event = %s, arg = %s\n", event, arg);
if IsMouseButtonPressed(5) then
forceMove = not forceMove
while(on) do
if(forceMove) then
ForceMove()
else
StartAttack()
end
end
ReleaseMouseButton(5)
end
if IsMouseButtonPressed(4) then
on = not on
ReleaseMouseButton(4)
end
end
function StartAttack()
PressAndReleaseMouseButton(1)
Sleep(1000)
end
function ForceMove()
MoveMouseWheel(1)
Sleep(20)
MoveMouseWheel(-1)
end
但一旦在游戏中,如果我用鼠标按钮4激活脚本,我就会陷入“强制移动”模式,而“自动攻击”模式永远无法工作。无法理解原因。当您按下鼠标按钮5时,您将激活“强制移动”模式。如果同时启用“开启”模式,则会导致无限循环:
while(on) do
if(forceMove) then
ForceMove()
else
StartAttack()
end
end -- loops regardless of mouse buttons
你将永远留在这里,不管你按下鼠标键。
您需要转移到鼠标事件处理程序之外的执行代码。处理程序应该只更新像forceMove这样的值,执行操作需要另一个函数。在这些函数中,您只执行一个步骤,而不是多个步骤。
然后再次检查是否按下鼠标按钮,执行操作等。
代码示例:
function update()
if IsMouseButtonPressed(4) then
on = not on
end
if IsMouseButtonPressed(5) then
forceMove = not forceMove
end
end
function actions()
if on then
if forceMove then
ForceMove()
end
end
end
如何组合:
您必须使用某种循环,但理想情况下,游戏引擎应该为您这样做。它看起来像这样:
local is_running = true
while is_running do
update()
actions()
end
现在,如果您按下一个按钮,您将当前状态保存在一些全局变量中,这些全局变量可以通过更新和操作访问。每个周期调用函数(可以是一帧的计算)。假设不按任何其他按钮,update()将不执行任何操作,因此forceMove
和on
保持不变。
通过这种方式,您可以连续移动,而不必执行循环()。反正我得用一段时间。我认为在OneEvent之外使用一个全局变量会很好,这要看情况而定。当然,你必须在某个地方有一个循环。虽然我不知道罗技游戏引擎,但对于任何游戏引擎来说,其基本思想应该大致相同。我更新了我的答案以反映这一点。