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

[Lua] [Error] In game ComputerCraft Messenger

Started by Puharesource, 23 July 2012 - 05:11 PM
Puharesource #1
Posted 23 July 2012 - 07:11 PM
So I have been trying to make an in game ComputerCraft messenger, I got it working quite easily but somehow, I don't even know what I did it just stopped working. I have now used several hours to try to fix it, considering I really don't have all that much lua experience.

I know what my problem is; Whenever the io.read() is in place the listener wont "listen" for other messages which is really annoying me and since, as I said I don't really have any lua experience then I don't know how to fix this problem. Also if someone ends of helping me, please tell me what you've done so that I can learn :)/>/>

PS: I already have a Login program that connects you to rednet that works just fine :o/>/>


--Strings
  displayName = ""
--Commands
  cShutdown = "/shutdown"
  cReboot = "/reboot"
  cRestart = "/restart"
  cRelog = "/relog"

--Functions
  function listener()
	event, id, text = os.pullEvent()
	if event == "rednet_message" then
	print(text)
   end
  end

  function uMessage()
  input = io.read()
  rednet.broadcast("" .. displayName .. "> " .. input .. "")
end

  function loginMessage()
  rednet.broadcast("+ " .. displayName .. ", has logged on!")
end

--"displayName Chooser"
  print"Please, choose a displayname."
  input = io.read()
  displayName = input

  loginMessage()

   --"Reader"/Commands
while true do

listener()


   if input == cReboot then
	shell.run("reboot")

   elseif input == cShutdown then
	shell.run("shutdown")

   elseif input == cRestart then
	shell.run("clear")
	shell.run("cd disk/RedNet")
	shell.run("clear")
	shell.run("Login")

   elseif input == cRelog then
	shell.run("clear")
	shell.run("cd disk/RedNet")
	shell.run("clear")
	shell.run("Success")
   else
   uMessage()
  end
end
Noodle #2
Posted 24 July 2012 - 12:17 AM
This is where the parallel api comes in handy.
Add:
while true do
   parallel.waitForAny(uMessage, listener)
end
Puharesource #3
Posted 24 July 2012 - 01:23 AM
This is where the parallel api comes in handy.
Add:
while true do
   parallel.waitForAny(uMessage, listener)
end

Thanks for the help :)/>/>, but there's a problem, now whenever I write a message, the message is blank to anyone else using the messenger and when a message appears while I'm writing it, it cancels the io.read() and goes to the next line. What do I do? ;S
Grim Reaper #4
Posted 24 July 2012 - 01:48 AM
I wrote a solution using an entirely separate function. You'll have to mess with the way that the messages are printed, but I hope it is what you are looking for:

Spoiler

--Strings
  displayName = ""
--Commands
  cShutdown = "/shutdown"
  cReboot = "/reboot"
  cRestart = "/restart"
  cRelog = "/relog"
--Functions
  function listener()
	    event, id, text = os.pullEvent()
	    if event == "rednet_message" then
	    print(text)
   end
  end
  function uMessage()
  input = io.read()
  rednet.broadcast("" .. displayName .. "> " .. input .. "")
end
  function loginMessage()
  rednet.broadcast("+ " .. displayName .. ", has logged on!")
end
function listenAndRead( nLength, cReplaceChar )
term.setCursorBlink( true )

nLength = nLength or -1 -- -1 is unlimited
sReturnString = ""

xPos, yPos = term.getCursorPos()

while true do
  event, char, message = os.pullEvent()
 
  if nLength ~= -1 and string.len( sReturnString ) >= nLength then term.setCursorBlink( false ); return sReturnString end -- Length check
 
  if event == "rednet_message" and message then -- Custom read function implementing the listener
   term.setCursorPos( 1, yPos+1 )
   print( message )
   term.setCursorPos( xPos + string.len( sReturnString ), yPos )
  elseif event == "char" then sReturnString = sReturnString .. char
  elseif event == "key" and char == 28 then term.setCursorBlink( false ); return sReturnString -- Enter
  elseif event == "key" and char == 14 then -- Backspace
   term.setCursorPos( xPos, yPos )
   term.write( string.rep( " ", string.len( sReturnString ) ) )
   sReturnString = string.sub( sReturnString, 1, string.len( sReturnString )-1 )
   term.setCursorPos( xPos, yPos )
  
   if not cReplaceChar then term.write( sReturnString )
   else term.write( string.rep( cReplaceChar, string.len( sReturnString ) ) ) end
  end
 
  term.setCursorPos( xPos, yPos )
  term.write( string.rep( " ", string.len( sReturnString ) ) )
  term.setCursorPos( xPos, yPos )
  if not cReplaceChar then term.write( sReturnString )
  else term.write( string.rep( cReplaceChar, string.len( sReturnString ) ) ) end
