Log inGet started
World

First Light

Branchmain
Branchesmain
Commit5b1bfa8dc1

README.md

firstlight.interact

What the player is looking at and can use. A registry of entities that answer to the interact key, a per-frame raycast from the player's camera that finds which one is under the reticle, and current() for the HUD to draw the prompt.

local Interact = require("firstlight.interact")

API

CallWhat it does
Interact.register(entity, { label, onUse, key? })Make an entity usable. entity is a proxy or an id. label is a string, or a function returning the label for this moment — or nil when there is nothing to do right now (an empty crate, a trap already set), which hides the prompt. onUse runs on the interact key. key overrides the legend (default "E").
Interact.unregister(entity)Forget it.
Interact.current(){ label, key, entity, distance } for the target under the reticle within reach, or nil. The HUD draws the reticle prompt from this.
Interact.use()Use the current target. Returns whether something was used.
Interact.scan(camera, playerRoot)The per-frame raycast. firstlight.sites calls it every play frame from the local player's camera; nothing else needs to.
Interact.isRegistered(entity) / Interact.targets()Introspection.
Interact.REACH_MReach in metres (3).

A hit on any descendant of a registered entity counts as a hit on it, so a bundle's mesh children and a collider child resolve to the prop. Targets need a collider for the ray to find them; firstlight.sites fits an invisible interact_hit box to anything it registers that has none.

Registering from another slice

local stove = entity.find("stove")
Interact.register(stove, {
    label = function()
        return if Rules.state.hut.stoveOn then "Stoke stove" else "Light stove"
    end,
    onUse = function() stove.component.get("firstlight.stove"):light() end,
})

The interact key is the interact control of @builtin::inputMaps.default (E on a keyboard, the west face button on a pad, an on-screen button on a phone). firstlight.sites owns that subscription and calls Interact.use().