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

How can I get this to work?

Started by ATMunn, 16 October 2013 - 04:39 PM
ATMunn #1
Posted 16 October 2013 - 06:39 PM
Title: How can I get this to work?
Hello. I am writing a program called Network, which is basicly a "secondary chat system" on the computers (hope that makes sense!) Here is a link to the code: www.pastebin.com/69fcdUuQ
Also, this program uses a API that I made called Utils. Link to the Utils API: www.pastebin.com/TDpM4qLd
Now why did I put this in Ask a Pro and not Programs? Well, theres one problem: Currently, to receive a message from another computer, you have to be running /receive, or else you will not receive any messages. What I want to do is remove the need for /receive while still allowing the player to type things in.
Hope that made sense! If it did, and you can help me, then please do :)/>
Bubba #2
Posted 16 October 2013 - 08:46 PM
You'll want to look into the parallel API. The parallel API allows you to do basic "multi-processing" by running two or more functions more or less at the same time. Check out the wiki for more info on the API, but what I think you'd what to do is something along these lines:


local function listen() --#This function will wait for messages and output them to the screen
  while true do
    local e = {os.pullEvent("modem_message")}
    --#Output the message
  end
end

local function getInput() --#This function reads the input from the user and communicates it to other servers
  while true do
    local input = read()
    --#Communicate the input to other listening chat networks
  end
end

parallel.waitForAny(getInput, listen)
ATMunn #3
Posted 17 October 2013 - 01:39 PM
I tried something simallar to that, but its simply waits about 20 seconds for so and then shuts down.
Here is the new code: http://www.pastebin.com/ikG2vLTd
Can someone help me again?
Engineer #4
Posted 17 October 2013 - 04:55 PM
You call the functions in the parallel call. You shouldnt do that.

parallel.waitForAny( functionName1, funcName2 )
Note that there are no parentheses after the function name.
ATMunn #5
Posted 17 October 2013 - 09:21 PM
I'm confused… Can you point out EXACTLY what I'm doing wrong and how to fix it? (Hopefully that makes sense)
Lyqyd #6
Posted 17 October 2013 - 09:40 PM
He did point out exactly what the problem was. Very clearly, in fact. You are calling the functions in the parallel call when you should instead be passing the functions. Look at his example parallel call and look at the one in your code and note the differences.
ATMunn #7
Posted 18 October 2013 - 03:39 PM
Umm… How do I NOT call the functions in the parallel function?
I changed it from

parallel.waitForAny(getInput(), listen(), read())
to

parallel.waitForAny( getInput, listen )
But it still does the same thing.
What's wrong?
theoriginalbit #8
Posted 18 October 2013 - 03:42 PM
Have a read of this thread on information about the parallel api.
kreezxil #9
Posted 18 October 2013 - 05:09 PM
I tried something simallar to that, but its simply waits about 20 seconds for so and then shuts down.
Here is the new code: http://www.pastebin.com/ikG2vLTd
Can someone help me again?

You should indent that code as it will be a nightmare when it gets longer. Right now it's a nightmare for me to read.
ATMunn #10
Posted 22 October 2013 - 03:37 PM
Changed my code again. Still can't figure out what's wrong.
Have a read of this thread on information about the parallel api.
That was too complicated.

New code (indented now!): www.pastebin.com/14SFDqEc

It would be nice if you showed where exactly in the code the problem is, and then posted what it should be.
CoderPuppy #11
Posted 22 October 2013 - 03:50 PM
You have two loops in the code, the first one has getInput in it. The other has the parallel call in it. Remove the first one, just the 'while true do' and the corresponding 'end'.
ATMunn #12
Posted 22 October 2013 - 05:25 PM
Remove the one that creates getInput or the one inside of getInput?
CoderPuppy #13
Posted 22 October 2013 - 06:01 PM
The one that creates getInput
ATMunn #14
Posted 22 October 2013 - 06:04 PM
Lemme try that…

It works now, but when you use a command, it will run it infinitely. New Code: www.pastebin.com/9XaFLjXN
CoderPuppy #15
Posted 22 October 2013 - 09:22 PM
What command were you trying to run? Also try adding some prints when you're debugging.
theoriginalbit #16
Posted 23 October 2013 - 12:33 AM
Changed my code again. Still can't figure out what's wrong.
Have a read of this thread on information about the parallel api.
That was too complicated.
I'm sorry to say, but if you cannot understand the info in that thread, then unfortunately you're going to have to do this in another way, the parallel api is coroutines made easy, and if you cannot understand how the parallel api works, you're going to have to avoid it. However that being said, in my opinion event loops (which is the only other way besides parallel/coroutines) is an even harder concept to grasp.
Engineer #17
Posted 23 October 2013 - 04:28 AM
However that being said, in my opinion event loops (which is the only other way besides parallel/coroutines) is an even harder concept to grasp.
*ahem* http://www.computercraft.info/forums2/index.php?/topic/14683-event-basics/ *ahem*
ATMunn #18
Posted 23 October 2013 - 12:28 PM
What command were you trying to run? Also try adding some prints when you're debugging.
Any command. Install my "utils" API and the program and type a command like "/id".
ATMunn #19
Posted 23 October 2013 - 01:31 PM
Nevermind, I fixed it! Now the program is fully fuctioning again!