Jump to content




How do I Make 2 Functions Active at Once?


  • You cannot reply to this topic
6 replies to this topic

#1 applesauce10189

  • Members
  • 162 posts

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,

#2 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

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.

#3 HPWebcamAble

  • Members
  • 933 posts
  • LocationWeb Development

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 by HPWebcamAble, 25 March 2015 - 10:58 PM.


#4 applesauce10189

  • Members
  • 162 posts

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 by applesauce10189, 25 March 2015 - 10:59 PM.


#5 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

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.

#6 applesauce10189

  • Members
  • 162 posts

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,

#7 HPWebcamAble

  • Members
  • 933 posts
  • LocationWeb Development

Posted 26 March 2015 - 10:21 PM

View Postapplesauce10189, on 26 March 2015 - 10:38 AM, said:

Almost like it's a competition who can help the most people or something,

Bingo :D





3 user(s) are reading this topic

0 members, 3 guests, 0 anonymous users