Skip to main content

Toggleableitem

Overview

This component implements a simple on/off toggle state for an entity—typically used for handheld items like lanterns or tools—and optionally invokes a user-defined callback function when the state changes.

Dependencies & Tags

None identified.

Properties

PropertyTypeDefault ValueDescription
instEntitynilReference to the entity the component is attached to.
onusefnfunction?nilOptional callback function invoked when the item is toggled; signature: function(inst, is_on).
onstopusefnfunction?nilReserved placeholder; not initialized or used in the current implementation.
onbooleanfalseCurrent toggle state (true = on, false = off).
stopuseeventstable?nilReserved placeholder; not initialized or used in the current implementation.

Main Functions

SetOnToggleFn(fn)

  • Description: Assigns a callback function to be executed whenever ToggleItem() is called. The callback receives the entity instance and the new toggle state.
  • Parameters:
    • fn (function?): A function to call on toggle, with signature function(inst, is_on).

CanInteract(doer)

  • Description: Determines whether the given entity (doer) can interact with this item. Currently always returns true, indicating unrestricted access.
  • Parameters:
    • doer (Entity): The entity attempting interaction.

ToggleItem()

  • Description: Inverts the current toggle state (on), and if onusefn is set, invokes it with the entity and new state as arguments.
  • Parameters: None.

Events & Listeners

None.