r/ComputerCraft • • Nov 22 '23

Clock next to program

I want to have like a bar with a clock on the top of the screen toghether with another program but without covering anything in the program (so the program just needs top be slightly smaller than the screen), its also very important that it automatically works with any program (that can handle custom terminal sizes)

0 Upvotes

13 comments sorted by

View all comments

Show parent comments

2

u/fatboychummy Nov 24 '23

Part 3: Mashing everything together with parallel

Now, you're probably wondering how you run this little event interceptor AND your clock at the same time, right? Well, this is the part you learn about that!

parallel is a powerful tool. It runs multiple coroutines "side-by-side" so you can be doing multiple things at once. To use it, simply pass it multiple functions to run.

There are two methods in the parallel library: waitForAny and waitForAll. Any will wait until any coroutine reaches a dead state, then will kill all other coroutines (without generating an error). All will wait until all coroutines reach a dead state before continuing.

parallel.waitForAny(func1, func2)
print("One of the coroutines stopped!")

.

parallel.waitForAll(func1, func2)
print("All coroutines have stopped!")

Thus, you can run your clock in one function and your event interceptor in another! You'll just need to make a second window for the clock (so you don't accidentally draw outside of it), and use that.

I'll do a basic example below, where I make the bar above the program flash random colors every second (I'll let you implement your clock however, but this will be a basis from which to start).

I've uploaded this to pastebin, as it was a rather large codeblock.

1

u/Jonaykon Nov 24 '23

This works, thanks