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

Glitchy Program Behavior

Started by Scoptile, 31 July 2015 - 08:57 PM
Scoptile #1
Posted 31 July 2015 - 10:57 PM
I'm making a file explorer type thing, and it works how i want it to right now, however over time, as i look through the different directories, it starts to act really wierd. It started to open a ton of shells, and glitch out alot. i would like to know what is causing this, and how i can fix it. Thanks in advance :)/>
Lupus590 #2
Posted 31 July 2015 - 11:00 PM
Can you link the code on pastebin or github?
Edited on 31 July 2015 - 09:00 PM
Scoptile #3
Posted 01 August 2015 - 01:09 AM
Its short so ill put it in a spoiler:
Edit: Dont run this on an important computer. it might cause problems
Spoiler

function reset ()
term.clear()
term.setCursorPos(1,1)
end
function line ()
print("")
end
function printcolor (x,y)
term.setTextColor(y)
print(x)
term.setTextColor(colors.white)
end
dir = ""
pg = 0
s = 0
function main ()
reset()
x = fs.list(dir)
n = 1
while n <= 18 do
  if fs.isDir(dir.."/"..tostring(x[n+(pg*18)])) then term.setTextColor(colors.green) end
  if fs.exists(dir.."/"..tostring(x[n+(pg*18)])) then print("["..n+(pg*18).."] "..x[n+(pg*18)]) end
  n = n+1
  term.setTextColor(colors.white)
end
local function key ()
  while true do
   local event, key, isHeld = os.pullEvent("key")
   if key == keys.left and not isHeld then pg = pg-1 end
   if key == keys.right and not isHeld then pg = pg+1 end

   if key == keys.r then dir = "" end

   reset()
   main()
  end
end
local function mouse ()
  while true do
   local event, button, xm, ym = os.pullEvent("mouse_click")
   if not fs.isDir(dir.."/"..x[ym]) then shell.openTab(dir.."/"..x[ym])
   else dir = dir.."/"..x[ym] end
   main()
  end
end
parallel.waitForAny(key,mouse)
end
main()
Edited on 31 July 2015 - 11:40 PM
ardera #4
Posted 01 August 2015 - 03:55 PM
When you're calling "main" inside the "mouse" function, the old "key" and "mouse" functions are still active, and still listening. They still receive the events.
To fix this, don't call main(), just reload the "x" table and reprint, don't spawn another 2 listeners. (= another parallel.waitForAny(key, mouse))

Fixed version:

function reset ()
term.clear()
term.setCursorPos(1,1)
end
function line ()
print("")
end
function printcolor (x,y)
term.setTextColor(y)
print(x)
term.setTextColor(colors.white)
end
dir = ""
pg = 0
s = 0
function main ()
reset()
local function reload()
x = fs.list(dir)
n = 1
while n <= 18 do
  if fs.isDir(dir.."/"..tostring(x[n+(pg*18)])) then term.setTextColor(colors.green) end
  if fs.exists(dir.."/"..tostring(x[n+(pg*18)])) then print("["..n+(pg*18).."] "..x[n+(pg*18)]) end
  n = n+1
  term.setTextColor(colors.white)
end
end
local function key ()
  while true do
   local event, key, isHeld = os.pullEvent("key")
   if key == keys.left and not isHeld then pg = pg-1 end
   if key == keys.right and not isHeld then pg = pg+1 end
   if key == keys.r then dir = "" end
   reset()
   reload()
  end
end
local function mouse ()
  while true do
   local event, button, xm, ym = os.pullEvent("mouse_click")
   if not fs.isDir(dir.."/"..x[ym]) then shell.openTab(dir.."/"..x[ym])
   else dir = dir.."/"..x[ym] end
   reload()
  end
end
parallel.waitForAny(key,mouse)
end
main()
Scoptile #5
Posted 01 August 2015 - 06:42 PM
thanks!