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

[Lua][Qeustion]Help with my chat program

Started by Spongy141, 05 December 2012 - 03:26 AM
Spongy141 #1
Posted 05 December 2012 - 04:26 AM
Hello, I already know that my chat program works, and has no errors, but I do not like were I have to wait to send a message for someone to reply first, someone on a CC server I play said you needed to do something like.

os.pullEvent
and I admit, I am not the good with some Lua in CC, like os, but here's my chat programs code. All i really want to know is were to put it, and what to put, because Im not good with os. thanks.

function top()
  term.clear()
  term.setCursorPos(1,1)
  term.setTextColor(colors.purple)
  print("Welcome to Chatv")
  term.setTextColor(colors.gray)
  print("====================================================")
end
function detectDevice(DeviceName)
DeviceSide="none"
for k,v in pairs(redstone.getSides()) do
if peripheral.getType(v)==DeviceName then
   DeviceSide = v
   break
end
end
  return(DeviceSide)
end
top()
ModemSide=detectDevice("modem")
rednet.open(ModemSide)
term.setTextColor(colors.green)
write("Username: ")
term.setTextColor(colors.white)
local user = read()
term.clear()
term.setCursorPos(1,1)
top()
while true do
  term.setTextColor(colors.lime)
  write("Message: ")
  term.setTextColor(colors.white)
  local a = read()
  rednet.broadcast(user.. ": " ..a)
  id, msg = rednet.receive()
  print(id.. ": is sending a message")
  print(msg)
end
Doyle3694 #2
Posted 05 December 2012 - 04:30 AM
You have 2 options really, either, you could to some extent recode read(), so that it will also accept a rednet_message event and print it out. OR, you could trick around some with parallel API. both working, I personally would have taken the first, more "legit" route. Plus I have no experience with parallel. Though, that being said, I think parallel is abit easier to use, though I also think the first route is safer, since then its your program from the bottom sort of say.
ChaddJackson12 #3
Posted 05 December 2012 - 04:38 AM
I'm not too good with this stuff, but I think I might be able to help. Instead of someone having to reply, just have a "Reload" option, where it would reload the program and load the new messages. Or you could have a while loop reloading the program every so often so that it could load the new messages.
Doyle3694 #4
Posted 05 December 2012 - 04:50 AM
chadd, he wants to read and reveive at the same time. and no timeframe or something, instant.
Spongy141 #5
Posted 05 December 2012 - 12:59 PM
You have 2 options really, either, you could to some extent recode read(), so that it will also accept a rednet_message event and print it out. OR, you could trick around some with parallel API. both working, I personally would have taken the first, more "legit" route. Plus I have no experience with parallel. Though, that being said, I think parallel is abit easier to use, though I also think the first route is safer, since then its your program from the bottom sort of say.
So I would just do

local a = read() id, msg = rednet.receive()
then just leave the rest as is?