Skip to content

Grabber

namespace Griff.Scenes;
public sealed class Grabber : Component

Picks up Draggable things with a button and carries them, for games played with a controller or keys: a cursor moved with the stick or the D-pad (give it a TopDownMovement or a GridMovement), or a character who carries things about. Press Action to pick up what’s under its hand (or else the nearest thing within Reach), and again to put it down where it’s held, onto a DropTarget as a mouse would.

It holds things by their middle at its hand: its own middle, moved by HoldOffset. A drop the mouse would send back (onto a full target or one for other kinds, or off targets for something that only goes on them) does nothing, so it keeps holding the thing. Dropped onto a full target that swaps, it picks up what was there. In a network game each player’s grabber answers to its own player.

This is a component: its saved properties are on its component page.

public string? Action { get; set; }

The input action that picks things up and puts them down, e.g. “Interact” (E, Enter or the gamepad’s Y) or “Jump” (Space or A).

public string? DropMessage { get; set; }

A message sent with Game.Send when it puts something down, with what it put down as its data.

public Color Highlight { get; set; }

The color what it would pick up turns, so the player can see what a press takes. Transparent leaves things alone.

public Vector2 HoldOffset { get; set; }

Where it holds things, from its middle, in world units: X is how far in front of it (on the side it faces: left or right, or up or down in a top-down game), Y how far up. (0, 1) is over a character’s head, (0.8, 0) in front of it, (0, 0) a cursor’s middle.

public bool HoldToCarry { get; set; }

True if it only carries things while Action is held, putting them down when it’s let go, as a mouse does. Otherwise one press picks up and the next puts down.

public Entity? Holding { get; }

What it’s carrying (a Draggable’s entity), or null.

public Entity? InReach { get; }

What a press would pick up now (lit with Highlight), or null. Null while it’s carrying something.

public string? PickUpMessage { get; set; }

A message sent with Game.Send when it picks something up, with what it picked up as its data.

public float Reach { get; set; }

How far from its hand a thing’s middle can be and still be picked up, in world units, if nothing is right under its hand. 0 only picks up what’s under it.

public string? RefuseSound { get; set; }

A sound played when a press does nothing: nothing in reach, or nowhere to put what it holds.

public void Press()

Acts as if Action were pressed: picks up what’s in reach, or puts down what it holds. For scripts.

public event Action<Entity, Entity>? Dropped

Raised when it puts something down, with what it put down and the drop target it landed on (or null).

public event Action<Entity>? PickedUp

Raised when it picks something up, with what it picked up.