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

How do I Make 2 Functions Active at Once?

Started by applesauce10189, 25 March 2015 - 09:54 PM
applesauce10189 #1
Posted 25 March 2015 - 10:54 PM
text = {"test1", "Test2", "Test3", "Test4", "Test5", "Test6", "Test7"}
rednet.open("top")

function pass()
  msg, id = rednet.receive()
  if msg == "kittens" then
    print("Hello!")
  else
    parallel.waitForAny(swag(),pass())
  end
end

function swag()
mon.clear()
mon.setCursorPos(1,1)
mon.setBackgroundColor(colors.black)
mon.setTextColor(colors.lime)
mon.setTextScale(3)
for i = 1,5 do
  mon.setCursorPos(1,i)
  mon.write(text[i])
end
sleep(3)
for i = 2,6 do
  mon.setCursorPos(1,i-1)
  mon.write(text[i])
end
sleep(3)
end
mon = peripheral.wrap("back")
parallel.waitForAny(swag(),pass())


--Password program, in a another comp

rednet.open("top")
function password(pass)
  term.clear()
  term.setCursorPos(1,1)
  term.write("Hello, this computer is locked by ")
  term.setTextColor(colors.purple)
  term.write("Apple")
  term.setTextColor(colors.white)
  term.write(".")
  term.setCursorPos(1,2)
  term.write("Please enter the password: ")
  local input = read("*")
  if input ~= pass then
    term.setCursorPos(1,3)
term.setTextColor(colors.red)
term.write("WRONG!")
sleep(3)
password("theoneandonly")
  else
    term.setCursorPos(1,3)
term.setTextColor(colors.lime)
term.write("Welcome!")
rednet.broadcast("kittens")
  end
end
password("theoneandonly")

Last I checked the parallel API was for making 2 or more things work at once, but from what I can tell it only makes 1 thing work at once and that 1 thing depends on whatever you put in first, for example, it's currently "parallel.waitForAny(swag(),pass())" and at the moment it'll run swag() and that only, but before I had pass() before it and it only ran pass(), how do I get both of these functions to work at the same time? It's for a server, and I don't want anyone terminating the program and messing with the computer, but at the same time I don't wanna use
 os.getEvent = os.getEventRaw
to stop the terminating unless I have a reliable password program, in case I want to make changes to the program later,
theoriginalbit #2
Posted 25 March 2015 - 10:57 PM
Have a read of my explanation here has to why your attempts with the parallel api doesn't work.
HPWebcamAble #3
Posted 25 March 2015 - 10:57 PM
You need to pass the function, not the result, to parallel


parallel.waitForAny(swag,pass) --# Without (), it just passes the value of swag and pass, which are functions

That should fix it


Edit: Ninja'd :ph34r:/>
Edited on 25 March 2015 - 09:58 PM
applesauce10189 #4
Posted 25 March 2015 - 10:58 PM
Oh forgot to mention, swag() is supposed to have a while loop in it…

Edit: Wow those were fast replies….
Edited on 25 March 2015 - 09:59 PM
theoriginalbit #5
Posted 25 March 2015 - 11:02 PM
What you would notice is that if the swag function was to stop executing for some reason the program would move on and the pass function would be executed, it is simply because you're invoking the functions instead of passing the pointers through to the parallel api so that it can invoke them in a way that makes it seem like they're running together.
applesauce10189 #6
Posted 26 March 2015 - 10:38 AM
Thanks for the quick reply :D/> Amazing how there's been times it took a while before I got a reply, then there's times like this where someone's already replied with a solution to my problem before I can add in "Oh I forgot this one small detail,"

It's ironic how I'm thanking you for your quick reply, more than 12 hours after the actual reply, also I find it a lil funny how in terms of minutes you and HPWebcamAble tied for first reply xD Almost like it's a competition who can help the most people or something,
HPWebcamAble #7
Posted 26 March 2015 - 10:21 PM
Almost like it's a competition who can help the most people or something,

Bingo :D/>