【问题标题】:LUA scripting switching on/off a scriptLUA 脚本打开/关闭脚本
【发布时间】:2016-09-22 22:42:32
【问题描述】:

我正在使用 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
            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 激活脚本,我就会陷入“强制移动”模式,而“自动攻击”模式永远不会起作用。不知道为什么。

  • 【问题讨论】:

      标签: lua logitech logitech-gaming-software


      【解决方案1】:

      当您按下鼠标按钮 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() 什么也不做,所以 forceMoveon 保持不变。 这样,您可以在 action() 中没有循环的连续运动。

      【讨论】:

      • 如果我错了,请纠正我,但这样我每次按下按钮 4/5 时只会触发 1 个动作,但我想继续执行这两个动作之一虽然“开启”模式为真(按钮 4),但只需使用按钮 5 从一个切换到另一个。无论如何,我必须使用一段时间。我认为在 OnEvent 之外使用全局变量就可以了
      • 嗯,这取决于。当然,你必须在某个地方有一个循环。虽然我不知道罗技游戏引擎,但任何游戏引擎的基本思想都应该大致相同。我已经更新了我的答案以反映这一点。
      猜你喜欢
      • 2021-05-31
      • 2011-08-23
      • 2019-04-23
      • 2013-05-17
      • 2011-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-17
      相关资源
      最近更新 更多