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

Checkbox does not work! Help please!

Started by LabyStudio, 14 April 2013 - 11:38 AM
LabyStudio #1
Posted 14 April 2013 - 01:38 PM
My code does not work! :(/>/>
(Checkbox)
-- Checkbox does not work!
-- Help please
term.clear()
term.setCursorPos(1,1)
term.setTextColor(colors.yellow)
print("Checkbox does not work")
term.setCursorPos(1,2)
print("Help please")
term.setCursorPos(40,1)
print("Toggle --> X")
term.setTextColor(colors.white)


---------------- Code --------------------
while true do
local event, button, X, Y = os.pullEvent("mouse_click")
XY = X..","..Y

local V = -5
local Z = 0
if XY == "51,1" and button == 1 then
if Z > V then
V = V + 10
term.clear()
term.setCursorPos(1,1)
print("+")
term.setCursorPos(51,1)
print("X")

end
end
if XY == "51,1" and button == 1 then
if Z < V then
V = V - 10
term.clear()
term.setCursorPos(1,1)
print("-")

term.setCursorPos(51,1)
print("X")

end
end
end



Help please!
Smiley43210 #2
Posted 14 April 2013 - 10:44 PM
Ok, hold on.
You need an endif in there, and you can remove one if.

Corrected
-- Checkbox does not work!
-- Help please

-- We will define V outside of the while loop so that it doesn't keep getting set each time the code loops.
-- That's partly why it wasn't changing: You added 10, but as soon as the while loop started over again, you had
-- "local V = -1", resetting it BACK to -5. Oh and that's aside from the fact that it was a local variable.
local V = -5

term.clear()
term.setCursorPos(1,1)
term.setTextColor(colors.yellow)
print("Checkbox does not work")
term.setCursorPos(1,2)
print("Help please")
term.setCursorPos(40,1)
print("Toggle --> X")
term.setTextColor(colors.white)


---------------- Code --------------------
while true do
local event, button, X, Y = os.pullEvent("mouse_click")
XY = X..","..Y

local Z = 0
if XY == "51,1" and button == 1 then
if Z > V then
V = V + 10
term.clear()
term.setCursorPos(1,1)
print("+")
term.setCursorPos(51,1)
print("X")

elseif Z < V then

V = V - 10
term.clear()
term.setCursorPos(1,1)
print("-")

term.setCursorPos(51,1)
print("X")

end
end
end

Note: I have not made any other changes to make it simpler or anything of the sort. I've only changed it enough to make it work.
1lann #3
Posted 14 April 2013 - 10:55 PM
You should do to string(X)..","..tostring(Y)

Also it would be better if you do checks seperately without converting them to a string
Ex: if X == 51 and Y == 1 then