class Godot::GodotArray(T)

Overview

A high-level Crystal wrapper around Godot's native Array type. Named GodotArray to avoid shadowing Crystal's top-level ::Array. Implements Enumerable and provides seamless conversion to/from Crystal Array.

Included Modules

Defined in:

libgodot/collections.cr

Constructors

Class Method Summary

Instance Method Summary

Instance methods inherited from class Object

try?(&)
try?
try?

Constructor Detail

def self.new(ptr : Pointer(Void) = Pointer(Void).null) #

def self.new(initial_items : Array(T)) #

Class Method Detail

def self.from(items : Array(T)) : GodotArray(T) #

def self.from_bridge_payload(payload_ptr : Pointer(Void)) : GodotArray(T) #

Instance Method Detail

def <<(value : T) : self #

def [](index : Int32) : T #

def [](index : Int64) : T #

def []=(index : Int32, value : T) : T #

def []=(index : Int64, value : T) : T #

def []?(index : Int32) : T | Nil #

def []?(index : Int64) : T | Nil #

def append(value : T) : self #

def clear : Void #

def clone : GodotArray(T) #

def dup : GodotArray(T) #
Description copied from class Reference

Returns a shallow copy of this object.

This allocates a new object and copies the contents of self into it.


def duplicate(deep : Bool = false) : GodotArray(T) #

def each(& : T -> ) : Void #
Description copied from module Enumerable(T)

Must yield this collection's elements to the block.


def empty? : Bool #
Description copied from module Enumerable(T)

Returns true if self does not contain any element.

([] of Int32).empty? # => true
([1]).empty?         # => false
[nil, false].empty?  # => false
  • #present? returns the inverse.

def filter_as(type : U.class) : Array(U) forall U #

def first? : T | Nil #
Description copied from module Enumerable(T)

Returns the first element in the collection. When the collection is empty, returns nil.

([1, 2, 3]).first?   # => 1
([] of Int32).first? # => nil

def is_empty : Bool #

def last? : T | Nil #

def pop : T | Nil #

def push(value : T) : self #

def sample : T | Nil #
Description copied from module Enumerable(T)

Returns a random element from self. All elements have equal probability of being drawn.

Raises IndexError if self is empty.

a = [1, 2, 3]
a.sample # => 2
a.sample # => 1

Uses the random instance when provided if the randomness needs to be controlled or to follow some traits. For example the following calls use a custom seed or a secure random source:

a.sample(Random.new(1))  # => 3
a.sample(Random::Secure) # => 1

def shift : T | Nil #

def size : Int64 #
Description copied from module Enumerable(T)

Returns the number of elements in the collection.

[1, 2, 3, 4].size # => 4

def to_a : Array(T) #
Description copied from module Enumerable(T)

Returns an Array with all the elements in the collection.

(1..5).to_a # => [1, 2, 3, 4, 5]

def to_array : Array(T) #

def to_s(io : IO) : Void #
Description copied from class Reference

Appends a short String representation of this object which includes its class name and its object address.

class Person
  def initialize(@name : String, @age : Int32)
  end
end

Person.new("John", 32).to_s # => #<Person:0x10a199f20>

def write_bridge_payload(ret_ptr : Pointer(Void)) : Void #