0

Im trying to put the following 2 scripts together so that they can run at the same exact time.

function OnEvent(event, arg)
    if EnableRCS ~= false then
        if IsMouseButtonPressed(3) then  -- Only check for right mouse button press
            repeat
                if IsMouseButtonPressed(1) then
                    repeat
                        MoveMouseRelative(0, RecoilControlStrength)
                        Sleep(DelayRate)
                    until not IsMouseButtonPressed(1)
                end
            until not IsMouseButtonPressed(3)
        end
    end
end

and

if event == "MOUSE_BUTTON_PRESSED" and arg == 8 then
      PressKeySequenceA()
  end

I've tried combining the script like this:

function OnEvent(event, arg)
  if EnableRCS ~= false then
    
    if event == "MOUSE_BUTTON_PRESSED" and arg == 8 then
      PressKeySequenceA()
    end

    
    if IsMouseButtonPressed(3) then
      repeat
        if IsMouseButtonPressed(1) then
          repeat
            MoveMouseRelative(0, RecoilControlStrength)
            Sleep(DelayRate)
          until not IsMouseButtonPressed(1)
        end
      until not IsMouseButtonPressed(3)
    end
  end
end

But it doesn't result in the scripts being executed simultaneously

Right now the scripts execute one by one but I want them to execute at the same time.

8
  • Is there something wrong with putting the second snippet at the top of the function?
    – Andrew Yim
    Commented Jul 6 at 15:24
  • 1
    please show how the second snippet is executed in your code
    – jsotola
    Commented Jul 6 at 15:28
  • @AndrewYim I tried doing that but the two separate scripts don't run simultaneously
    – censori
    Commented Jul 6 at 15:53
  • "...I want them to execute at the same time." -- You want the two scripts to run in parallel on two different cores? This doesn't make much sense, and seems like an XY problem. What is the actual problem that you are trying to solve? Commented Jul 6 at 18:10
  • @adabsurdum I want the function "PressKeySequenceA" to run at the same time as MoveMouseRelative. I'm using this script for a game and the PressKeySequenceA moves my character in the game around while MoveMouseRelative is supposed to move my mouse around. I want to move my character using WASD which is already in the function and I want to move my mouse at the same time. My bad if my tech language is bad because I don't have much experience in this besides some youtube videos and an AP class
    – censori
    Commented Jul 6 at 19:20

1 Answer 1

0

What you could try is running the PressKeySequenceA() on a separate thread. It's probably not running at the same time because its waiting for PressKeySequenceA() to be finished. Try doing something like this:

function OnEvent(event, arg)
  if EnableRCS ~= false then

    if event == "MOUSE_BUTTON_PRESSED" and arg == 8 then
      local co = coroutine.create(PressKeySequenceA)
      coroutine.resume(co)
    end

    if IsMouseButtonPressed(3) then
      repeat
        if IsMouseButtonPressed(1) then
          repeat
            MoveMouseRelative(0, RecoilControlStrength)
            Sleep(DelayRate)
          until not IsMouseButtonPressed(1)
        end
      until not IsMouseButtonPressed(3)
    end
  end
end
1
  • logitech ghub doesn't have the coroutine library
    – censori
    Commented Jul 7 at 16:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.