37 posts
Location
Sweden
Posted 02 April 2012 - 07:32 PM
Hello my fellow ComputerCrafters!
I have recently started to play around with CC and i decided to try creating a chatprogram in LUA. Here is the code.
rednet.open("bottom") --I am using wires
term.clear()
term.setCursorPos(1,1)
function chat()
t = io.read
rednet.broadcast(t)
end
chat()
Am i on the right track or will i have to erase the whole thing and restart?
I am very thankful for fast help!
146 posts
Location
the Netherlands
Posted 02 April 2012 - 07:53 PM
If you add a while loop to it, you will have a great one-way chat program. Because you use read() it waits for user inputs and not for rednet messages. In this case, use for the receiver something like this:
While true do
event, sender, msg = os.pullEvent()
write(sender)
print(' says:')
print(msg)
end
I don't exactly know the code for a two-way chatprogram but maybe you can use 2 computers at one side, so you have 4 computers in total?
Good luck :)/>/>
454 posts
Location
London
Posted 02 April 2012 - 07:57 PM
If you add a while loop to it, you will have a great one-way chat program. Because you use read() it waits for user inputs and not for rednet messages. In this case, use for the receiver something like this:
While true do
event, sender, msg = os.pullEvent()
write(sender)
print(' says:')
print(msg)
end
I don't exactly know the code for a two-way chatprogram but maybe you can use 2 computers at one side, so you have 4 computers in total?
Good luck :)/>/>
You could look at the source of read(), copy it, then use that as your loop, while printing new messages at the top. Or something like that.
You could alternatively replace os.pullEvent so that it will print new messages on the screen (hacky, but works).
37 posts
Location
Sweden
Posted 02 April 2012 - 09:09 PM
If you add a while loop to it, you will have a great one-way chat program. Because you use read() it waits for user inputs and not for rednet messages. In this case, use for the receiver something like this:
While true do
event, sender, msg = os.pullEvent()
write(sender)
print(' says:')
print(msg)
end
I don't exactly know the code for a two-way chatprogram but maybe you can use 2 computers at one side, so you have 4 computers in total?
Good luck :)/>/>
So basicly what i should do is to code it like this?
rednet.open("bottom") --I am using wires
term.clear()
term.setCursorPos(1,1)
function chat()
t = io.read
rednet.broadcast(t)
While true do
event, sender, msg = os.pullEvent()
write(sender)
print(' says:')
print(msg)
end
end
chat()
Correct me if im wrong.
454 posts
Location
London
Posted 02 April 2012 - 09:13 PM
Lua's control structures are always lower case; "While" in the example should be "while".
You should also use local variables.
You also need to call io.read; right now you are just setting t to the function io.read.
On top of that, you will not recieve messages while typing one, and you won't be able to type messages until you recieve one.
473 posts
Location
Poland
Posted 02 April 2012 - 09:16 PM
You can write and receive at one time, it's just a bit harder to do. I suggest reading on the parallel API ('help parallel').
80 posts
Posted 03 April 2012 - 08:36 PM
You can look at my RedChat program. It both sends and receives at the same time without using the parallel API.