So I've been trying to make my Lua scripts in LogitechGHUB better so that they don't skip a few shots or stop working all of a sudden for a couple of seconds, originally I made this code but no matter what I did it still failed at random moments
EnablePrimaryMouseButtonEvents(true);
function OnEvent(event,arg)
if IsKeyLockOn(LockKey)then
if IsMouseButtonPressed(RC) then
repeat
if IsMouseButtonPressed(LC) then
repeat
MoveMouseRelative(0,11)
if (coun2<2 and IsMouseButtonPressed(LC))
then
MoveMouseRelative(3,13)
end
if (coun2>10 and coun2<25 and IsMouseButtonPressed(LC))
then
MoveMouseRelative(0,1)
end
if (coun2>35 and coun2<55 and IsMouseButtonPressed(LC))
then
MoveMouseRelative(1,0)
end
if (coun2>65 and coun2<75 and IsMouseButtonPressed(LC))
then
MoveMouseRelative(1,1)
end
if (coun2>85 and IsMouseButtonPressed(LC))
then
MoveMouseRelative(1,1)
end
Sleep(1)
coun2 = coun2+1
until not IsMouseButtonPressed(LC)
coun2=0
end
until not IsMouseButtonPressed(RC)
end
end
end
LockKey="numlock"
coun2 = 0
LC=1
RC=3
I changed the idea of using counters to make it more customizable, for loops like this
EnablePrimaryMouseButtonEvents(true)
function OnEvent(event, arg)
if event == "MOUSE_BUTTON_PRESSED" and arg == 1 and IsMouseButtonPressed(3) and IsKeyLockOn("numlock") then
for i = 1, 2 do
MoveMouseRelative(3,20)
Sleep(1)
if not IsMouseButtonPressed(1) then return end
end
for i = 1, 135 do
MoveMouseRelative(1,12)
Sleep(1)
if not IsMouseButtonPressed(1) then return end
end
end
end
Which did in fact make it more consistent as long as I didn't use too many for loops, but it still occasionally stops moving the mouse for random periods of time. I tried changing the Sleep() functions for FastSleep() that I saw another user recommending, but it remained the same just faster. Is there a way of making the script less bound to fail or like something I'm not understanding that messes the code? Or should I simply try a different coding language?
Try
local LockKey = "numlock"
local moves = { -- dx/msec, dy/msec, msec
{0.3, 2.0, 30},
{0.0, 1.1, 100},
{0.0, 1.2, 150},
{0.0, 1.1, 100},
{0.1, 1.1, 200},
{0.0, 1.1, 100},
{0.1, 1.2, 100},
{0.0, 1.1, 100},
{0.1, 1.2, math.huge},
}
local function get_distance(t)
local x, y = 0, 0
for _, move in ipairs(moves) do
local vx, vy, mt = move[1], move[2], move[3]
if t < mt then
mt = t
end
x, y = x + vx*mt, y + vy*mt
t = t - mt
if t <= 0 then break end
end
return math.floor(x), math.floor(y)
end
function OnEvent(event, arg)
if event == "PROFILE_ACTIVATED" then
EnablePrimaryMouseButtonEvents(true)
elseif event == "MOUSE_BUTTON_PRESSED" and arg == 1
and IsMouseButtonPressed(3) and IsKeyLockOn(LockKey)
then
local t0 = GetRunningTime()
local t = t0
repeat
Sleep(10)
local oldx, oldy = get_distance(t - t0)
t = GetRunningTime()
local newx, newy = get_distance(t - t0)
MoveMouseRelative(newx - oldx, newy - oldy)
until not IsMouseButtonPressed(1)
end
end