Jump to content




Multiple Functions Active at once


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

#1 joviboy

  • New Members
  • 2 posts

Posted 06 October 2013 - 10:04 AM

Multiple Functions Active at once

Is it possible to keep 2 functions activated at once? Like an ingame clock while you're on a menu? Or a fuel meter while still waiting for a keypress in a menu function?

#2 svdragster

  • Members
  • 222 posts
  • LocationGermany

Posted 06 October 2013 - 12:02 PM

You want to use the parallel Api.

#3 MKlegoman357

  • Members
  • 1,170 posts
  • LocationKaunas, Lithuania

Posted 06 October 2013 - 02:23 PM

Parallel API allows you to run more than one function at once. You can use it like this:

local function log () --//This will log every event into a file called "events.log"
  while true do
    local event = {os.pullEvent()}
    local file = fs.open("events.log", "a")

    file.writeLine(table.concat(event, ","))
    file.close()
  end
end

local function input () --//This is just an example function that will ask your name
  print("What is your name?")

  local name = read()

  print("Hello, " .. name .. ". Your identity has been stolen by nobody. Have a nice day!")
end

--//To run those functions at the same time we can use parallel API like this:

parallel.waitForAny(log, input)

--//As you can see, we just pass our functions into parallel.waitForAny and now they will be running together,
--//it will ask your name while logging everything you type into a file called "events.log".

--//There is one problem with parallel.waitForAny: when one function stops, every other functions stops too.
--//But what if you want to leave other functions running when one is stopped?
--//In my example, when this program will get your name it will no longer log the events.
--//To keep "log" function still running we can use this:

parallel.waitForAll(log, input)

Edited by MKlegoman357, 07 October 2013 - 08:09 AM.


#4 LBPHacker

  • Members
  • 766 posts
  • LocationBudapest, Hungary

Posted 06 October 2013 - 11:27 PM

View PostMKlegoman357, on 06 October 2013 - 02:23 PM, said:

fs.open("events.log", "r")
Very nice answer except for that file which is opened in read mode - append mode would be better.

#5 joviboy

  • New Members
  • 2 posts

Posted 07 October 2013 - 12:40 AM

Thank you very much!

EDIT: but how do I fit it into my program? Can't seem to find the right place :(
local menuOptions  = {"Brench Miner", "Quarry Miner", "Console", "Restart", "Shutdown"}
local termX, termY = term.getSize() --Get the terminal size
local selected = 1 --The selected menu item

function inserText(text, termY)
--term.setCursorPos(termX/2-#text+1/2,termY)
term.setCursorPos(16,termY+4)
term.write(text)
return true
end

function centerText(text, termY)
term.setCursorPos(termX/2-#text/2,termY)
term.write(text)
return true
end

function ingameclock()
while true do
  term.clear()
  local intime = textutils.formatTime(os.time(), true)
  local inday = os.day()
  local x = 0;

  term.clear()
  term.setBackgroundColor(colors.blue)
  term.setCursorPos(1,2)
  write("																			 ")
  term.setBackgroundColor(colors.black)
  term.setCursorPos(2,2)
  if os.time() > 19 or os.time() < 6 then
   term.setTextColor(colors.red)
  else
   term.setTextColor(colors.white)
  end
  write("[")
  term.setTextColor(colors.white)
  term.setCursorPos(3,2)
  write("0")
  term.setCursorPos(8-#intime,2)
  write(intime)
  if os.time() > 19 or os.time() < 6 then
   term.setTextColor(colors.red)
  else
   term.setTextColor(colors.white)
  end
  write("]")

  term.setTextColor(colors.white)
  term.setCursorPos(51-12+2,2)
  write("[DAY: ")
  write("000")
  term.setCursorPos(51-12+11-#tostring(inday),2)
  write(inday)
  term.setTextColor(colors.white)
  write("]")
  sleep(0,8)
end
end

function start()
while true do
  term.setTextColor(colors.lime)
  term.setBackgroundColor(colors.black)
  term.setCursorPos(1,1)
  centerText("Main Menu", 1)
  term.setCursorPos(1,18)
  term.setTextColor(colors.yellow)
  print("---------------------------------------------------")
  centerText("UP / DOWN - ENTER", 19)
  term.setBackgroundColor(colors.black)
  term.setTextColor(colors.lightGray)
  for i,v in ipairs(menuOptions) do
   if i == selected then
		term.setBackgroundColor(colors.orange)
  term.setCursorPos(16,i+4)
  write("				")
  term.setTextColor(colors.white)
  inserText(i.."> "..v, i)
  term.setBackgroundColor(colors.black)
  term.setTextColor(colors.lightGray)
   else
		inserText(i.."> "..v,i)
   end
  end
  x,y = os.pullEvent() --Get user input
  if y == keys.down and selected < #menuOptions then
   selected = selected + 1
  elseif y == keys.up and selected > 1 then
   selected = selected - 1
  elseif y == keys.enter then
   return selected
  end
end
end

x = start()
print()
print("You selected option: ".. menuOptions[x])
term.setTextColor(colors.green)
print("restarting program lol")
sleep(1)
term.clear()
shell.run("startmenu")


#6 jay5476

  • Members
  • 289 posts

Posted 07 October 2013 - 02:17 AM

well basically this is an explaination of the parallel api
there are to functions:
parallel.waitForAny and parallel.waitForAll you can use both of them like
parallel.waitForAny(func1,func2,func3) --etc.
parallel.waitForAll(func1,func2,func3) --etc.
parallel.waitForAny can be used to simulate simultaneous running of functions
by running 1 function until it yields then doing the same for the next function

parallel.waitForAll waits for 1 function to finish before running the next





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users