Skip to content

CoroutineCore

Vurv edited this page Feb 14, 2021 · 12 revisions

Last updated for version v0.3.1

Main Info

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.

Documentation

Functions

coroutine(s function_name)

Creates a coroutine object to be run with xco: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.

coroutine(s function_name, t data)

Creates a coroutine object to be run with xco: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.

xco = coroutineRunning()

Returns the current e2 coroutine running, else nothing

s = xco:status()

Returns a string of the status of the coroutine, 'dead' for finished, 'suspended' for yielded, and 'running' if it is running.

xco:wait(n wait_seconds)

Makes a coroutine wait for n amount of seconds, in this time, it is yielded and cannot be resumed (Like sleep() in python)

coroutineWait(n wait_seconds)

Makes a coroutine wait for n amount of seconds, in this time, it is yielded and cannot be resumed (Like sleep() in python)

xco:yield()

Makes the coroutine pause until it is resumed again. Does not pass any data to the next resume call.

xco:yield(t data)

Makes the coroutine pause until it is resumed again. Passes the data to the next xco:resume() call.

coroutineYield()

Makes the coroutine pause until it is resumed again. Does not pass any data to the next resume call.

coroutineYield(t data)

Makes the coroutine pause until it is resumed again. Passes the data to the next xco:resume() call.

xco:resume()

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 xco:status()

xco:resume(t data)

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 xco: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.

xco = xco:reboot()

Returns a coroutine object that behaves as if the coroutine given was never started or was reset, 'rebooting' it

xco = xco:reboot(t args)

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.

xco = nocoroutine()

Returns an invalid coroutine.

n = coroutinesLeft()

Returns how many more coroutines you can make with this chip. Max is 1000.

Constants

n XCO_MAX

Returns the maximum amount of coroutines you can make in a single E2 chip.

n XCO_STACK_MAX

Returns how many times you can create a coroutine inside of a coroutine. This is to prevent infinite recursion crashes :P

Example

@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