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

Doing two things at once

Started by JamsoWamso, 19 January 2015 - 07:55 AM
JamsoWamso #1
Posted 19 January 2015 - 08:55 AM
Hi,

I'll start by explaining what I want to do. You know how in Windows, you can press a certain key during the boot sequence and the computer enters recovery mode? That's sort of what I'd like to do.

I'm attempting to create a very basic graphical operating system. I have a working login screen so far which willl kickstart the main program once I make that part.

What I'm trying to do now is the startup script, which is a seperate program to the login screen.

I have a false loading screen (I know it's pointless, but it provides an opportunity to enter recovery mode).
I have a function to advance the loading bar, and then launch the login program.

I also have a function which uses os.pullEvent("key") to detect a key press, and the key type. If the key type is the key ID of 'r' then the function launches the recovery mode program.

I want to run these two functions simultaneously, so that the loading bar advances while the computer also checks for a key press.

I'm not comfortable using coroutines yet, and I find the wiki page on the parallel API confusing.

Does anyone know of a way perform these functions simultaneously?

I'm typing this on an iPad late at night, so I'll post the code from my computer tomorrow if anyone would like to see it.
DannySMc #2
Posted 19 January 2015 - 03:42 PM
Hi

You need to look into os.pullEvent() and what it does, personally I am not a big fan of coroutines either as they seem to be problematic when drawing data to the screen and trying to get a buffer to work at the same time. So I don't use that, and the parallel api is good but I guess it makes less work.

So try this: if you wish to make a loading bar make it a function that updates every second?

So you could have a loading bar and an event running. So you use os.startTimer(1) and the use os.pullEvent() to catch the timer and any character press.

Post your code and I shall amend.

The idea is:
(This uses tables as it is easier to catch everything without having to add loads of variables)


while true do
local args = { os.pullEvent() }
if args[1] == "timer" then
  -- Here will be where you update the login bar so if you are using a loading bar make it go up a few bars per second
  -- then that gives the user time to press the recovery option.
elseif args[1] == "key" then
  -- get key press here
  -- Do recovery stuff
  -- REMEMBER to add a break once the key is pressed to make sure it breaks out of the while true loop. Example:
  recoverymodefunction()
  break
end
end
JamsoWamso #3
Posted 19 January 2015 - 07:47 PM
Hi.

Thanks for your help, I have it all working as I would like it now. :)/>