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

Pls halp buttons

Started by SeaGemGames, 16 July 2017 - 10:29 PM
SeaGemGames #1
Posted 17 July 2017 - 12:29 AM
SOLVED
Hey, I need help with a button, it shows, but doesn't work, and doesn't throw errors.


function fwrite(path, text)
local file = assert(io.open(path, "w"))
file:write(text)
file:close()
end
function writeFromTable(path,t)
local text = ""
for _, line in pairs(t) do
  text = text..line.."\n"
end
fwrite(path,text)
end
local function getTable(path)
if fs.exists(path) then
  local file = io.open(path, "r")
  local lines = {}
  local i = 1
  local line = file:read("*l")
  while line ~= nil do
   lines[i] = line
   line = file:read("*l")
   i = i + 1
  end
  file:close()
  return lines
end
return {}
end
function replaceLine(path,n,text)
local lines = getTable(path)
lines[n] = text
writeFromTable(path,lines)
end
function fappend(path,text)
local file = assert(io.open(path,"a"))
file:write(text.."\n")
file:close()
end
local config = getTable("os/config")
local termmode = tonumber(string.sub(config[1],string.find(config[1],":")+2))
local x1,y1 = 26,1
local option = "Toggle"
local function button()
term.setCursorPos(x1,y1)
term.setBackgroundColour(colours.black)
term.setTextColour(colours.white)
term.write(option)
end
function getMode()
return termmode
end
term.setBackgroundColour(colours.blue)
term.clear()
term.setTextColour(colours.white)
term.setCursorPos(1,1)
if termmode == 0 then
  term.write("Terminal Mode: Native")
else
  term.write("Terminal Mode: Peripheral")
end
button()
while true do
local event, button, cx, cy = os.pullEvent("mouse_click")
if cx >= x1 and cx < option:len() and cy == y1 and button == 1 then
  print("xyz")
  if termmode == 0 then
   print("abc")
   replaceLine(config,1,"1 termmode : 1")
   termmode = 1
  elseif termmode == 1 then
   print("123")
   replaceLine(config,1,"1 termmode : 0")
   termmode = 0
  end
end
end
Edited on 17 July 2017 - 05:55 AM
SeaGemGames #2
Posted 17 July 2017 - 07:54 AM
Simple fix, sorry for this post. I knew it was this line.

if cx >= x1 and cx < option:len() and cy == y1 and button == 1 then
should be

if cx >= x and cx < x+option:len() and cy == y and button == 1 then