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

Problem when typing?

Started by digpoe, 06 May 2012 - 08:11 PM
digpoe #1
Posted 06 May 2012 - 10:11 PM
I was trying to make a program, that broadcasts what you type, and runs another program that acts as the receiver. But, when I tested this - I couldn't seem to type after it.. I'm thinking instead of using two programs, I should use pcall with a while true do loop in it, I'm not sure the pcall will run the while true do and let the script continue though.. Please help?
MysticT #2
Posted 06 May 2012 - 11:26 PM
Without the code is pretty hard to find the problems.
Are you trying to run two programs at the same time? If you are, try the parallel API.
To be able to type and receive messages at the same time, you have to handle the key, char and rednet_message events, or use the parallel API to run read() and your receiver function.
digpoe #3
Posted 07 May 2012 - 09:23 AM
Thanks. I'll post the code, but i would first ask where this "parallel API" is
Program 1:

term.clear()
term.setCursorPos(1,1)
write("Hello. Starting Chatter...")
sleep(2)
term.clear()
term.setCursorPos(1,1)
write("What side is your Wireless Modem on?")
web = read()
if string.lower(web) == "right" or "left" or "top" or "bottom" or "back" or "front" then
rednet.open (string.lower(web))
else
term.clear()
term.setCursorpos(5,5)
write("Invalid side. Chatter has to reboot.")
sleep(3)
os.reboot()
end
term.clear()
term.setCursorPos(1,1)
write("What to type? Type help for help.")
while true do
ans = read()
if ans == ("exit") then
write("Shutting down. Closing open ports..")
rednet.close(string.lower(web))
sleep(3)
term.clear()
term.setCursorPos(1,1)
write("Ports closed. Shutting down entirely....")
sleep(2)
os.shutdown()
elseif ans == ("help") then
term.clear()
term.setCursorPos(1,1)
write("Welcome to Chatter! a simple-to-use chatting program. The commands are: Exit (Shuts the computer down)")
rednet.broadcast(ans) -- I think i just found my first error...
end
write("What to type?")
shell.run("msgrcv")
end
Message Receiver:

while true do
event = os.pullEventRaw()
if event == "rednet_message" then
write(event.text) --I don't think this works either...
end
end
MysticT #4
Posted 07 May 2012 - 06:02 PM
There's some errors in the code:
Change this:

web = read()
if string.lower(web) == "right" or "left" or "top" or "bottom" or "back" or "front" then
to:

local web = string.lower(read())
if web == "right" or web == "left" or web == "top" or web == "bottom" or web == "back" or web == "front" then

You should add an else here:

write("Welcome to Chatter! a simple-to-use chatting program. The commands are: Exit (Shuts the computer down)")
else -- add this
rednet.broadcast(ans) -- I think i just found my first error...
So it broadcasts the text if it's not a command.

For the receiver:

while true do
  id, msg = rednet.receive()
  write(msg) --I don't think this works either...
end
But it will just print every received message and never exit.

To make all work at the same time, you have to choose one of the options I said before:
- Use the parallel API, it's included in CC (type help parallel to learn more about it):
To use this API, you need to create two functions: one to send and one to receive. Then you just do:

parallel.waitForAny(send, receive) -- send and receive are the functions you need to create
The send function would read the message and then send it, and the receive function would wait for a rednet message and show it on the screen.
The problem with this is that the functions may write on the same place of the screen, so you need some way to make sure that doesn't happen (like making the send function write on the last line).
- Handle the events, the user input and message send/receive yourself:
You need a loop that gets the events and handles each of them. The events you need to handle would be: key and char (for user input) and rednet_message (to get the received messages). It can be a little bit more complex since you have to store the user input, concatenate the entered keys and send the message when the user presses enter (everything that read() does), but you have more control over the program.

I would use the second option (I actually used it on my chat program), but it can be a little harder to start with.
Try some of this and come back with any question you have.