Skip to content

Coroutine Methods

Juju Adams edited this page Dec 15, 2021 · 9 revisions

.Get()

Returns: Any type, a value that has been passed by YIELD...THEN, RETURN...THEN, or PAUSE...THEN

Name Datatype Purpose
None

If no value has been returned from the coroutine then this method returns undefined. The returned value is reset to undefined after resuming execution after a YIELD...THEN or PAUSE...THEN command. if the coroutine reaches the end of its code, the return value is reset to undefined; if you'd like a coroutine to return a specific value, use the RETURN command (as is normal for other GML functions). If the coroutine is cancelled using the .Cancel() method then the return value is also reset to undefined.

 

.GetCreator()

Returns: An instance ID or a struct, the scope that the coroutine was created in

Name Datatype Purpose
None

 

.GetPaused()

Returns: Boolean, whether the coroutine was paused using the PAUSE...THEN command or the .Pause() method

Name Datatype Purpose
None

 

.Pause([returnValue])

Returns: undefined

Name Datatype Purpose
[returnValue] any Value to return via the .Get() function. If not specified, undefined will be used

Pauses execution of the coroutine, allowing it to be resumed at some later point. Coroutines that are waiting for an async event via the ASYNC_AWAIT_* commands will be paused after the ASYNC_AWAIT_* code has completed, this is so that async events cannot be accidentally missed. If the coroutine is already paused or has already completed then this method does nothing.

This method cannot be called from within the coroutine; instead please use the PAUSE command.

Please note that this method should not be used to pause a coroutine from within its own code. Instead, use the PAUSE...THEN command.

 

.Resume()

Returns: undefined

Name Datatype Purpose
None

Resumes execution of the coroutine after it has been paused. If the coroutine was not paused or has already completed then this method does nothing. .Resume() resets the returned value for the coroutine to undefined.

 

.GetComplete()

Returns: Boolean, whether the coroutine has completed

Name Datatype Purpose
None

A coroutine is considered "complete" when any of the three following things happens:

  1. The coroutine runs out of code to execute, much like you'd expect with normal functions
  2. Upon reaching a RETURN command
  3. When the coroutine is cancelled using the .Cancel() method (see below)

In any case, the CO_ON_COMPLETE completion code is executed.

 

.Cancel()

Returns: undefined

Name Datatype Purpose
None

Immediately halts the coroutine, completing it; the CO_ON_COMPLETE completion code will be executed accordingly. .Cancel() resets the returned value for the coroutine to undefined.

This method cannot be called from within the coroutine; instead please use the RETURN command.

 

.Restart()

Returns: undefined

Name Datatype Purpose
None

Immediately halts execution of the coroutine and restarts code execution from the beginning of the coroutine's code. This will also call CO_ON_COMPLETE. .Restart() sets the returned value for the coroutine to undefined.

This method cannot be called from within the coroutine; instead please use the RESTART command.

Please note that this method does not reset variables set in the coroutine.

 

.WeakReference(state)

Returns: undefined

Name Datatype Purpose
state boolean Whether this coroutine should hold a weak reference to its creator (if that creator is a struct)

 

.CancelWhenOrphaned(state)

Returns: undefined

Name Datatype Purpose
state boolean Whether this coroutine should automatically cancel itself if its creator is destroyed or garbage collected

If a coroutine is cancelled due to its creator being destroyed or garbage collected, the CO_ON_COMPLETE code block will still be executed.

N.B. A deactivated instance counts as a non-existent instance.