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

Delayed Callback

Started by Mitchfizz05, 12 December 2016 - 10:06 PM
Mitchfizz05 #1
Posted 12 December 2016 - 11:06 PM
Hello!

Out of the blue, I'm posting a small library I've made, which will be used for a larger project I'm working on.

It's a library that lets you specify a callback, and queue it for execution after a set amount of time without blocking the rest of the code from executing.

The general gist:

callback.new(function ()
print("Hello world!")
end, 5)

This would print "Hello world!" after 5 seconds.

When there are no more callbacks waiting to be executed, an event called "no_events_left" will be raised. This might be useful if you want your program to exit after all callbacks are finished, for example.

Setup

This library does require your program to have a main pullEvent loop, as it needs to be able to see all events happening to know when to execute the callback. How to do this depends on how your pullEvent loop is setup.

If you're loop is something like this: local e, param1, param2 = os.pullEvent() , or something similar, put this on the line after os.pullEvent()…

callback.pullEvent({e, param1})

If you're loop is something like this: local e = {os.pullEvent()} , or similar, put this after…

callback.pullEvent(e)

Obviously, substitute variable names where necessary.

Also, you'll want to load the API with os.loadAPI("callback").

If you're confused, feel free to check the demo (under downloads).

Download

pastebin get rsVLRw8t callback
pastebin get Gz1TFRas demo

The library is licensed under the MIT license (you don't need to ask me for permission to use, just give credit).

Thanks!
Edited on 12 December 2016 - 10:31 PM
justync7 #2
Posted 13 December 2016 - 01:22 AM
Huh, Seems like you were a bit late to the party.
Good job, nonetheless.
Mitchfizz05 #3
Posted 13 December 2016 - 05:02 PM
Huh, Seems like you were a bit late to the party.
Good job, nonetheless.

Ah damn it! xD

Eh well. I suppose this will still serve its purpose in my own project.