Behavior
namespace Griff.Runtime;
public abstract class Behavior : ComponentBase class for gameplay scripts. Override Start and Update (every step of the game), and FixedUpdate to push bodies just before they move; public read/write properties are saved with the scene and editable in the inspector.
Properties
Section titled “Properties”protected IAudio Audio { get; }ClaimsBack
Section titled “ClaimsBack”protected virtual bool ClaimsBack { get; }Override it to have Back (Esc and the gamepad’s B) for this script while it’s true: meanwhile they don’t open a Panel on its Open On or close one that lets the game go on behind it, so the script’s own reading of them (putting a tool down, skipping a cutscene) comes first, and the next press, once it’s false, opens the game menu. Only a script that’s running claims it: not one on a disabled or paused entity, or behind a panel that pauses the game.
protected override bool ClaimsBack => _tool is not null;protected DebugDraw Debug { get; }Draws lines, boxes, circles and text over the game for a frame; see DebugDraw.
protected Game Game { get; }The running game. Only available from Start onwards.
protected IInput Input { get; }The controls of whoever plays this entity: this machine’s, or in a network game, those of the player it belongs to (see InputOf).
Tables
Section titled “Tables”protected TableSet Tables { get; }The game’s tables (Tables): float damage = Tables.Get("Spells", "Fireball")["Damage"];, or a class of your own filled from a row, Tables.Get<Spell>("Spells", "Fireball").
protected Time Time { get; }Transform
Section titled “Transform”protected Transform Transform { get; }Methods
Section titled “Methods”AddSystem
Section titled “AddSystem”protected T AddSystem<T>(T system)Runs system every frame until this entity is destroyed or left behind by a scene change, or the script is reloaded; see GameSystem. Add it in Start. A system of the same type is replaced, so a scene that adds one each time it starts still runs just one.
protected GameTimer After(float seconds, Action action)Calls action once after seconds, unless this entity is destroyed first.
protected GameTimer Every(float seconds, Action action)Calls action every seconds while this entity exists.
FadeTo
Section titled “FadeTo”protected Tween FadeTo(float alpha, float seconds, Func<float, float>? ease = null)Fades this entity’s sprite, text and tilemap to alpha.
FixedUpdate
Section titled “FixedUpdate”protected virtual void FixedUpdate()Called every step just before bodies move (after Update and everything else that runs before physics), and only while game time moves: not while the game is paused, at a Scale of 0, or frozen by a hit stop, when physics doesn’t step either. Delta is the time the bodies are about to move by. The place to push bodies: set velocities, add forces.
protected void Flash(Color? color = default, float seconds = 0.1f)Paints this entity’s sprites (and its children’s) color, white by default, for a moment.
GetComponent
Section titled “GetComponent”protected T? GetComponent<T>()LateUpdate
Section titled “LateUpdate”protected virtual void LateUpdate()Called every step after Update, once everything else has moved: physics, pickups and damage, and cameras following their targets. Good for a camera of your own, or something that stays by what moved. Screen anchors are placed afterwards, so they stay pinned to wherever the camera ends up.
MoveTo
Section titled “MoveTo”protected Tween MoveTo(Vector2 position, float seconds, Func<float, float>? ease = null)Glides this entity to position (in its parent’s space).
protected MessageListener On(string message, Action action)Calls action whenever someone sends message with Game.Send, until this entity is destroyed or the script is reloaded (Start then runs again on the new version, so listen in Start).
protected MessageListener On<T>(string message, Action<T> action)Like On, with the data sent along, e.g. On<int>("ScoreChanged", score => ...).
OnClick
Section titled “OnClick”protected virtual void OnClick()Called when this entity’s Button is clicked (or its shortcut key pressed).
OnCollision
Section titled “OnCollision”protected virtual void OnCollision(Entity other)Called when this entity’s body bumps into a solid collider, or something bumps into it.
OnDamage
Section titled “OnDamage”protected virtual void OnDamage(float amount, Entity? source)Called when this entity’s Health takes damage, with how much and what dealt it (null if a script didn’t say).
OnDeath
Section titled “OnDeath”protected virtual void OnDeath()Called once when this entity’s Health reaches zero, before it’s destroyed.
OnDestroy
Section titled “OnDestroy”protected virtual void OnDestroy()Called when the entity is destroyed or the game stops.
OnLoaded
Section titled “OnLoaded”protected virtual void OnLoaded()Called once a saved game has been loaded (LoadGame, a Continue), after this script’s Start and once every script’s saved fields are back: its private fields, the plain objects and lists they hold and the entities they point to. A coroutine that was running isn’t saved (the next build of the scripts couldn’t carry it on), so restart the work here from what the fields say: a worker picks up the task it holds. Nothing by default.
OnPickup
Section titled “OnPickup”protected virtual void OnPickup(Entity pickup)Called when this entity collects a Pickup, e.g. to heal on a heart: if (pickup.Name == "Heart"). The pickup disappears at the end of the frame.
OnTriggerEnter
Section titled “OnTriggerEnter”protected virtual void OnTriggerEnter(Entity other)Called when a trigger and another collider start overlapping (on both entities).
OnTriggerExit
Section titled “OnTriggerExit”protected virtual void OnTriggerExit(Entity other)Called when a trigger and another collider stop overlapping (on both entities).
RotateTo
Section titled “RotateTo”protected Tween RotateTo(float degrees, float seconds, Func<float, float>? ease = null)Turns this entity to degrees around Z.
ScaleTo
Section titled “ScaleTo”protected Tween ScaleTo(Vector2 scale, float seconds, Func<float, float>? ease = null)Grows or shrinks this entity to scale on X and Y.
protected virtual void Start()Called once, before the first Update, after every behavior in the scene exists.
StartCoroutine
Section titled “StartCoroutine”protected Coroutine StartCoroutine(IEnumerator routine)Runs routine over several frames, e.g. StartCoroutine(Waves()); see Coroutine. It stops if this entity is destroyed, and when the script is reloaded (Start then runs again on the new version).
StopCoroutines
Section titled “StopCoroutines”protected void StopCoroutines()Stops every coroutine this behavior started.
StopListening
Section titled “StopListening”protected void StopListening()Stops every message listener this behavior started.
protected Tween Tween(float seconds, Action<float> apply, Func<float, float>? ease = null)Animates a value over seconds; see Tween. Stops if this entity is destroyed.
Update
Section titled “Update”protected virtual void Update()Called every step of the game while the behavior and its entity are enabled: 60 times a second (see StepRate) whatever the screen’s frame rate, with Delta the step’s game time, so the same input plays the same everywhere. The screen is drawn between steps, so what moves here moves smoothly. Read the controls here: a press counts in one step. While a panel pauses the game, only the scripts on open panels update.
protected static WaitSeconds Wait(float seconds)Yield this from a coroutine to wait seconds of game time.
WaitUntil
Section titled “WaitUntil”protected static WaitCondition WaitUntil(Func<bool> condition)Yield this from a coroutine to wait until condition is true (checked every frame).
WaitWhile
Section titled “WaitWhile”protected static WaitCondition WaitWhile(Func<bool> condition)Yield this from a coroutine to wait as long as condition is true.