run simultaneous repeat loops Lua Script Logitech GHUB - lua

This is my first attempt at LUA so bear with me!
I'm trying to combine a couple of functions that I want to loop at the same time, but I can only get them to run sequentially.
Ideally, the function bound to mouse button 6 would start on press and stop on release (currently it doesn't stop at all!!). Separately the function on mouse button 3 would run when it is pressed and stop on release (this one does stop).
At the moment the button 3 loop works as it should, but the button 6 loop doesn't end.
Ideally, I would like both to work and stop on their respective button press and release, AND for them to BOTH run at the same time when both 3 & 6 are pressed at together.
Can this be done and if so what do I need to change to achieve this?
EnablePrimaryMouseButtonEvents(true);
function OnEvent(event, arg)
if event == "MOUSE_BUTTON_PRESSED" and arg == 6 then
repeat
MoveMouseRelative(300,0)
Sleep(150)
MoveMouseRelative(-300,0)
Sleep(1500)
until event == "MOUSE_BUTTON_RELEASED" and arg == 6
elseif IsMouseButtonPressed(3) then
repeat
MoveMouseRelative(0,300)
Sleep(1000)
until not IsMouseButtonPressed(3)
end
end
Thanks in advance :D

Step 1.
Make sure you're not using MB#4 ("back") in the game.
If you don't use MB#4 in the game, just skip "Step 1".
If some action is assigned to MB#4 in the game, do the following:
choose keyboard button you don't currently use in the game (for example, F12)
goto GHUB (KEYS tab); bind F12 to your physical MB#4
goto game options; bind the action to F12 instead of MB#4
Now when you press physical MB#4, the game sees F12 and activates your old action.
Step 2.
Goto GHUB (SYSTEM tab)
Unbind "Back" from MB#4
Bind "Back" to MB#6
Step 3.
The script.
local all_buttons = {
[4] = { -- mouse button #6
{time=0, x=50 }, -- at time 0 move mouse right 50 pixels
{time=150, x=-50}, -- at time 150 move mouse left 50 pixels
{time=150+1500}, -- at time 150+1500 repeat the loop
},
[3] = { -- right mouse button
{time=0, y=50}, -- at time 0 move mouse down 50 pixels
{time=1000}, -- at time 1000 repeat the loop
}
}
function OnEvent(event, arg)
if event == "PROFILE_ACTIVATED" then
EnablePrimaryMouseButtonEvents(true)
elseif event == "MOUSE_BUTTON_PRESSED" then
repeat
Sleep(10)
local tm = GetRunningTime()
local some_loop_is_active
for btn_no, info in pairs(all_buttons) do
if not info.started and IsMouseButtonPressed(btn_no) then
info.started = tm
info.next_step = 1
end
some_loop_is_active = some_loop_is_active or info.started
local next_action = info[info.next_step]
if info.started and tm - info.started >= next_action.time then
info.next_step = info.next_step + 1
local x, y = next_action.x, next_action.y
if x or y then
x, y = x or 0, y or 0
local sx, sy = x < 0 and -1 or 1, y < 0 and -1 or 1
x, y = x*sx, y*sy
while x > 0 or y > 0 do
local px, py = x < 127 and x or 127, y < 127 and y or 127
MoveMouseRelative(px*sx, py*sy)
x, y = x-px, y-py
end
else
info.started = nil
end
end
end
until not some_loop_is_active
end
end
You should know that MoveMouseRelative() is limited by 127 pixels.
To move mouse cursor 300 pixels you should invoke it 3 times:
MoveMouseRelative(0,100);MoveMouseRelative(0,100);MoveMouseRelative(0,100)

Related

How to make repeated intermitten movement while holding "x" key --> LUA --- LOGITECH

I got this code that makes my mouse drag down if i press mouse 5 while numlock is active, but i want to make my mouse go right and press D at same time for half a second then switch to mouse left and press A at same time and keep repeating this untill i stop pressing mouse5 --- Mouse 5 is IsMouseButtonPressed(5) if youre not familiar with the API
function OnEvent(event, arg)
if IsKeyLockOn("numlock" )then
if IsMouseButtonPressed(5) then
repeat
MoveMouseRelative(0,5)
Sleep(5)
until not IsMouseButtonPressed(1)
end
end
end
How do i make it
Try this code:
local keydir = {[-1]="A", [1]="D"}
local speed = 1
function OnEvent(event, arg)
if event == "MOUSE_BUTTON_PRESSED" and arg == 5 and IsKeyLockOn("numlock") then
local dir = -1
repeat
dir = -dir
local time0 = GetRunningTime()
repeat
MoveMouseRelative(dir*speed, 0)
Sleep(15)
until GetRunningTime() - time0 > 500
PressAndReleaseKey(keydir[dir])
Sleep(15)
until not IsMouseButtonPressed(5)
end
end

How i use the same keypress for 2 variables?(LUA)

EXAMPLE: If i press the M6 button, my cursor go to a X place, if i press the M6 button again he goes to a Y place, how can i does this alternation ?
local TOPX, TOPY, MIDX, MIDY
TOPX = 59305 -- Top side X
TOPY = 54527 -- Top side Y
MIDX = 61764 -- Mid lane X
MIDY = 58683 -- Mid lane Y
function OnEvent(event, arg)
for n = 1,2
do
if event == "MOUSE_BUTTON_RELEASED" and arg == 6 then
MoveMouseTo(MIDX, MIDY);
for n = 2,4
do
if
event == "MOUSE_BUTTON_RELEASED" and arg == 6 then
MoveMouseTo(TOPX, TOPY);
end
end
end
end
end
One way to do it would be having an outter flag boolean variable which would allow you to identify the state of the M6 button strokes.
local buttonPressedOnce = true
function onEvent(event, arg)
-- Check if the button is the one we desire.
if (event == "MOUSE_BUTTON_RELEASED" and arg == 6) then
if (buttonPressedOnce) then
-- execute X update code
else
-- execute Y update code
end
buttonPressedOnce = not(buttonPressedOnce)
end
end
Explanation: The first press will always be the X axis update code, so we initialize the flag as true, and with each click call we proceed to update the flag value, making sure each button stroke will switch to the desired state.

Lua script loop keys press

I have a logitech g102 mouse and i want to create a lua script which will press a different key every time i press the mouse button 4. Specifically i want each time i click the mouse button 4 to loop around 3 keyboard strokes (5,6,7). So if i click the first time my mouse button 4 it will press the number 3, the second time the number 4, third time number 5 and then repeat this as many times i press mouse button 4. I have already tried some code but didnt get anywhere. Can somebody help me?
local keys = {"h", "e", "l", "l", "o"} -- cycle of keys
local idx
local tm = -math.huge
function OnEvent(event, arg)
if event == "MOUSE_BUTTON_PRESSED" and arg == 4 then
if GetRunningTime() - tm > 2000 then
idx = 0
end
idx = idx % #keys + 1
PressKey(keys[idx])
elseif event == "MOUSE_BUTTON_RELEASED" and arg == 4 then
ReleaseKey(keys[idx])
tm = GetRunningTime()
end
end

How to make an object move from top to bottom in Lua?

In a game I'm making, there's a square where the game is happening inside. I want the circles that spawn in to start at the top and travel down to the bottom. In the original program, they travel from left to right. How would I go about this? I don't know how to get it to start at the top and travel down instead of starting at the left and traveling to the right.
Here's the original code (I know it's a lot, sorry). I am trying to help a friend.
--Made by Joms or /u/jomy582
--Please credit me if using this in a battle.
spawntimer = 0
timerspawn = 0
storedtime = Time.time
bullets = {}
bulletsbig = {}
bulletswarn = {}
dir = 1
bigheight = {33, 98}
function Update()
spawntimer = spawntimer + (Time.dt*Time.mult)
timerspawn = timerspawn + (Time.dt*Time.mult)
--normal bullets
--change the number in the if statement to make them spawn more frequently/less frequently
--EX: spawntimer > 0.2 spawns them pretty fast
--EX2: spawntimer > 1 spawns them pretty slow
--Make sure to change the subtraction method in the if statement
if spawntimer > 0.16 then
spawntimer = spawntimer-0.16
local bullet = CreateProjectile("bullet", -Arena.width/2, math.random(-Arena.height/2, Arena.height/2))
bullet.SetVar("deadly", true)
table.insert(bullets, bullet)
end
--warning. spawns a warning every 5 seconds
--You could change it, but that may cause some bugs
if timerspawn > 2.2 then
timerspawn = timerspawn-2.2
dir = math.random(1,2)
local bulletwarn = CreateProjectile("warning", 0, Arena.height/2 - bigheight[dir])
bulletwarn.SetVar("warningtimer", 0)
bulletwarn.SetVar("soundtimer", 0)
bulletwarn.SetVar("animtimer", 0)
bulletwarn.SetVar("anim", 1)
table.insert(bulletswarn, bulletwarn)
end
--controlling normal bullets
--a simple method that moves the bullets 1 pixel each frame
--you can change it by editing the bullet.move
--EX: bullet.Move(5*Time.mult, 0) moves the bullets 5 pixels each frame
for i=1, #bullets do
local bullet = bullets[i]
if bullet.isactive then
bullet.Move(2*Time.mult, 0)
if bullet.y > -Arena.height/2 then
bullet.Remove()
end
end
end
--controlling warning timer
--a method that controls the warning timer
for i=1, #bulletswarn do
local bullet = bulletswarn[i]
if bullet.isactive then
local warningtimer = bullet.GetVar("warningtimer") + (Time.mult*Time.dt)
local soundtimer = bullet.GetVar("soundtimer") + (Time.mult*Time.dt)
local animtimer = bullet.GetVar("animtimer") + (Time.mult*Time.dt)
local bulletSprite = bullet.sprite
local anim = bullet.GetVar("anim")
--flashing colors
--change the animtimer > TIME where time is how often you want the warning to blink
--warnings last for 3 seconds. Can change that as well
--to get different colors, find the rgb value of the color you want and insert them below
if animtimer > 0.08 then
animtimer = animtimer-0.08
if anim == 1 then
local r = 0 --put Red value here
local g = 0 --put Green value here
local b = 169 --put Blue value here
bulletSprite.color = {r/255, g/255, b/255} -- changes the color to whatever you'd like. Uses RGB.
bullet.SetVar("anim", 2)
elseif anim == 2 then
local r = 0 --put Red value here
local g = 0 --put Green value here
local b = 255 --put Blue value here
bulletSprite.color = {r/255, g/255, b/255} -- changes the color to whatever you'd like. Uses RGB.
bullet.SetVar("anim", 1)
end
end
--plays a timer evert 10 frames for 3 seconds.
--change the soundname to change the sound
--change the soundtimer > 0.16 to change how often it plays
--can change how long it lasts as well by changing the less than statement
if soundtimer > 0.10 then
soundtimer = soundtimer-0.10
Audio.PlaySound("alarm")
end
--this controls when to spawn the bullets
--change the statement to change how long the warning timer is
if warningtimer > 2 then
warningtimer = warningtimer-2
Audio.PlaySound("shoot")
local bullet1 = CreateProjectile("leftbigbullet", Arena.width/2+30, Arena.height/2 - bigheight[dir]) --where to spawn them
bullet1.SetVar("speed", -4) --how fast
bullet1.SetVar("deadly", true)
local bullet2 = CreateProjectile("rightbigbullet", -Arena.width/2-30, Arena.height/2 - bigheight[dir]) --where to spawn them
bullet2.SetVar("speed", 4) --how fast
bullet2.SetVar("deadly", true)
table.insert(bulletsbig, bullet1)
table.insert(bulletsbig, bullet2)
bullet.Remove()
end
bullet.SetVar("warningtimer", warningtimer)
bullet.SetVar("animtimer", animtimer)
bullet.SetVar("soundtimer", soundtimer)
end
end
--controlling big bullets
--this method controls the big bullets
for i=1, #bulletsbig do
local bullet = bulletsbig[i]
if bullet.isactive then
local speed = bullet.GetVar("speed")
bullet.SendToBottom()
bullet.Move(speed*Time.mult, 0)
if bullet.absx > 700 or bullet.absx < -70 then
bullet.Remove()
end
end
end
end
function OnHit(bullet)
if(bullet.getVar("deadly")) then
Player.Hurt(3)
end
end

