class

Godot::Channel

Inherits Godot::RefCounted < Godot::Object < Reference < Object

A thread-safe, actor-style communication channel bridging Crystal OS threads, cooperative gameplay fibers, Godot C++ threads, and GDScript coroutines.

Inherits from Godot::RefCounted and registers with ClassDB as GodotChannel.

Concurrency Invariant: In Crystal 1.20+, raw OS threads (Thread.new) do not possess a fiber execution context (Fiber#execution_context is nil). Attempting to suspend an empty unbuffered Crystal Channel(T) on an OS thread raises NilAssertionError. Godot::Channel guarantees safety across all threads by utilizing native OS synchronization (Thread::Mutex + Thread::ConditionVariable) and cooperative yielding (Fiber.yield) on the Main Thread.

Constructors

new(capacity : Int32 = 16) : Channel
Source
new(pointer : Pointer(Void)) : Channel
Source

Instance methods

_godot_call_virtual_with_data(method_name : String, args : Pointer(Pointer(Void)), ret : Pointer(Void)) : Void

Dispatches generic virtual methods and ClassDB method calls with raw arguments and return buffer

Source
await_receive(timeout_sec : Float64 | Nil = nil) : ChannelItem | Nil

Cooperatively awaits an item from the channel inside a Crystal gameplay fiber. Yields execution slices via Fiber.yield each frame, preventing engine starvation.

Source
call(method : String, *args) : Pointer(Void)

Dynamic method dispatch for GDScript invocation

Source
capacity
Source
capacity=(capacity : Int32)
Source
close

Closes the channel. Unblocks all waiting senders and receivers and emits signal closed.

Source
closed?
Source
empty?

Returns true if the channel currently holds no items

Source
full?

Returns true if the channel buffer is at full capacity

Source
is_closed
Source
is_empty
Source
is_full
Source
receive(timeout_sec : Float64 | Nil = nil) : ChannelItem | Nil

Receives an item from the channel, blocking the calling thread until an item is available. Safe for background OS worker threads (Thread.new, WorkerThreadPool). Do NOT call this on Godot's Main Thread; use #try_receive or #await_receive instead!

Source
send(val : ChannelItem) : Bool

Sends an item into the channel, blocking if the channel is full until space becomes available. Returns true on success, or false if the channel was closed. Automatically dispatches signal received on Godot's Main Thread via call_deferred.

Source
size

Returns the current number of queued items

Source
try_receive

Non-blocking receive operation. Returns the next queued item, or nil if the channel is empty. Safe to call anywhere, including in _process or timer callbacks on the Main Thread.

Source
try_send(val : ChannelItem) : Bool

Sends an item into the channel without blocking. Returns true if successfully enqueued, or false if full or closed.

Source