#1
Posted 17 December 2012 - 04:28 PM
I'm fairly new to ComputerCraft and Lua, however I know the basics thanks to a friend very fluent in it. I've been working on a basic IRC client for a couple days now, and my problem is this: how can I make a simultaneous coroutine? I've had to split sending an receiving messages over to computers (one with a large monitor, one only the terminal) because I haven't been able to let rednet rednet.open() and rednet.broadcast() at the same time without having only two clients take turns making one message then waiting for the next sender to make another message before sending another one. If anyone can help me out, that would be great.
#2
Posted 17 December 2012 - 04:42 PM
Every time an event triggers in the os it goes onto a stack when the pullEvent is called it pulls an event off that stack. so you can use this and check for "rednet_message".
NOTE: open rednet at the top of the program, close at the bottom or anywhere you deliberately leave the program.
Is this what you were after?
#3
Posted 17 December 2012 - 05:51 PM
TheOriginalBIT, on 17 December 2012 - 04:42 PM, said:
Every time an event triggers in the os it goes onto a stack when the pullEvent is called it pulls an event off that stack. so you can use this and check for "rednet_message".
NOTE: open rednet at the top of the program, close at the bottom or anywhere you deliberately leave the program.
Is this what you were after?
That is completely unrelated to the question.
OP: Do something like this:
function receive() id, msg, dist = rednet.receive() end function broadcast() write("> ") input = read() rednet.broadcast(input) end parallel.waitForAny(receive, broadcast)
While you could write coroutines, the parallel API is a lot easier to use.
#5
#6
Posted 17 December 2012 - 08:03 PM
TheOriginalBIT, on 17 December 2012 - 08:00 PM, said:
Yes, I am aware. The point was that he was suggesting "using the parallel API" instead of writing coroutines, which is nonsensical.
#7
Posted 17 December 2012 - 08:33 PM
Lyqyd, on 17 December 2012 - 08:03 PM, said:
TheOriginalBIT, on 17 December 2012 - 08:00 PM, said:
Yes, I am aware. The point was that he was suggesting "using the parallel API" instead of writing coroutines, which is nonsensical.
I am aware.
#8
Posted 17 December 2012 - 11:08 PM
Unless you rewrote the parallel API.
#9
Posted 18 December 2012 - 02:26 AM
Lyqyd, on 17 December 2012 - 08:03 PM, said:
#10
Posted 18 December 2012 - 04:28 AM
#11
#12
Posted 18 December 2012 - 05:03 AM
just loop os.pullEvent and look for "rednet_message" and "key"
this allows you to make a custom UI without confusing coroutines
#13
Posted 18 December 2012 - 05:05 AM
PixelToast, on 18 December 2012 - 05:03 AM, said:
just loop os.pullEvent and look for "rednet_message" and "key"
this allows you to make a custom UI without confusing coroutines
But then you have to concatenate an empty string and the keypress, and incorporate backspace to remove the last character, and incorporate enter to finish it off. Its not worth it to me, when i could use read() and parallel.waitForAny().
#14
Posted 18 December 2012 - 10:44 AM
Dlcruz129, on 18 December 2012 - 05:05 AM, said:
PixelToast, on 18 December 2012 - 05:03 AM, said:
just loop os.pullEvent and look for "rednet_message" and "key"
this allows you to make a custom UI without confusing coroutines
But then you have to concatenate an empty string and the keypress, and incorporate backspace to remove the last character, and incorporate enter to finish it off. Its not worth it to me, when i could use read() and parallel.waitForAny().
read() can be terminated with ctrl + t
os.pullEventRaw cant be.
now as for messing with strings not hard,
outside the main loop
text = ""
event readers
event, param = os.pullEventRaw() if event == "char" then text = text..param eslseif event == "key" then if param == keys.backspace then text = text:sub(1, text:len() - 1) elseif param == keys.enter then processInput( text ) text = "" end end term.clear() print( text )
Done. Not hard. Typed in a few seconds.
#15
Posted 18 December 2012 - 11:35 AM
Dlcruz129, on 17 December 2012 - 05:51 PM, said:
TheOriginalBIT, on 17 December 2012 - 04:42 PM, said:
Every time an event triggers in the os it goes onto a stack when the pullEvent is called it pulls an event off that stack. so you can use this and check for "rednet_message".
NOTE: open rednet at the top of the program, close at the bottom or anywhere you deliberately leave the program.
Is this what you were after?
That is completely unrelated to the question.
OP: Do something like this:
function receive() id, msg, dist = rednet.receive() end function broadcast() write("> ") input = read() rednet.broadcast(input) end parallel.waitForAny(receive, broadcast)
While you could write coroutines, the parallel API is a lot easier to use.
so I've tried to input that with it, and I still can only send messages but not receive them, however a computer with a program to scan the network still picks it up.
#16
Posted 18 December 2012 - 11:37 AM
mmine1, on 18 December 2012 - 11:35 AM, said:
Dlcruz129, on 17 December 2012 - 05:51 PM, said:
TheOriginalBIT, on 17 December 2012 - 04:42 PM, said:
Every time an event triggers in the os it goes onto a stack when the pullEvent is called it pulls an event off that stack. so you can use this and check for "rednet_message".
NOTE: open rednet at the top of the program, close at the bottom or anywhere you deliberately leave the program.
Is this what you were after?
That is completely unrelated to the question.
OP: Do something like this:
function receive() id, msg, dist = rednet.receive() end function broadcast() write("> ") input = read() rednet.broadcast(input) end parallel.waitForAny(receive, broadcast)
While you could write coroutines, the parallel API is a lot easier to use.
so I've tried to input that with it, and I still can only send messages but not receive them, however a computer with a program to scan the network still picks it up.
I say try using pullEvents I've got a system working perfect with that.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users