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

[Lua][Question] Parallel Help?

Started by nateracecar5, 21 May 2013 - 04:41 PM
nateracecar5 #1
Posted 21 May 2013 - 06:41 PM
I have a program that uses a parallel to download a file while a loading bar is being shown. The thing is, the loading bar is taking priority over the download and the file won't download until the loading bar stops. The thing is, the download function tells the loading bar when to stop, so until the download finishes, the loading bar will keep going until the download starts, which won't because of the loading bar. Please help! Here is the code (I have the download function set to wait 5 seconds because I will have more then one file.) The loading bar ISN'T one of those annoying loading bars that are fake. It waits for the download to finish. Just an FYI. Also, this code is ALL me. No help at all.


--Setting the colors
term.setBackgroundColor(colors.white)
term.setTextColor(colors.cyan)
term.clear()
term.setCursorPos(1,1)
loading = "yes"
local installing = true
--Functions
local function centerPrint(text)
  local x, y = term.getSize()
  local x2, y2 = term.getCursorPos()
  term.setCursorPos(math.ceil((x/2) -(text:len()/2)), y2)
  write(text)
  term.setCursorPos(1,1)
end
local function load(yLoc)
  w, h = term.getSize()
  local x2, y2 = term.getCursorPos()
  loc = math.ceil(w/2)
  loc2 = yLoc
  while loading == "yes" do
	term.setCursorPos(loc-2, loc2)
	print("----")
	sleep(0.5)
	term.setCursorPos(loc-2, loc2)
	print("O---")
	term.setCursorPos(loc-2, loc2)
	sleep(0.5)
	print("-O--")
	term.setCursorPos(loc-2, loc2)
	sleep(0.5)
	print("--O-")
	term.setCursorPos(loc-2, loc2)
	sleep(0.5)
	print("---O")
	term.setCursorPos(loc-2, loc2)
	sleep(0.5)
	print("--O-")
	term.setCursorPos(loc-2, loc2)
	sleep(0.5)
	print("-O--")
	term.setCursorPos(loc-2, loc2)
	sleep(0.5)
	print("O---")
	term.setCursorPos(loc-2, loc2)
	sleep(0.5)
  end
end
function downloadFiles()
w, h = term.getSize()
term.setCursorPos(1, h-1)
shell.run("pastebin", "get", "1EzJKSLn", "rn")
sleep(5)
installing = false
loading = "no"
end
--Main Code
centerPrint("RedOS Installer")
term.setCursorPos(1, 4)
centerPrint("Installing Files....")
parallel.waitForAny(downloadFiles, load(9))
term.clear()
print("Installed!")
W00dyR #2
Posted 21 May 2013 - 07:36 PM
I am not sure of this, but I think because you use "shell.run()" to download the file, it will run that program, which overwrites anything else that happens in the code. I got a thing you could do IF what I am saying is correct (I will need a real pro to confirm that though :P/>).

- Write your own pastebin function using the http://pastebin.com/raw.php?i= API provided by pastebin itself. (You can probably find this somewhere around here)
- Copy the pastebin API and use it as a local function

Again, I am not familiar with this, so if I were you I would wait for other replies that may be more usefull, just thought I would post it anyway, maybe its usefull anyways :P/>
nateracecar5 #3
Posted 21 May 2013 - 09:23 PM
Thanks, but that doesn't work. I am starting to experiment with GitHub and it is giving me troubles too.
Lyqyd #4
Posted 21 May 2013 - 09:42 PM
Ignore Woody's advice above. Change `load(9)` to `function() load(9) end` in your parallel call. It's waiting for the load function to return before it starts the paralleling.
nateracecar5 #5
Posted 21 May 2013 - 09:52 PM
I'm not sure if I did this right. I put parallel.waitForAny(downloadFiles, function() load(9) end) and it gives me a "parallel: 4: bad argument: function expected, got boolean" error. Help?
nateracecar5 #6
Posted 21 May 2013 - 09:59 PM
I updated my code if this helps

--Setting the colors
term.setBackgroundColor(colors.white)
term.setTextColor(colors.cyan)
term.clear()
term.setCursorPos(1,1)
loading = "yes"
installing = "true"
--Functions
local function centerPrint(text)
  local x, y = term.getSize()
  local x2, y2 = term.getCursorPos()
  term.setCursorPos(math.ceil((x/2) -(text:len()/2)), y2)
  write(text)
  term.setCursorPos(1,1)
end
local function downloadFiles(url, path)
for i = 1, 3 do
  local response = http.get(url)
  if response then
   local data = response.readAll()
   if path then
    local f = io.open(path, "w")
    f:write(data)
    f:close()
    sleep(2)
    installing = "false"
   end
   centerPrint("Download success!")
   return true
  end
end
centerPrint(error("Download failed."))
return false
end
local function load(yLoc)
  w, h = term.getSize()
  local x2, y2 = term.getCursorPos()
  loc = math.ceil(w/2)
  loc2 = yLoc
  while loading == "yes" do
  term.setCursorPos(loc-3, loc2)
  print("|O---|")
  term.setCursorPos(loc-3, loc2)
  sleep(0.5)
  print("|-O--|")
  term.setCursorPos(loc-3, loc2)
  sleep(0.5)
  print("|--O-|")
  term.setCursorPos(loc-3, loc2)
  sleep(0.5)
  print("|---O|")
  term.setCursorPos(loc-3, loc2)
  sleep(0.5)
  print("|--O-|")
  term.setCursorPos(loc-3, loc2)
  sleep(0.5)
  print("|-O--|")
  term.setCursorPos(loc-3, loc2)
  sleep(0.5)
end
--Main Code
centerPrint("RedOS Installer")
term.setCursorPos(1, 4)
centerPrint("Installing Files....")
parallel.waitForAny(downloadFiles("https://raw.github.com/redstonefreak589/redos/master/RedOS", "RedOS"), function() load(9) end)
term.clear()
nateracecar5 #7
Posted 21 May 2013 - 10:21 PM
never mind I will just stick with pastebin. Thank you!
Lyqyd #8
Posted 22 May 2013 - 01:41 AM
See, you caused exactly the same problem again. You can't call either of the functions when you pass them as arguments to parallel. You were calling the downloadFiles function rather than passing it in. You changed more than I told you to, which caused it to not work. If you're told to change something, change only that thing.
Zudo #9
Posted 22 May 2013 - 03:28 AM

function downloadAllFiles()
  downloadFiles("https://raw.github.com/redstonefreak589/redos/master/RedOS","RedOS")
end

function loadNine()
  load(9)
end


parallel.waitForAny(downloadAllFiles, loadNine)

Try that

WTF MY KEYBOARD IS MESSED UP!
nateracecar5 #10
Posted 22 May 2013 - 07:36 AM
Okay, I see what you're getting at. I see now that if I want to do the downloadFiles function along with the load function I need to pass it as a function. Thanks for the constructive criticism Lyqyd!
nateracecar5 #11
Posted 22 May 2013 - 07:48 AM
Okay, so I did end up using my github code, and I was able to fix the parallel with your help, Lyqyd. Thank you for helping me and fixing my mistake and giving me just a push with the construction criticism.