Posted 23 April 2014 - 10:45 PM
Stuff[1] = coroutine.resume()
Stuff[1]
'=' expected.Is there any way of doing something similar to this, executing a line stored in a table?
Stuff[1] = coroutine.resume()
Stuff[1]
'=' expected.Stuff[1]()
It's a function pointer like any other, why would calling it be any different? :P/>
Stuff[1] = coroutine.resume()
would store the result of coroutine.resume() in your table, not the function iself.
local modem = peripheral.wrap('top')
if not modem.isOpen(os.getID()) then
modem.open(os.getID())
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][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 event()
local r = ''
local event = {os.pullEvent()}
if event[1] == "modem_message" then
recieveChat(event)
elseif event[1] == "char" then
r = r..event[2]
elseif event[1] == "mouse_click" then
resolve(event)
elseif event[1] == "key" and event[2] == 28 then
sendChat(r, sendID)
end
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
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, coroutine.resume(newContact))
local bcontact = newButton(10, 20, 5, 10, "New Contact", colors.lime, colors.red, coroutine.resume(newContact))
won't work as intended. Same as above, your passing the results of coroutine.resume(newContact), not the function itself.
local bcontact = newButton(10, 20, 5, 10, "New Contact", colors.lime, colors.red, function() coroutine.resume(newContact) end)