Log inGet started
World

Builtin Library

Branchmain
Branchesmainpublish
Commit0a13e47b57

README.md

applicable

Can be applied for an owner, with values for its declared parameters. A type that lists applicable under interfaces: in its type.yaml guarantees that any of its assets can be put in effect for an owner, with values for the parameters the asset declares (asset.schema), and that the owner can change those values, turn the application off and on, and drop it, which leaves nothing behind, and that the owner can read the application's state as it stands. A caller applies any implementer with the same calls; here ref is any asset whose type lists applicable:

local view = ref:as("applicable")
local state = view:apply("owner-key", {})
view:setEnabled("owner-key", false)
view:state("owner-key")
view:drop("owner-key")

The members

MemberWhat it does
apply(self, owner, values, opts?) -> ApplyStatePuts the asset in effect for owner with values. Called again for the same owner, it changes the values.
setEnabled(self, owner, enabled) -> ApplyStateTurns that application on or off while it stays applied; its values are kept.
drop(self, owner)Removes that application entirely. Dropping an owner with nothing applied does nothing.
state(self, owner) -> ApplyState?The application's state now, the answer apply would give, read without applying. nil when nothing is applied for owner.

owner is a key the caller forms from something only it holds, so two callers never share an application. With the asset, it identifies the application across the calls.

ApplyState is { status = "inForce" | "off" | "held", heldBy?, notice? }: held when another application is in force instead (heldBy names it), and notice the words a person should see about the application. A type may return more fields beside these and documents them itself. An application's state can change with no call for it, when another application of the same kind comes or goes; state answers it as it stands on the frame it is asked.

The values an application receives

Every call of a member passes through the interface's contract (@builtin::interfaces.applicable.contract), which keeps one record per application: the values the owner stated and whether it is on. The type's apply receives the asset's own values (asset.settings, else its schema's defaults) with what the owner stated laid over them. A name values gives that the schema does not declare, or a value its descriptor refuses, raises at the call, naming the asset and the parameter. Apply.values(self, owner) and Apply.stated(self, owner) read an application's record.

After an edit/play flip, a render-device rebuild, and an edit of an applied asset, the contract applies every record again; no owner acts. A type's apply puts back what of its effect is gone and leaves what still stands as it is.

Implementing it

# <type>.assetType/type.yaml
interfaces: [applicable]
settings: { values: "<type>.yaml", schema: { type: "settings.luau" } }
-- <type>.assetType/behavior.luau
local Apply = require("@builtin::interfaces.applicable.contract")
local M = {}
M.ref = {
    apply = function(self, owner, values, opts)
        -- make the application `owner` be `values`, on when `opts.enabled`
        return { status = if opts.enabled then "inForce" else "off" }
    end,
    setEnabled = function(self, owner, enabled)
        return { status = if enabled then "inForce" else "off" }
    end,
    drop = function(self, owner)
        -- remove everything made for `owner`
    end,
    state = function(self, owner)
        -- the answer `apply` would give now, without applying
        local on = Apply.enabled(self, owner)
        return { status = if on then "inForce" else "off" }
    end,
}
return M

A type states settings: in its type.yaml so its parameters are readable with asset.schema; an empty schema is valid, and such an asset is applied with no values. A type whose own apply means something else meets the member under M.interfaces.applicable.apply.

Required behaviour

  1. In effect on return. When apply returns inForce, the asset's effect is in effect with those values.
  2. A change is a write. After apply(owner, changes), the effect is what a first apply with the complete changed values would have given.
  3. Edit and play. In effect in edit mode and in play mode.
  4. Off keeps values. setEnabled(false) takes the effect away; setEnabled(true) brings it back with the same values.
  5. Drop leaves nothing. After drop, what the application made is gone: the engine's entities, render features, post-process entries and claims are as before the first apply.
  6. Nothing saved. Nothing an application holds reaches a saved scene.
  7. Independent. Two owners' applications of one asset are two: one changing, turning off or dropping never changes the other, except through the asset's own rule for which application is in force.
  8. One in force is the asset's rule. An asset whose effect holds one application at a time decides among its applications and reports the others held.
  9. Kept in force. What an application holds is put back after the events that remove it, by the contract's restatement.

The conformance suite (conformance) checks these against every asset of every implementing type it can reach.