Skip to main content

Klaussacklock

Overview

This component serves as a locking mechanism for entities (typically containers or doors), allowing interaction with keys. It stores a callback function (onusekeyfn) that defines how a key should be processed when used on the entity. It also automatically adds and removes the "klaussacklock" tag on the entity to indicate its purpose.

Dependencies & Tags

  • Tags added: "klaussacklock" (added in constructor, removed on entity removal)
  • Component dependencies: Relies on key.components.stackable being present if key is stackable and consumed; otherwise calls key:Remove() directly.

Properties

PropertyTypeDefault ValueDescription
instEntitynil (set via constructor)Reference to the owning entity instance.
onusekeyfnfunction?nilCallback function to invoke on key use; signature: fn(inst, key, doer) -> success: boolean, fail_msg: string?, consumed: boolean.

Main Functions

SetOnUseKey(onusekeyfn)

  • Description: Assigns the callback function that defines the logic for using a key on this entity.
  • Parameters:
    onusekeyfn (function): A function with signature (entity, key, doer) -> (success: boolean, fail_msg: string?, consumed: boolean).

UseKey(key, doer)

  • Description: Executes the key-usage logic by invoking onusekeyfn. Handles key consumption (removing the key from inventory or destroying it) if the callback signals consumption. Returns success/failure state and optional failure message.
  • Parameters:
    key (Entity?): The key entity being used. Must be valid.
    doer (Entity): The entity (typically a player) performing the key use action.
    Returns:
    • true if the operation succeeded and key was consumed.
    • false, fail_msg if the operation failed (e.g., incorrect key, condition not met), where fail_msg is a message to display.

Events & Listeners

None identified.