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.
This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
Simultaneous coroutines?
Started by mmine1, 17 December 2012 - 03:28 PMPosted 17 December 2012 - 04:28 PM
Ok, so here's the deal:
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.
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.
Posted 17 December 2012 - 04:42 PM
So the option I would suggest here is to use os.pullEvent() or os.pullEventRaw()
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?
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?
Posted 17 December 2012 - 05:51 PM
So the option I would suggest here is to use os.pullEvent() or os.pullEventRaw()
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.
Posted 17 December 2012 - 07:58 PM
While you could write coroutines, the parallel API is a lot easier to use.
What do you think the parallel API does with the functions you pass it?
Posted 17 December 2012 - 08:00 PM
While you could write coroutines, the parallel API is a lot easier to use.
What do you think the parallel API does with the functions you pass it?
I can understand why he recommended, it does make coroutines a little easier. however even parallel isn't simultaneous coroutine like the OP wanted. Coroutines only run one at a time.
Posted 17 December 2012 - 08:03 PM
While you could write coroutines, the parallel API is a lot easier to use.
What do you think the parallel API does with the functions you pass it?
I can understand why he recommended, it does make coroutines a little easier. however even parallel isn't simultaneous coroutine like the OP wanted. Coroutines only run one at a time.
Yes, I am aware. The point was that he was suggesting "using the parallel API" instead of writing coroutines, which is nonsensical.
Posted 17 December 2012 - 08:33 PM
While you could write coroutines, the parallel API is a lot easier to use.
What do you think the parallel API does with the functions you pass it?
I can understand why he recommended, it does make coroutines a little easier. however even parallel isn't simultaneous coroutine like the OP wanted. Coroutines only run one at a time.
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.
Posted 17 December 2012 - 11:08 PM
I think that the intended meaning is that using the parallel API wouldn't be the same as writing a coroutine, it would be using a pre-written coroutine.
Unless you rewrote the parallel API.
Unless you rewrote the parallel API.
Posted 18 December 2012 - 02:26 AM
I'm pretty positive that using parallel instead of manually making your own coroutines can reduce your program by a couple lines, that and it usually makes your script's logic look more sound.Yes, I am aware. The point was that he was suggesting "using the parallel API" instead of writing coroutines, which is nonsensical.
Posted 18 December 2012 - 04:28 AM
Yes, using the parallel API to manage your coroutines can save you a few lines, if it's what you need. You do still have to write coroutines for it to run, though. :)/>
Posted 18 December 2012 - 04:58 AM
While you could write coroutines, the parallel API is a lot easier to use.
What do you think the parallel API does with the functions you pass it?
Yes, I know how the parallel API works, I was simply stating that it was more user-friendly.
Posted 18 December 2012 - 05:03 AM
i wouldnt use coroutines for something like this, its too simple
just loop os.pullEvent and look for "rednet_message" and "key"
this allows you to make a custom UI without confusing coroutines
just loop os.pullEvent and look for "rednet_message" and "key"
this allows you to make a custom UI without confusing coroutines
Posted 18 December 2012 - 05:05 AM
i wouldnt use coroutines for something like this, its too simple
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().
Posted 18 December 2012 - 10:44 AM
i wouldnt use coroutines for something like this, its too simple
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.
Posted 18 December 2012 - 11:35 AM
So the option I would suggest here is to use os.pullEvent() or os.pullEventRaw()
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.
Posted 18 December 2012 - 11:37 AM
So the option I would suggest here is to use os.pullEvent() or os.pullEventRaw()
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.