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

[Question] Am i able to create a chat program in LUA?

Started by Xhisor, 02 April 2012 - 05:32 PM
Xhisor #1
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!
Dirkus7 #2
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 :)/>/>
Advert #3
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).
Xhisor #4
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.
Advert #5
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.
Liraal #6
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').
kamnxt #7
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.