Source File
functions.go
Belonging Package
github.com/mlange-42/ark/ecs
package ecsimport// ComponentID returns the [ID] for a component type via generics.// Registers the type if it is not already registered.//// The number of unique component types per [World] is limited to 256 (64 with build tag ark_tiny).//// Panics if called on a locked world and the type is not registered yet.//// Note that type aliases are not considered separate component types.// Type re-definitions, however, are separate types.//// ⚠️ Warning: Using IDs that are outside of the range of registered IDs anywhere in [World]// or other places will result in undefined behavior!func [ any]( *World) ID {return .componentID(reflect.TypeFor[]())}// ComponentIDs returns a list of all registered component IDs.func ( *World) []ID {:= .storage.registry.IDs:= make([]ID, len())for , := range {[] = id8()}return}// ComponentInfo returns the [CompInfo] for a component [ID], and whether the ID is assigned.func ( *World, ID) (CompInfo, bool) {, := .storage.registry.ComponentType(.id)if ! {return CompInfo{}, false}return CompInfo{ID: ,Type: ,IsRelation: .storage.registry.IsRelation[.id],}, true}// TypeID returns the [ID] for a component type.// Registers the type if it is not already registered.//// The number of unique component types per [World] is limited to 256 (64 with build tag ark_tiny).func ( *World, reflect.Type) ID {return .componentID()}// Comp is a helper to pass component types to functions and methods.// Use function [C] to create one.type Comp struct {tp reflect.Type}// C creates a [Comp] instance for the given type.func [ any]() Comp {return Comp{reflect.TypeFor[]()}}// Alias for C if shadowed by another C.func comp[ any]() Comp {return Comp{reflect.TypeFor[]()}}// Type returns the reflect.Type of the component.func ( Comp) () reflect.Type {return .tp}// ResourceID returns the [ResID] for a resource type via generics.// Registers the type if it is not already registered.//// The number of resources per [World] is limited to 256 (64 with build tag ark_tiny).func [ any]( *World) ResID {return .resourceID(reflect.TypeFor[]())}// ResourceIDs returns a list of all registered resource IDs.func ( *World) []ResID {:= .resources.registry.IDs:= make([]ResID, len())for , := range {[] = ResID{id: }}return}// ResourceTypeID returns the [ResID] for a resource type.// Registers the type if it is not already registered.//// See [ResourceID] for a more commonly used generic variant.func ( *World, reflect.Type) ResID {return .resourceID()}// ResourceType returns the reflect.Type for a resource [ResID], and whether the ID is assigned.func ( *World, ResID) (reflect.Type, bool) {return .resources.registry.ComponentType(.id)}// GetResource returns a pointer to the given resource type in the world.// Returns nil if there is no such resource.//// Uses reflection. For more efficient repeated access, use [Resource].// Once created, it is more than 20 times faster than the GetResource function.//// See also [AddResource].func [ any]( *World) * {:= .resources.Get(ResourceID[]())if == nil {return nil}return .(*)}// AddResource adds a resource to the world.// Returns the ID for the added resource.//// Panics if there is already such a resource.//// Uses reflection. For more efficiency when used repeatedly, use [Resource].//// The number of resources per [World] is limited to 256 (64 with build tag ark_tiny).//// See also [GetResource].func [ any]( *World, *) ResID {:= ResourceID[]().resources.Add(, )return}
![]() |
The pages are generated with Golds v0.8.7. (GOOS=linux GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds. |