-
Notifications
You must be signed in to change notification settings - Fork 2
CoroutineCore
Last updated for version v0.3.1
This is a sub-addon in VExtensions that adds coroutines yes, lua coroutines]() to expression2. These work by isolating user defined functions and allowing the chip to run them in their own scopes.
From the Garry's Mod Wiki:
Coroutines are similar to threads, however they do not run simultaneously. They offer a way to split up tasks and dynamically pause & resume functions.
This is useful for spreading out multiple functions at once to avoid quota and wait for certain situations.
This should be fully functional and safe
If you find any bugs, please report them to the Issues page.
Creates a coroutine object to be run with :resume(). Searches for any function you define with the name function_name, keep in mind that function must take no arguments, or take a table as an argument. This is forced by whether you pass a table to resume/yield.
Creates a coroutine object to be run with :resume(). Searches for any function you define with the name function_name that ONLY takes a table as an argument. Creates the coroutine function while passing that data as a function argument. Not to be confused with passing data between resuming and yielding, which correspond to each other and not function arguments.
Returns the current e2 coroutine running, else nothing
Returns a string of the status of the coroutine, 'dead' for finished, 'suspended' for yielded, and 'running' if it is running.
Makes a coroutine wait for n amount of seconds, in this time, it is yielded and cannot be resumed (Like sleep() in python)
Makes a coroutine wait for n amount of seconds, in this time, it is yielded and cannot be resumed (Like sleep() in python)
Makes the coroutine pause until it is resumed again. Does not pass any data to the next resume call.
Makes the coroutine pause until it is resumed again. Passes the data to the next :resume() call.
Makes the coroutine pause until it is resumed again. Does not pass any data to the next resume call.
Makes the coroutine pause until it is resumed again. Passes the data to the next :resume() call.
Resumes the coroutine after it was previously paused. You cannot resume a coroutine when it has finished, so remember to check if it is dead with :status()
Resumes the coroutine after it was previously paused. You cannot resume a coroutine when it has finished, so remember to check if it is dead with :status(). It passes the given data to the previous yield() call inside of that coroutine, if you want to pass data in and out of the coroutine.
Returns a coroutine object that behaves as if the coroutine given was never started or was reset, 'rebooting' it
Returns a coroutine object that behaves as if the coroutine given was never started or was reset, 'rebooting' it. Requires that the function that the coroutine was made from takes a table, and passes "args" to that function as the first argument.
Returns an invalid coroutine.
Returns how many more coroutines you can make with this chip. Max is 1000.
Returns the maximum amount of coroutines you can make in a single E2 chip.
Returns how many times you can create a coroutine inside of a coroutine. This is to prevent infinite recursion crashes :P
@name CoroutineCore Example
@persist Co:coroutine
if(first()){
function thread(){
while(1){
coroutineWait(5)
print("5 seconds have passed")
}
}
Co = coroutine("thread")
runOnTick(1)
}elseif(tickClk()){
if(Co:status()!="dead"){
Co:resume()
}
}
See more in-depth examples in our Discussions