end
end
--"displayName Chooser"
  print"Please, choose a displayname."
  input = io.read()
  displayName = input
  loginMessage()
   --"Reader"/Commands
while true do
listenAndRead( 24, nil )

   if input == cReboot then
	    shell.run("reboot")
   elseif input == cShutdown then
	    shell.run("shutdown")
   elseif input == cRestart then
	    shell.run("clear")
	    shell.run("cd disk/RedNet")
	    shell.run("clear")
	    shell.run("Login")
   elseif input == cRelog then
	    shell.run("clear")
	    shell.run("cd disk/RedNet")
	    shell.run("clear")
	    shell.run("Success")
   else
   uMessage()
  end
end

Hope I helped! :)/>/>
Noodle #5
Posted 24 July 2012 - 03:18 AM
^^ More complex.. Rewrite the line.
term.setCursorPos(x, y) -- X + Y, whereever the text appears
term.clearLine()
Another thing, You are waiting for a message, just put both the functions in a while true loop.
Puharesource #6
Posted 24 July 2012 - 06:57 PM
I wrote a solution using an entirely separate function. You'll have to mess with the way that the messages are printed, but I hope it is what you are looking for:
Spoiler
 --Strings displayName = "" --Commands cShutdown = "/shutdown" cReboot = "/reboot" cRestart = "/restart" cRelog = "/relog" --Functions function listener() event, id, text = os.pullEvent() if event == "rednet_message" then print(text) end end function uMessage() input = io.read() rednet.broadcast("" .. displayName .. "> " .. input .. "") end function loginMessage() rednet.broadcast("+ " .. displayName .. ", has logged on!") end function listenAndRead( nLength, cReplaceChar ) term.setCursorBlink( true ) nLength = nLength or -1 -- -1 is unlimited sReturnString = "" xPos, yPos = term.getCursorPos() while true do event, char, message = os.pullEvent() if nLength ~= -1 and string.len( sReturnString ) >= nLength then term.setCursorBlink( false ); return sReturnString end -- Length check if event == "rednet_message" and message then -- Custom read function implementing the listener term.setCursorPos( 1, yPos+1 ) print( message ) term.setCursorPos( xPos + string.len( sReturnString ), yPos ) elseif event == "char" then sReturnString = sReturnString .. char elseif event == "key" and char == 28 then term.setCursorBlink( false ); return sReturnString -- Enter elseif event == "key" and char == 14 then -- Backspace term.setCursorPos( xPos, yPos ) term.write( string.rep( " ", string.len( sReturnString ) ) ) sReturnString = string.sub( sReturnString, 1, string.len( sReturnString )-1 ) term.setCursorPos( xPos, yPos ) if not cReplaceChar then term.write( sReturnString ) else term.write( string.rep( cReplaceChar, string.len( sReturnString ) ) ) end end term.setCursorPos( xPos, yPos ) term.write( string.rep( " ", string.len( sReturnString ) ) ) term.setCursorPos( xPos, yPos ) if not cReplaceChar then term.write( sReturnString ) else term.write( string.rep( cReplaceChar, string.len( sReturnString ) ) ) end end end --"displayName Chooser" print"Please, choose a displayname." input = io.read() displayName = input loginMessage() --"Reader"/Commands while true do listenAndRead( 24, nil ) if input == cReboot then shell.run("reboot") elseif input == cShutdown then shell.run("shutdown") elseif input == cRestart then shell.run("clear") shell.run("cd disk/RedNet") shell.run("clear") shell.run("Login") elseif input == cRelog then shell.run("clear") shell.run("cd disk/RedNet") shell.run("clear") shell.run("Success") else uMessage() end end 
Hope I helped! :)/>/>

Thank you so much, but as I wrote in my post, I have hardly any experience with Lua, so could you please explain the code a little, because some of it isn't working too well and I'd like to try and fix it ;3