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

I give up, running shell and os.pullEvent :(

Started by deeredman1991, 19 December 2014 - 07:22 PM
deeredman1991 #1
Posted 19 December 2014 - 08:22 PM
I've been wrecking my brain all morning :(/> essentially I want to add an "autorun" feature to disk with the directory "autorun" in them. but I can't get os.pullEvent to work without yielding. :(/> I've been all over the forums and google but to no avail. So I have finally decided to make an account and ask you guys for assistance. :)/> Here is my code so far. :)/>



function Autorun()

if os.pullEvent("disk") then
  print("WORKING!")
  return
end
end

parallel.waitForAny(function() shell.run("rom/programs/shell") end, Autorun())
hbomb79 #2
Posted 19 December 2014 - 10:44 PM
This function is going to run shell and your function in parallel
so your problem is the shell doesnt work correctly?

Try this:


function shellRun()
  shell.run("shell")
end

function diskWait()
while true do
  local e, p1, p2, p3 = os.pullEvent()
  if e == "disk" then
   print("Disk Inserted")
   --# Autorun the files here
  end
end
end

parallel.waitForAny(shellRun, diskWait)

I have tested this and it works

NOTE: Name this file startup to run it when the PC starts
Edited on 19 December 2014 - 09:49 PM
Bomb Bloke #3
Posted 19 December 2014 - 10:47 PM
but I can't get os.pullEvent to work without yielding. :(/>

Long story short, you can't.

You could get around this to some extent by putting your disk-listening function into a separate co-routine to the main shell (like with hbomb's example), but that brings problems of its own - you'd need to pause the main shell's co-routine whenever you decide to execute your "auto-run" code or you'll have two scripts fighting over the one terminal. The parallel API on its own can't handle that.

The "easy" way to go about this, at least if you're on CC 1.6 or later, is multishell and the associated API. The idea is that you run your disk-listening code in its own multishell tab, and have that tab give itself focus whenever it goes to "autorun" something (then give it back to the previous tab when it's done).
hbomb79 #4
Posted 19 December 2014 - 10:49 PM
What I posted works, I have tested it and it allows use of shell and detects disk inputs, I think thats what he wanted.. am I wrong xD

It would be running the autorun function when the disk is inserted, Im not sure if he needs the shell to continue or pause when his autorun executes, It would depend on the situation, but I can see how it might cause issues, I havent used the multishell api too much so, I cant really give an example with such methods
Edited on 19 December 2014 - 09:52 PM
Dragon53535 #5
Posted 20 December 2014 - 06:47 AM
I think his problem, is that it runs the autorun function as he tells it to instead of passing the function to the parallel call…



parallel.waitForAny(function() shell.run("rom/programs/shell") end, Autorun())
--#Should be
parallel.waitForAny(function() shell.run("rom/programs/shell") end, Autorun)
Bomb Bloke #6
Posted 21 December 2014 - 11:11 AM
Oh hey. I never got around to reading that last line of code. Nice catch.

What I posted works, I have tested it and it allows use of shell and detects disk inputs, I think thats what he wanted.. am I wrong xD

It'll certainly do what you think it will. But think about what happens when you flesh it out to run the "autorun" script - let's say it calls read() in order to ask you to type something. When you type, are you typing into the shell, or into the autorun script? The answer is both at once, which is hardly ideal. Likewise, if one of the two scripts decides to move the cursor around the screen (by using term.setCursorPos(), print(), a paintutils command…), it'll be moved for both scripts as well, meaning they'll each quickly lose track as to where it is!

This can be sorted out by writing a custom co-routine manager that can selectively pause one of the threads of execution (so that when the autorun script starts, the shell halts). The multishell API offers another solution - the multiple scripts keep running, but each gets its own terminal window (they can't control the other's cursor), and only the script in the foreground is allowed to "see" key/char/user-input-related events.
hbomb79 #7
Posted 21 December 2014 - 07:55 PM
Ahh I see. Didn't realise that. Thanks for pointing that out