Jump to content




Parallel Api Woes


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

#1 campicus

  • Members
  • 164 posts

Posted 16 October 2013 - 12:59 AM

I am trying to get my program to run a redstone pulsing loop with the ability to quit at any time. I cannot for the life of me get this to work, help please!

I don't get an error or anything, the program just hangs.

local quit = false

function pulse()
  --"pulsing program here"
end

function reset()
  local event, key = os.pullEvent("char")
  if key == "q" then
    quit = true
  end
end

while true do
  parallel.waitForAny(pulse(), reset())
  if quit then
    fs.delete("pulserLog")
    os.reboot()
  end
end


#2 immibis

    Lua God

  • Members
  • 1,033 posts
  • LocationWellington, New Zealand

Posted 16 October 2013 - 01:19 AM

This:
  parallel.waitForAny(pulse(), reset())
should be this:
  parallel.waitForAny(pulse, reset)


#3 campicus

  • Members
  • 164 posts

Posted 16 October 2013 - 07:58 AM

You sir, are, as always, amazing!! Bask in the warmth of my commas!

#4 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 16 October 2013 - 10:21 AM

ok so as an expansion on what immibis has said, and an explanation as to why your code didn't work.

The parallel API requires function pointers so that it can turn them into coroutines and run them.

What you're doing is you're calling the functions before passing them to the parallel api, so pulse is being called and your program immediately gets caught in pulse's infinite loop, if pulse was to actually exit, the program would then continue onto reset, which would run in it's loop, if reset was to exit, the program would continue and then call the parallel function with whatever values both of the previous functions (pulse and reset) returned.

What is a function pointer?
Spoiler

Edited by theoriginalbit, 12 February 2014 - 05:54 AM.


#5 TheOddByte

    Lazy Coder

  • Members
  • 1,607 posts
  • LocationSweden

Posted 16 October 2013 - 11:59 AM

View Posttheoriginalbit, on 16 October 2013 - 10:21 AM, said:

ok so as an expansion on what immibis has said, and an explanation as to why your code didn't work.

The parallel API requires function pointers so that it can turn them into coroutines and run them.

What you're doing is you're calling the functions before passing them to the parallel api, so pulse is being called and your program immediately gets caught in pulse's infinite loop, if pulse was to actually exit the program would then continue onto reset, which would run in it's loop, if reset was to exit the program would continue and then call the parallel function with whatever values both of the previous functions returned.

What is a function pointer?
Spoiler
Even though immibis and you have already helped him I'm just wondering how you can turn something simple into something advanced and amazing? :P

@campicus
You can also do this to exit an loop so you know
local quit = false

while not quit do
	-- Code
end
It may not be useful for your code, But sometimes it can be proven useful

Also, If you are using 'os.reboot' to just get out of the program I would suggest you used 'error' since 'os.reboot' can be bad if the user has an os installed or something

Example function
local function exitProgram(text)
	term.clear()
	term.setCursorPos(1,1)
	print(text.."\n)
    error()
end
This would ofcourse exit the program then clear the screen and print some text when it's been exited

#6 campicus

  • Members
  • 164 posts

Posted 16 October 2013 - 06:23 PM

Thanks for all the help guys! I love this community, it is a rare thing to see so many people so will to put their time and effort into helping others, for nothing in return :)





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users