Did some LUA work this week just for a small personal project!
After being asked in multiple job applications if I have any experience with LUA scripting, I decided it may be a good week to learn it, because it also had some relevance to my current hobbies (gaming). With the re-launch of Diablo II coming out, I decided to play hardcore on a private server, and a friend of mine had a good idea, to create a LUA script that leaves a game when you press a button on your mouse to prevent your character from being deleted if you die.
This was a really fun idea because it is a simple command, that does a lot for us in our fun setting. I love learning new things that have relevance, it is quite a fun little puzzle for me! So we started out by just trying to create an on-mouse-button script to state the location of the xy position of the mouse, because we wanted to make sure it was reading correctly and ending the loop without crashing the PC. We were going to be creating a script that presses escape -> up arrow -> enter, and then breaks the loop. If the loop wasn’t broken, it wouldn’t be good.
function OnEvent(event, arg, mouse)
if (event == "MOUSE_BUTTON_PRESSED" and arg == 6) then
repeat
x, y = GetMousePosition()
OutputLogMessage("Mouse is at %d, %d\n", x, y)
end
end
This is where we started. It was pretty simple after doing some googling and reading stack overflow, as well as the mouse manual to find out which button did what for his mouse. This one worked flawlessly so we then added our actual script for what we wanted to do, and surprisingly it worked first try AND broke the loop.
function OnEvent(event, arg, mouse)
if (event == "MOUSE_BUTTON_PRESSED" and arg == 6) then
repeat
--x, y = GetMousePosition()
--OutputLogMessage("Mouse is at %d, %d\n", x, y)
PressKey ("escape")
ReleaseKey ("escape")
PressKey ("up")
ReleaseKey ("up")
PressKey ("enter")
ReleaseKey ("enter")
until IsMouseButtonPressed(6)
end
end
We left the original XY position in just to make sure that we can do future testing with different mice. Such as mine, with is the Logitech G-600 Gaming Mouse, and that has been a little bit more of a nightmare to try. We are still working on the solution for this one because it is not the default commands, and the manual from Logitech for LUA scripting isn’t quite accurate for this mouse.
function OnEvent(event, arg, mouse)
if (event == "G_PRESSED" and arg == 9) then
repeat
--x, y = GetMousePosition()
--OutputLogMessage("Mouse is at %d, %d\n", x, y)
PressKey ("escape")
ReleaseKey ("escape")
PressKey ("up")
ReleaseKey ("up")
PressKey ("enter")
ReleaseKey ("enter")
until IsGPressed(9)
end
end
We started trying this out, and the G_Pressed arg == 9 was starting the loop, but the until IsGPressed(9) was not exiting the loop and was causing a crash. We tried a few different commands other than IsGPressed(9) but none seemed to break the loop. The manual from Logitech has almost every button mislabeled so our next task is to make a command to see which mouse button is read per button on the mouse. So without writing proper code, it would look something like this.
function OnEvent(event, arg, mouse)
if (event == "G_PRESSED" and arg == 1) then
repeat
OutputLogMessage("Mouse button 1")
until IsGPressed(1)
if (event == "G_PRESSED" and arg == 2) then
repeat
OutputLogMessage("Mouse button 2")
until IsGPressed(2)end
end
From this we will do all 20 buttons and check our response, but I know that this original code will need to be changed. I am still new to LUA so I have a lot of work to do, but once you start coding you notice a lot of similarities, it is just syntax to search for after that. This will be a fun project to see if we can complete!