Behaviours
A behaviour is what an object does, written once and put on any
object: health, a spin, a flash when hit. It’s a Behaviour subclass,
added in components: or with addComponent, and it gets every call an
object gets (onStart, onUpdate, …):
class Spin extends Behaviour { Spin({this.speed = 1});
double speed;
@override void onUpdate() { super.onUpdate(); rotate(Vec3.up, speed * time.delta); // its object's transform }}
class Coin extends GameObject { Coin() : super(components: [MeshRenderer(Mesh.sphere(), Material(color: Color(0xFFFFC030))), Spin(speed: 3)]);
void stop() => get<Spin>()?.enabled = false; // still there, but not called}From a behaviour, its object is near: gameObject, and the same
shortcuts an object has (transform, position, get<T>(), world,
time, input, assets, startRoutine, on).
Adding and finding components
Section titled “Adding and finding components”addComponent(b)puts a behaviour on at any time and returns it;removeComponent(b)takes it off for good.get<T>()gives the first component of typeT(or a subtype, or an interface it implements), or null;getAll<T>()all of them;has<T>().getInParent<T>()andgetInChildren<T>()look up or down the tree too, andgetAllInChildren<T>()gives every one below; as Unity’s, they skip objects turned off unlessincludeInactive: true. They find components:getInParent<Level>()for aLevelobject throws and points atancestor<Level>().enabled = falsestops its calls, its routines and its tweens until it’s turned back on.- A destroyed object still has its components, to read: a dead enemy’s
get<Health>()still says how much it took. - To keep a behaviour in a typed field, make it in a constructor of your
own and pass it on, so the field and the components share it:
Enemy() : this._(Health(3));thenEnemy._(this.health) : super(components: [health]);withfinal Health health;.
A subclass or a behaviour?
Section titled “A subclass or a behaviour?”Which one? If a subclass’s onUpdate would make sense on another kind
of object, it belongs in a behaviour. Keep a class for what an object is
(its state, its parts, its typed fields), and behaviours for what it
does that others could do too, or that comes and goes while it plays.