Accessor<T extends Component> extension type

One entity seen as its T component - what entity<Transform3D>() returns, and where a component's helper methods live.

A helper reached this way takes only what it is about, because the entity it operates on is the receiver:

entity<Transform3D>().setEuler(yaw: 0.5);
entity<Transform3D>().lookAt(0, 0, 0);
final gap = entity<Transform3D>().distanceTo(other);

Writing one for your own component

Declare an extension named after the component, on Accessor of it:

mixin Health on Component {
  late final DataPointer<int> hp;

  @override
  void describeStruct(DataDescriptor data) {
    super.describeStruct(data);
    hp = data.hasInt32(100);
  }
}

extension HealthAccessor on Accessor<Health> {
  void damage(int amount) {
    final health = component;
    health.hp[this] = health.hp[this] - amount;
  }
}

Inside the extension this is the entity, so a column indexes with this directly, and component hands back the Health it belongs to. Hold that in a local when you touch it more than once - each read resolves the component again.

A helper taking a second entity reads that one through its own component (other.get<Health>()), because it may be a different archetype with a different row layout. Only the receiver is guaranteed to be this one.

Everything on Entity is available too - destroy(), sceneSlot, tryGet - and an accessor can be passed anywhere an entity is wanted, because it implements Entity.

Why the helpers go here and not on the component

Two components can want the same method name, and Accessor<Health> and Accessor<Transform3D> are different types, so each can declare distanceTo or damage without the two colliding in a library that imports both. A helper put on the component mixin, or on Entity itself, has no such separation.

It costs nothing: Accessor<T> erases to Entity, which erases to int, so identical(entity<T>().entity, entity) holds and nothing is allocated to reach a helper.

on
Implemented types
Available extensions

Constructors

Accessor(Entity entity)
const

Properties

address Pointer<Never>

Available on int, provided by the IntAddress extension

The memory address of the underlying data.
no setter
archetypeId int
no setterinherited
bitLength int
Returns the minimum number of bits required to store this integer.
no setterinherited
component → T
no setter
entity Entity
final
hashCode int
Returns a hash code for a numerical value.
no setterinherited
isEven bool
Returns true if and only if this integer is even.
no setterinherited
isFinite bool
Whether this number is finite.
no setterinherited
isInfinite bool
Whether this number is positive infinity or negative infinity.
no setterinherited
isNaN bool
Whether this number is a Not-a-Number value.
no setterinherited
isNegative bool
Whether this number is negative.
no setterinherited
isOdd bool
Returns true if and only if this integer is odd.
no setterinherited
oneBitCount int
The number of 1 bits in the binary representation of this integer.
no setterinherited
pageIndex int
no setterinherited
rowOffset int
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
scene Scene

Available on Entity, provided by the EntityLifetime extension

The loaded scene this entity lives in.
no setter
sceneSlot int
Which loaded scene this row belongs to, as Scene.slot, or -1 if its page has been freed or it was created outside any scene.
no setterinherited
sign int
Returns the sign of this integer.
no setterinherited
toJS JSNumber

Available on num, provided by the NumToJSExtension extension

Converts this num to a JSNumber.
no setter
trailingZeroBitCount int
The number of trailing (least significant) zero bits in the binary representation of this integer.
no setterinherited
value int
finalinherited

Methods

abs() int
Returns the absolute value of this integer.
inherited
addChild(Entity child) → void

Available on Accessor<Parent>, provided by the ParentAccessor extension

Appends child to the end of this entity's child list - a doubly-linked splice through firstChild/lastChild and child's own nextSibling/prevSibling.
adopt(Entity child) → void

Available on Accessor<Parent>, provided by the ParentAccessor extension

Moves child here from wherever it is, keeping it and its subtree alive
call<T extends Component>() Accessor<T>
This entity, seen as its T component:
inherited
ceil() int
Returns this.
inherited
ceilToDouble() double
Returns this.toDouble().
inherited
clamp(num lowerLimit, num upperLimit) num
Returns this num clamped to be in the range lowerLimit-upperLimit.
inherited
compareTo(num other) int
Compares this to other.
inherited
destroy() → void