Balloon game with Corona SDK

I am totally new to game development for iPhone/iPad.
I have got my code working and all the 10 balloons are floating in the air but I have a few questions:
The balloons should be in the sequence or random order. They move the tendon to the edge and then the player should move back the balloons to the right place with the mouse. How?
What are the right dimensional numbers (x, y) so my balloons are equally displayed and positioned on the screen?
My random function keeps popping out more balloons by simple click.
I would like the user to perform some math operations, for instance add two random balloons and display the right answer on the screen so the result can move back to the right edge of balloon placement. how to code this? How can I use 2 different level of difficulties? (L1, L2)
How to make my balloons to move to the different edges on the screen?
How can a user move back the balloons with mouse to the right places?
How can I tie my balloons to a rope (horizontally)? so the user can make a choice.
My background image is about 3MB original(1024 x 768) to match well with iPad resolution, can I change the size without affecting the display in iPad?
I feel like the local balloon1, 2, 3, is repeated too much, and same goes to moveBalloon and applyLinear. Is there a way of shortening them? or is it normal since there are 10 balloons?
I have added sound to the first balloon by simple click, should duplicate the same function for the rest of the 9 balloons (another mess)? I will use the same sound to all.
Your feedback is much appreciated.
If you want multiple balloons, it would be MUCH MUCH easier to use a table. You can have as many baloons as you want with very little effort.
Balloons = {} -- track all baloons
Function addBalloon(x,y,xVel,yVel)
Tab = {x = x, y = y, vel = {x = xVel, y = yVel}}
Table.insert(balloons,tab)
End
Function moveAllBalloons()
For_,i in pairs(balloons) do
i.x = i.x + i.vel.x
i.y = i.y + i.vel.y
End
End
Function isPlaying
For _,i in pairs(balloons)
If --[[mouse.x]] <= i.x - (balloon.width/2) and --[[other parameters here]] then
PlaySound
End
End
End
for different difficulties, you can do something like
if L1 then
Num1 = math.random(3,15)
Num2 = math.random(3,15
OpFind = math.random(3)
If opfind == 1 then
Operation = "+"
Elseif opfind == 2 then
Operation = "-"
Elseif opfind ==3 then
Operation = "*"
End
ElseIf L2 then
num1 = math.random(7,50)
Num2 = math.random(7,50)
OpFind = math.random(4)
If opfind == 1 then
Operation = "^"
Elseif opfind == 2 then
Operation = "%"
Elseif opfind ==3 then
Operation = "*"
Elseif opfind == 4 then
Operation == "/"
End
End

Resources