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

Run 2 Functions At Once

Started by jamd315, 06 October 2013 - 03:53 AM
jamd315 #1
Posted 06 October 2013 - 05:53 AM
So my problem is that I am building an elevator with a display, buttons, and what floor it's on. It's pretty messy code now, but I am cleaning it up. My problem is, I need to have a display on every floor, and they communicate via rednet. But I can't wait for a button and get hung up on rednet.receive(). I looked at coroutines, but I don't think they will work.

EDIT:
Sorry, I can't pastebin, my world just corrupted. I'll have to rewrite it. Any suggestions on how to implement the above into my code?

EDIT2:
Found it in my computer file inside my saves
Elevator Main http://pastebin.com/9QaWm0bi
Elevator Trigger

rednet.open("top")
rednet.send(56, "1on")
rednet.send(61, "1on")
rednet.send(62, "1on")
rednet.send(69, "1on")
rednet.send(70, "1on")
rednet.send(71, "1on")
rednet.send(72, "1on")
rednet.send(73, "1on")
rednet.send(74, "1on")
rednet.send(75, "1on")
rednet.send(76, "1on")
Elevator Base

rednet.open("bottom")
while true do
local id, message = rednet.receive()
if message == "1on" then
  redstone.setOutput("back", true)
else
  redstone.setOutput("back", false)
end
end
theoriginalbit #2
Posted 06 October 2013 - 06:10 AM
Ok firstly coroutines will definitely work, but easier use of them is done through the parallel api. However I greatly discourage use of the parallel api, or coroutines when they're not needed, this is one of those times.

A better method would be to use an event loop, making use of os.pullEvent and the large range of events that you can receive from them. In this case you can have a loop that listens for mouse_click/monitor_touch (whichever is appropriate) and rednet_message/modem_message (whichever appropriate) and then perform the task at hand when they're received, it makes for much cleaner code, and a very logical structure.

Also you could, if you wish, and are running the correct ComputerCraft version, control a bunch of monitors from the one computer (using network cables and wired modems) making for an even cooler experience.

As for the "can't pastebin" bit of your OP, as long as the world folder isn't deleted you can always recover the files at:
Windows
%APPDATA%/minecraft/[world]/saves/computer/[computer id]
Mac OS X
~/Library/Application\ Support/minecraft/[world]/saves/computer/[computer id]
Linux
uhhh i think .home/minecraft/[world]/saves/computer/[computer id]

replacing [world] with the world name and [computer id] with the id of the computer

EDIT: you figured out where the files are xD And that kids is why you refresh before posting
Edited on 06 October 2013 - 04:11 AM
jamd315 #3
Posted 06 October 2013 - 07:31 PM
Ok firstly coroutines will definitely work, but easier use of them is done through the parallel api. However I greatly discourage use of the parallel api, or coroutines when they're not needed, this is one of those times.

A better method would be to use an event loop, making use of os.pullEvent and the large range of events that you can receive from them. In this case you can have a loop that listens for mouse_click/monitor_touch (whichever is appropriate) and rednet_message/modem_message (whichever appropriate) and then perform the task at hand when they're received, it makes for much cleaner code, and a very logical structure.

Also you could, if you wish, and are running the correct ComputerCraft version, control a bunch of monitors from the one computer (using network cables and wired modems) making for an even cooler experience.

As for the "can't pastebin" bit of your OP, as long as the world folder isn't deleted you can always recover the files at:
Windows
%APPDATA%/minecraft/[world]/saves/computer/[computer id]
Mac OS X
~/Library/Application\ Support/minecraft/[world]/saves/computer/[computer id]
Linux
uhhh i think .home/minecraft/[world]/saves/computer/[computer id]

replacing [world] with the world name and [computer id] with the id of the computer

EDIT: you figured out where the files are xD And that kids is why you refresh before posting
Thanks, will look into coroutine for future projects, thought they were a method to pause a function. I will integrate os.pullevent into this, hope to release onto programs page when done.