Available on Entity, provided by the EntityLifetime extension

Destroys one entity: its subtree, then its links, then its row.
detach() → void

Available on Accessor<Child>, provided by the ChildAccessor extension

Unlinks this entity from its parent, keeping it and its subtree alive.
floor() int
Returns this.
inherited
floorToDouble() double
Returns this.toDouble().
inherited
gcd(int other) int
Returns the greatest common divisor of this integer and other.
inherited
get<T extends Component>() → T
The prefab describing this entity's archetype - the instance holding the DataPointer fields the struct's mixins wrote into their late finals. Combine with the entity itself to reach data: instance.get<Transform2D>().transformOffsetX[instance].
inherited
modInverse(int modulus) int
Returns the modular multiplicative inverse of this integer modulo modulus.
inherited
modPow(int exponent, int modulus) int
Returns this integer to the power of exponent modulo modulus.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
remainder(num other) num
The remainder of the truncating division of this by other.
inherited
removeChild(Entity child) → void

Available on Accessor<Parent>, provided by the ParentAccessor extension

Destroys child and everything under it.
round() int
Returns this.
inherited
roundToDouble() double
Returns this.toDouble().
inherited
toDouble() double
This number as a double.
inherited
toInt() int
Truncates this num to an integer and returns the result as an int.
inherited
toRadixString(int radix) String
Converts this int to a string representation in the given radix.
inherited
toSigned(int width) int
Returns the least significant width bits of this integer, extending the highest retained bit to the sign. This is the same as truncating the value to fit in width bits using an signed 2-s complement representation. The returned value has the same bit value in all positions higher than width.
inherited
toString() String
Returns a string representation of this integer.
inherited
toStringAsExponential([int? fractionDigits]) String
An exponential string-representation of this number.
inherited
toStringAsFixed(int fractionDigits) String
A decimal-point string-representation of this number.
inherited
toStringAsPrecision(int precision) String
A string representation with precision significant digits.
inherited
toUnsigned(int width) int
Returns the least significant width bits of this integer as a non-negative number (i.e. unsigned representation). The returned value has zeros in all bit positions higher than width.
inherited
truncate() int
Returns this.
inherited
truncateToDouble() double
Returns this.toDouble().
inherited
tryGet<T extends Component>() → T?
get, but null instead of throwing when the archetype lacks T - the OptWith<Child>() half of a query.
inherited

Operators

operator %(num other) num
Euclidean modulo of this number by other.
inherited
operator &(int other) int
Bit-wise and operator.
inherited
operator *(num other) num
Multiplies this number by other.
inherited
operator +(num other) num
Adds other to this number.
inherited
operator -(num other) num
Subtracts other from this number.
inherited
operator /(num other) double
Divides this number by other.
inherited
operator <(num other) bool
Whether this number is numerically smaller than other.
inherited
operator <<(int shiftAmount) int
Shift the bits of this integer to the left by shiftAmount.
inherited
operator <=(num other) bool
Whether this number is numerically smaller than or equal to other.
inherited
operator ==(Object other) bool
Test whether this value is numerically equal to other.
inherited
operator >(num other) bool
Whether this number is numerically greater than other.
inherited
operator >=(num other) bool
Whether this number is numerically greater than or equal to other.
inherited
operator >>(int shiftAmount) int
Shift the bits of this integer to the right by shiftAmount.
inherited
operator >>>(int shiftAmount) int
Bitwise unsigned right shift by shiftAmount bits.
inherited
operator [](Child child) Entity

Available on Accessor<Parent>, provided by the ParentAccessor extension

The entity of child that this one owns - the other half of what EntityStruct.of declares.
operator ^(int other) int
Bit-wise exclusive-or operator.
inherited
operator unary-() int
Return the negative value of this integer.
inherited
operator |(int other) int
Bit-wise or operator.
inherited
operator ~() int
The bit-wise negate operator.
inherited
operator ~/(num other) int
Truncating division operator.
inherited