This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Elrol_Arrowsend's profile picture

[1.7.10] Mouse clicking working 5% of the time

Started by Elrol_Arrowsend, 03 March 2018 - 03:30 AM
Elrol_Arrowsend #1
Posted 03 March 2018 - 04:30 AM
I have been following a tutorial online (it stops after the fourth video, for some reason) however when I click on the 'buttons' most of the time it wont work, I will have to click it a few times for it to register, and I am not sure why. I added a debug print method, and clicked on each symbol on the main screen, and the check should be working 100%. I realize that this is for a much older version of mc, but I figure that I could get some help finding the error or making my code work better.

This is the Main startup file, the other files are for apis and a config for colors and what not: https://pastebin.com/7wUekCzm

If any more information is needed, please let me know so that I can get this problem solved asap

SquidDev #2
Posted 03 March 2018 - 07:56 AM
The issue lies in where you are pulling the event. If you look at your mouseClick function:

local function menuClick()
    for k,v in pairs(menu) do
        local e = {os.pullEvent()}
        if e[1] == "mouse_click" then
This effectively looks at the first button, waits for some event and checks if you clicked that button. Then it looks at the second button, waits for another click and does the same thing. What you want to do instead is fetch one event and compare every button.

In order to do that, we'll just pass the event as an argument:


local function menuClick(button, x, y)
  for k,v in pairs(menu) do
    if button == 1 then
      --# Same as before, but using button/x/y instead of e[2]/e[3]/e[4]
And then inside main, call menuClick(button, x, y) instead. You'll obviously need to do the same for securityClick too.
Elrol_Arrowsend #3
Posted 03 March 2018 - 02:20 PM
Ok, firstly, I would like to thank you for helping out with some of the code, however it did not solve the issue, or I am not doing something right, I added the mentioned fix to the code, and I still saw the same issue. I then added a new function for testing the params (button, x, y) to make sure they were coming out right, and now when I click on the menu or secutrity menu, it calls a nil at the line where i have the test function.

Here is the link to the updated code: https://pastebin.com/umTvRJ4m

As well as a picture of the error:
Bomb Bloke #4
Posted 04 March 2018 - 06:42 AM
That's a matter of scope - when your script executes the function definition for your securityClick() function (which contains line 127), it hasn't yet reached the function definition for the function that line wants to call (testClick(), which is defined lower down in the script).

Because "testClick" is an undefined variable at the time the securityClick function is built, securityClick expects testClick to exist within the global scope. However, when you later define testClick, you do so in the local scope - hence when you later call the securityClick function, it fails to find testClick and you get your "attempt to call nil" error.

The solution is to move your testClick function definition up above the definitions of any functions that would want to refer to it.
Lupus590 #5
Posted 04 March 2018 - 12:18 PM
The solution is to move your testClick function definition up above the definitions of any functions that would want to refer to it.

Or you could do this, but Bomb Bloke's solution is the better one.


local func1 --# tells Lua that there will be a thing called func1 here
local function func2()
  --# do stuff
  func1() --# here we will use func1
end
--# below line may actually have to be
--# func1 = function()
function func1() --# tell Lua what func1 is
  print("it works")
end
func2()
Edited on 04 March 2018 - 11:18 AM
Elrol_Arrowsend #6
Posted 04 March 2018 - 07:53 PM
I always forget about that. Im used to java. I moved the function above the other and it is called. It shows the right coords when i click however it still doesnt do what one would expect. I think the issue is with the menuClick and securityClick because the test click works and shows the right coords but the menu stays up so the if statements dont work.

–Edit–

Managed to get it working, somehow I had y = 1 on most of the tables, so when it went to look for the click it was looking in the wrong place. Thanks for the help.
Edited on 06 March 2018 - 08:58 PM