assertion

package
v0.4.25 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 2, 2026 License: LGPL-2.1 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func As

func As[T any](entity ec.Entity) (*T, bool)

As 从实体中提取一些需要的组件,复合在一起直接使用。

示例:

type IA interface {
	MethodA()
}
...
type IB interface {
	MethodB()
}
...
type CompositeAB struct {
	IA `ec:"A"`			// 指定组件名
	IB `ec:",CompB"`	// 指定组件原型名
}
...
entity.AddComponent("A", compA)
entity.AddComponent("B", compB)
...
v, ok := As[CompositeAB](entity)
if ok {
	v.MethodA()
	v.MethodB()
}

注意:

1.类型参数必须为结构体类型,可以是匿名结构体。
2.内部逻辑有使用反射,为了提高性能,可以使用一次后存储转换结果重复使用。
3.实体更新组件后,需要重新提取。
4.需要使用tag标记组件名或组件原型名,若没有标记,将会尝试使用字段名作为组件名去查找。
5.提取失败会返回false。

func Cast

func Cast[T any](entity ec.Entity) *T

Cast 从实体中提取一些需要的组件,复合在一起直接使用,提取失败会panic。

示例:

type IA interface {
	MethodA()
}
...
type IB interface {
	MethodB()
}
...
type CompositeAB struct {
	IA `ec:"A"`			// 指定组件名
	IB `ec:",CompB"`	// 指定组件原型名
}
...
entity.AddComponent("A", compA)
entity.AddComponent("B", compB)
...
Cast[CompositeAB](entity).MethodA()
Cast[CompositeAB](entity).MethodB()

注意:

1.类型参数必须为结构体类型,可以是匿名结构体。
2.内部逻辑有使用反射,为了提高性能,可以使用一次后存储转换结果重复使用。
3.实体更新组件后,需要重新提取。
4.需要使用tag标记组件名或组件原型名,若没有标记,将会尝试使用字段名作为组件名去查找。
5.提取失败会panic。

func Inject

func Inject(entity ec.Entity, target any) error

Inject 向目标注入组件

func InjectRV

func InjectRV(entity ec.Entity, target reflect.Value) error

InjectRV 向目标注入组件

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL