21 posts
Location
C:\Windows\System32\
Posted 08 October 2017 - 09:41 AM
So I was trying to make something I can receive incoming RedNet messaged and broadcast them at the same time. Creating 2 functions and running them in parallel works fine. Now the problem is, that I tried to create a 3rd function, which waits for the user to press ctrl, and it will launch another function which will prompt a "menu" asking for new protocol to listen to. I have tested stuff but it wont seem to work for some reason. Here's the code:
*Code removed by The_Penguin21*
Also, another problem I get is that if I broadcast a RedNet message from another computer without a protocol, it will display like:
<id> <message> <
instead of
<id> <message <>
while if it detects a protocol it should be:
<id> <message> <protocol>
The reason it outputs the protocol is, that if I inter an empty protocol (I should add a line to the beginning of changeProtocol() which adds nil to it), it should listen to any protocol and output that protocol.
At the beginning, it wont seem to change the protocol. When it calls changeProtocolFull() I think it gets messed up since broadcast() is also called, as it runs in parallel.
Edited on 04 November 2017 - 08:28 AM
1220 posts
Location
Earth orbit
Posted 08 October 2017 - 04:09 PM
You're defining 'prot' as local to the function 'changeProtocol' - therefore it will only exist within that function. Try defining prot outside of, and before, the changeProtocol function (e.g. declare protocol at the beginning of your program - in your case, just after rednet.open("back")).
Edited on 08 October 2017 - 02:10 PM
3057 posts
Location
United States of America
Posted 08 October 2017 - 04:26 PM
The display issue is because print will stop on it's first nil argument. If you don't pass it nil, then it will keep going.
The cleanest way I can think of to do this is to use "or"
print( ..., protocol or "", ... )
21 posts
Location
C:\Windows\System32\
Posted 08 October 2017 - 06:08 PM
If I use "or" within the print, won't it then either display "<protocol" or "<>"? How do i get it to put a ">" after the protocol?
3057 posts
Location
United States of America
Posted 08 October 2017 - 06:12 PM
Not if you do it properly.
What would happen is it would display "protocol" or "" between "<" and ">". "" is non-nil, so print will continue, but it won't display anything either.
21 posts
Location
C:\Windows\System32\
Posted 08 October 2017 - 06:14 PM
Ok thanks, I will give it a try.
21 posts
Location
C:\Windows\System32\
Posted 08 October 2017 - 06:31 PM
Okay, so it is almost working, now the only problem that occurs is that when i press ctrl. After i press ctrl, it asks for a new protocol; when I start to write the new protocol's name, it writes it after the text "Protocol: " but also at the beginning of it. After i press enter, it changes the "prot" to the new protocol I entered, but also broadcasts the new protocol's name over rednet using the old protocol. I think it is because of the broadcast() function is running in parallel with the changeProtocolFull(). Any ways to fix this?
1220 posts
Location
Earth orbit
Posted 08 October 2017 - 07:41 PM
I'm not sure this is the best way to achieve what you want, but it should fix your re-broadcast issue. Basically remove changeProtocolFull and change your broadcast function to this…
local function broadcast()
while true do
local userInput = read()
if userInput == "prot" then
changeProtocol()
elseif userInput ~= "" then
rednet.broadcast(userInput, prot)
end
end
end
Every time you type
prot you would be prompted for the new protocol. The shortcoming of this solution is that you can't broadcast the word prot.