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

Window Error

Started by KingofGamesYami, 24 April 2014 - 01:21 AM
KingofGamesYami #1
Posted 24 April 2014 - 03:21 AM
I have no idea how to deal with this…

window:247: bad argument: double expected, got nil
I'm trying to debug this:
Spoiler

local modem = peripheral.wrap('back')
if not modem.isOpen(os.getComputerID()) then
 modem.open(os.getComputerID())
end

function newButton( maxx, minx, miny, maxy, name, active, inactive, func )
 buttons[#buttons+1] = {}
  local b = buttons[#buttons]
  b['mx'] = maxx
  b['sx'] = minx
  b['my'] = maxy
  b['sy'] = miny
  b['name'] = name
  b['ic'] = inactive
  b['ac'] = active
  b['b'] = false
  b['clicked'] = func
  return #buttons
end

function showButtons()
 for i = 1, #buttons do
  if buttons[i]['b'] then
   term.setBackgroundColor(buttons[i]['ic'])
   term.setCursorPos(buttons[i]['sx'], buttons[i]['sy'])
   for l = 0, buttons[i]['mx'] - buttons[i]['sx'], 1 do
    for m = 0, buttons[i]['my'] - buttons[i]['sy'], 1 do 
     term.setCursorPos(buttons[i]['sx'] + l, buttons[i]['my'] + m)
     term.write(' ')
    end
   end
   term.setCursorPos((buttons[i]['mx'] + buttons[i]['sx'] - string.len(buttons[i]['name'])) / 2, (buttons[i]['my'] + buttons[i]['sy'] - 1) / 2)
   term.write(buttons[i]['name'])
  end
 end
end

function resolve(event)
 local x, y, clicked = event[2], event[3]
 for i = 1, #buttons do
  if buttons[i]['b'] == true and buttons[i][mx] >= x and buttons[i][sx] <= x and buttons[i][my] >= y and buttons[i][sy] <= y then
   clicked = i 
  end
 end
 buttons[clicked]['clicked']()
end

function sendChat(text, sendID)
 while true do
  local w, h = term.getSize()
  term.setCursorPos(x, y)
  local msg = getKeys()
  local modem = peripheral.wrap('top')
  modem.transmit(sendID, os.getID(), msg)
  coroutine.yield()
 end
end

function receiveChat(event)
 while true do
  local x, y = term.getCursorPos()
  local w, h = term.getSize()
  if y >= h - 2 then
   term.clear()
   term.setCursorPos(1, 1)
  else
   term.setCursorPos(1, y+1)
  end
  print(contacts[event[3]]..": "..event[5])
 end
end

chat = coroutine.create(sendChat())
getChat = coroutine.create(receiveChat())

local contacts

if fs.exists("Chat") and fs.isDir("Chat") then
 local file = fs.open("Chat/contacts", "r")
 local data = file.readAll()
 contacts = textutils.unserialize(data)
 file.close()
else
 fs.mkDir("Chat")
 local file = fs.open("Chat/contacts", "w")
 file.writeLine("{ }")
 file.close()
 contacts = {}
end

function createContact()
 while true do 
  term.write("Name: ")
  local name = read()
  term.write("Computer ID: ")
  local ID = read()
  contacts[ID] = name
  file = fs.open("Chat/contacts", "w")
  file.write(textutils.serialize(contacts))
  file.close()
  coroutine.yeild()
 end
end

newContact = coroutine.create(createContact())

function reset()
 term.setBackgroundColor(colors.red)
 term.clear()
 term.setBackgroundColor(colors.blue)
 local w, h = term.getSize()
 for i = 1, h do
  term.setCursorPos(1, i)
  term.write(' ')
  term.setCursorPos(w, i)
  term.write(' ')
 end
 for i = 1, w do
  term.setCursorPos(i, 1)
  term.write(' ')
  term.setCursorPos(i, h/2 + 1)
  term.write(' ')
  term.setCursorPos(i, h)
  term.write(' ')
 end
 term.setBackgroundColor(colors.red)
 term.setCursorPos(2, h/2)
end

function event()
 local r = ''
 local event = {os.pullEvent()}
 if event[1] == "modem_message" then
  recieveChat(event)
  return event[4]
 elseif event[1] == "char" then
  r = r..event[2]
  return false
 elseif event[1] == "mouse_click" then
  resolve(event)
  return false
 elseif event[1] == "key" and event[2] == 28 then
  sendChat(r, sendID)
  return false
 else
  return false
 end
end

local w, h = term.getSize()
local text = "Enter a Connection ID"
term.setCursorPos((w - #text) / 2, (h - #text) / 2)
term.write(text)
term.setCursorPos(1, h)

coroutine.resume(chat, contact)

local bcontact = newButton(10, 20, 5, 10, "New Contact", colors.lime, colors.red, function() coroutine.resume(newContact) end)
…which I realize does nothing useful at this time. However I hope to create a chat program with two "windows", which haven't been coded as of yet.
Anavrins #2
Posted 24 April 2014 - 05:30 AM
At line 51, " term.setCursorPos(x, y) ", you haven't defined x and y anywhere in the sendChat() function.
CometWolf #3
Posted 24 April 2014 - 05:31 AM
Edit: ninja!

There's no x or y variable defined for your send chat function, which attempts to set the cursor at the variables x and y.
Edited on 24 April 2014 - 03:31 AM