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

[Lua][Question]Reading messages.

Started by exploder, 21 October 2012 - 06:18 PM
exploder #1
Posted 21 October 2012 - 08:18 PM
Hello computercraft community.

I am trying to make program that allows user to write messages,save them and show later, but I'm getting an error:startup:19: attemt to call a string (19 line is print("message "..message)

But I just can't get that right, I have red("I don't know if that's a verb because I don't know English that well.") the wiki and tutorials in this forums, but still I can't get it right.

And could you please show me how to make computer detect if there is message or not, because if there is no message then I want computer to say that to user(I tried fs.exists, but can't figure it out either.).


SpoilerSorry, for posting full code, but else you wouldn't understand what I'm trying to do, but I removed some parts of it to make it shorter.



id = os.computerID()
pass = "gta"


function checkm() --Here computer opens and prints the message (I think here is the mistake)
term.restore()
file = fs.open("messages/message","r")
local text = {}
local print = file.readLine()
repeat
table.insert(text,print)
line = file.readLine()
until line == nil
file.close()
local message = text[1]
term.clear()
term.setCursorPos(1,1)
print("Message "..message) --19 line with the error.
end


function message() --Here user writes the message which gets saved into the computer.This seems to work fine, because everything gets really saved.
term.clear()
term.setCursorPos(1,1)
print("Monitor can't work, while accessing computer.")
term.restore()
term.clear()
term.setCursorPos(1,1)
fs.makeDir("messages")
print("Enter your message")
write("Text:")
local text = read()
local file = fs.open("messages/message","w")
file.writeLine(text)
file.close()
term.clear()
term.setCursorPos(1,1)
print("Message written")
end


mon = peripheral.wrap("left")
term.redirect(mon)
term.clear()
term.setCursorPos(1,1)

while true do
local time = os.time()
time = textutils.formatTime(time, true)

print[[		
1.Access Computer.
2.Shutdown.
3.Leave a message.
4.Check messages.]]

term.setCursorPos(1,10) print("Computer ID:"..id.."") term.setCursorPos(26,10) print("Time "..time)

os.startTimer(0.5) event, param1, param2 = os.pullEvent() if event == "char" and param1 == "1" then access() elseif event == "char" and param1 == "2" then shutdown() elseif event == "char" and param1 == "3" then message() elseif event == "char" and param1 == "4" then checkm() end

term.clear()
term.setCursorPos(1,1)
end


If you need more details, just ask.

Thank you.
sjele #2
Posted 21 October 2012 - 08:20 PM
On line 7 you are making print a string.

local print = file.readLine()

Spoiler

id = os.computerID()
pass = "gta"

function checkm() --Here computer opens and prints the message (I think here is the mistake)
term.restore()
file = fs.open("messages/message","r")
local text = {}
local texts = file.readLine()
repeat
table.insert(text,texts)
line = file.readLine()
until line == nil
file.close()
local message = text[1]
term.clear()
term.setCursorPos(1,1)
print("Message "..message) --19 line with the error.
end

function message() --Here user writes the message which gets saved into the computer.This seems to work fine, because everything gets really saved.
term.clear()
term.setCursorPos(1,1)
print("Monitor can't work, while accessing computer.")
term.restore()
term.clear()
term.setCursorPos(1,1)
fs.makeDir("messages")
print("Enter your message")
write("Text:")
local text = read()
local file = fs.open("messages/message","w")
file.writeLine(text)
file.close()
term.clear()
term.setCursorPos(1,1)
print("Message written")
end

mon = peripheral.wrap("left")
term.redirect(mon)
term.clear()
term.setCursorPos(1,1)
while true do
local time = os.time()
time = textutils.formatTime(time, true)
print[[		
1.Access Computer.
2.Shutdown.
3.Leave a message.
4.Check messages.]]
term.setCursorPos(1,10) print("Computer ID:"..id.."") term.setCursorPos(26,10) print("Time "..time)
os.startTimer(0.5) event, param1, param2 = os.pullEvent() if event == "char" and param1 == "1" then access() elseif event == "char" and param1 == "2" then shutdown() elseif event == "char" and param1 == "3" then message() elseif event == "char" and param1 == "4" then checkm() end
term.clear()
term.setCursorPos(1,1)
end
exploder #3
Posted 21 October 2012 - 09:08 PM
On line 7 you are making print a string.

local print = file.readLine()

Spoiler

id = os.computerID()
pass = "gta"

function checkm() --Here computer opens and prints the message (I think here is the mistake)
term.restore()
file = fs.open("messages/message","r")
local text = {}
local texts = file.readLine()
repeat
table.insert(text,texts)
line = file.readLine()
until line == nil
file.close()
local message = text[1]
term.clear()
term.setCursorPos(1,1)
print("Message "..message) --19 line with the error.
end

function message() --Here user writes the message which gets saved into the computer.This seems to work fine, because everything gets really saved.
term.clear()
term.setCursorPos(1,1)
print("Monitor can't work, while accessing computer.")
term.restore()
term.clear()
term.setCursorPos(1,1)
fs.makeDir("messages")
print("Enter your message")
write("Text:")
local text = read()
local file = fs.open("messages/message","w")
file.writeLine(text)
file.close()
term.clear()
term.setCursorPos(1,1)
print("Message written")
end

mon = peripheral.wrap("left")
term.redirect(mon)
term.clear()
term.setCursorPos(1,1)
while true do
local time = os.time()
time = textutils.formatTime(time, true)
print[[		
1.Access Computer.
2.Shutdown.
3.Leave a message.
4.Check messages.]]
term.setCursorPos(1,10) print("Computer ID:"..id.."") term.setCursorPos(26,10) print("Time "..time)
os.startTimer(0.5) event, param1, param2 = os.pullEvent() if event == "char" and param1 == "1" then access() elseif event == "char" and param1 == "2" then shutdown() elseif event == "char" and param1 == "3" then message() elseif event == "char" and param1 == "4" then checkm() end
term.clear()
term.setCursorPos(1,1)
end

Thank you, this worked, but could somebody help me again, because I have updated my code and I can't get os.pullEvent() to work this time.

The problem is when you write the message you can choose to continue or show message, but it doesn't seem to work because you can press any button on keyboard and it drops you back to menu, just this time the menu shows in terminal, what is the problem now?

Spoilerfunction message() –Here user writes the message which gets saved into the computer.This seems to work fine, because everything gets really saved.
term.clear()
term.setCursorPos(1,1)
print("Monitor can't work, while accessing computer.")
term.restore()
term.clear()
term.setCursorPos(1,1)
fs.makeDir("messages")
print("Enter your message")
write("Text:")
local text = read()
local file = fs.open("messages/message","w")
file.writeLine(text)
file.close()
term.clear()
term.setCursorPos(1,1)
print(" Message written.")
term.setCursorPos(1,13)
print(" Press [1] to continue, or press [2]")
print(" to show the message. ")
event, param1, param2 = os.pullEvent("char")
if param2 == "e" then reb()
elseif param2 == "c" then showm()
end
end
ChunLing #4
Posted 21 October 2012 - 09:34 PM
"char" events only return the event "char" and a single parameter, the character. You're trying to use the second parameter, but it never exists and is always nil.
exploder #5
Posted 21 October 2012 - 11:57 PM
"char ents only return the event "char" and a single parameter, the character. You're trying to use the second parameter, but it never exists and is always nil.

Thank you for replying, but could you explain this a bit in more detail because English is not really my native language.

Thank you.
ChunLing #6
Posted 22 October 2012 - 12:43 AM
From your code it looks like you already were able to fix this. Is there still a problem?
os.startTimer(0.5)
event, param1, param2 = os.pullEvent()
if event == "char" and param1 == "1" then access()
elseif event == "char" and param1 == "2" then shutdown()
elseif event == "char" and param1 == "3" then message()
elseif event == "char" and param1 == "4" then checkm()
end
exploder #7
Posted 22 October 2012 - 08:32 AM
From your code it looks like you already were able to fix this. Is there still a problem?
os.startTimer(0.5)
event, param1, param2 = os.pullEvent()
if event == "char" and param1 == "1" then access()
elseif event == "char" and param1 == "2" then shutdown()
elseif event == "char" and param1 == "3" then message()
elseif event == "char" and param1 == "4" then checkm()
end

Yes, I tried that as well, but still can't get it working correct because it still gets me to main menu (just in terminal) by pressing any button.
ChunLing #8
Posted 22 October 2012 - 08:44 AM
Oh, right. You need to restrict your event pulls to chars, because key events arrive first. Use "os.pullEvent("char") " so that you don't pull key events, or if you want to use that timer event then use a repeat until loop that only exits when the timer fires.
exploder #9
Posted 22 October 2012 - 02:03 PM
Oh, right. You need to restrict your event pulls to chars, because key events arrive first. Use "os.pullEvent("char") " so that you don't pull key events, or if you want to use that timer event then use a repeat until loop that only exits when the timer fires.

Thank you, now this works.