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

[question] random number

Started by ewart4fun, 08 September 2012 - 04:35 AM
ewart4fun #1
Posted 08 September 2012 - 06:35 AM
hi everyone
i've made a program that allowes you to send a rednet mail to 1 host computer.
and the host computer saves the messages,
but at the end of the messages i get a random number does any1 know what causes it?


message sender

os.pullEvent = os.pullEventRaw
--config
menuname = "informationdesk"
programname = "startup"
updatername = "updater"
updaterpass = "hello"
shellpass = "peanut"
host = 6
function clear()
term.clear()
term.setCursorPos(1, 1)
end
local w,h = term.getSize()
function printCentred( y, s )
    local x = math.floor((w - string.len(s)) / 2)
    term.setCursorPos(x,y)
    term.clearLine()
    term.write( s )
end
local nOption = 1
-- Display menu
local function drawMenu()
    term.clear()
    term.setCursorPos(1,1)
    term.write(menuname)
    term.setCursorPos(w-11,1)
    if nOption == 1 then
    term.write( "OPTION 1" )
    elseif nOption == 2 then
    term.write( "OPTION 2" )
    else
    term.write( "OPTION 3" )
    end
end
-- Display the frontend
term.clear()
local function drawFrontend()
    clear()
    printCentred( math.floor(h/2) - 3, "" )
    printCentred( math.floor(h/2) - 2, "SELECT AN OPTION" )
    printCentred( math.floor(h/2) - 1, "" )
    printCentred( math.floor(h/2) + 0, ((nOption == 1) and "[ Send message ]") or "Send message" )
    printCentred( math.floor(h/2) + 1, ((nOption == 2) and "[ Main menu ]") or "Main menu" )
printCentred( math.floor(h/2) + 2, "" )
end
drawMenu()
drawFrontend()
while true do
drawMenu()
drawFrontend()
local e,p = os.pullEventRaw()
    if e == "key" then
	    local key = p
	    if key == 17 or key == 200 then
		    -- Up
		    if nOption > 1 then
			    nOption = nOption - 1
			    drawMenu()
			    drawFrontend()
		    end
	    elseif key == 31 or key == 208 then
		    -- Down
		    if nOption < 2 then -- Change 3 by the number of option.
			    nOption = nOption + 1
			    drawMenu()
			    drawFrontend()
		    end
	    elseif key == 28 then
		    -- Enter
		    break
	    end
    end
end

-- Conditions
if nOption == 1 then
clear()
print("enter your name")
	 name = read()
	 print("enter your message")
	 msg = read()
	 msg2 = " "..msg
	 rednet.send(host, name..msg2)
	 shell.run(programname)
else
clear()
	 shell.run(programname)
end

message host

rednet.open("top")
filename = "messages"
term.clear()
term.setCursorPos(1, 5)
print("	  X==---==X")
print("	  X mails X")
print("	  X==---==X")
local h = fs.open(filename, "r")
repeat
local line = h:readLine()
print(line)
until not line
h:close()
term.setCursorPos(1, 1)
print("X==--------------==X")
print("X looking for mail X")
print("X==--------------==X")
id, name, msg = rednet.receive()
file = fs.open("messages", "a")
file.writeLine(id.." "..name.." "..msg)
file.close()
os.reboot()
Luanub #2
Posted 08 September 2012 - 06:39 AM
It's an error message. Sometimes its the line number of the code that is having a problem. Does the number change or is it always the same? Have you tried doing a reboot on the computer and try again? Sometimes this will help you get the "real" error message.

I think I read the problem wrong.

Its this line in the message host.

id, name, msg = rednet.receive()


rednet.receive() picks up the send ID, msg, and distance in that order.

So the id is right, the message is being stored as name, and the distance as msg.
Edited on 08 September 2012 - 04:43 AM
ewart4fun #3
Posted 08 September 2012 - 06:44 AM
each computer i send it from sends a different message between 3 and 4

Edit: but the computer keeps sending the same message


then i supposed to see it in id first then msg and then distance

but i see it in id first and then name of sender then msg and then the random number


file.writeLine(id.." "..name.." "..msg)
Edited on 08 September 2012 - 04:55 AM
Luanub #4
Posted 08 September 2012 - 06:55 AM
rednet.send() only uses two arguemnts the to id, and the msg. They way you are sending it, it will combine name and msg into one string and send it as the msg field. You will need to seperate them after receiving the message.
ewart4fun #5
Posted 08 September 2012 - 07:03 AM
thx it worked,
you were right
to get it to work i just needed to change
file.writeLine(id.." "..name.." "..msg)
to
file.writeLine(id.." "..name)

i dont need to seperate them i think, cause the program reads it in 1 line anyway

hmm i didnt know rednet.receive see the distance the sender was
Edited on 08 September 2012 - 05:09 AM