Itemaffinity
Overview
The ItemAffinity component enables a character to gain a temporary sanity bonus when carrying specific items or items with certain tags. Only the highest-priority matching item’s bonus is applied at any given time—no stacking occurs. The component automatically refreshes the active bonus whenever items are added to or removed from the character’s inventory.
Dependencies & Tags
Component Dependencies:
inst.components.inventory— used to check for item presence.inst.components.sanity.externalmodifiers— used to apply and remove sanity bonuses.
Tags:
None added or removed.
Properties
| Property | Type | Default Value | Description |
|---|---|---|---|
affinities | table | {} | A list of affinity records; each record defines a mapping (prefab or tag), a sanity_bonus, and a priority used to resolve conflicts. |
Main Functions
AddAffinity(prefab, tag, sanity_bonus, priority)
- Description: Registers a new item affinity rule. If
prefabis provided, the bonus applies when that specific item is held; iftagis provided, the bonus applies to any item matching the tag. Higherpriorityvalues override lower ones. The active sanity bonus is recalculated immediately. - Parameters:
prefab: Optional string — the prefab name of the item to check for.tag: Optional string — the tag to match against inventory items (used ifprefabis nil).sanity_bonus: number — the sanity value to apply while the item is carried.priority: number — relative priority of this affinity; higher values take precedence.
RemoveAffinity(prefab)
- Description: Removes the affinity rule associated with a specific
prefab. The active sanity bonus is recalculated and applied if another affinity matches. - Parameters:
prefab: string — the prefab name whose affinity should be removed.
RefreshAffinity()
- Description: Re-evaluates all registered affinities in priority order and applies the bonus from the first matching item (by
prefabortag) currently in the character’s inventory. Removes any previously applied modifier before reapplying. - Parameters: None.
SortAffinities()
- Description: Sorts the
affinitieslist in descending order ofpriority(highest first). Called internally duringRefreshAffinity().
Events & Listeners
- Listens for:
"itemget"— triggersRefreshAffinity()when an item is picked up."itemlose"— triggersRefreshAffinity()when an item is lost (e.g., dropped or consumed)."dropitem"— triggersRefreshAffinity()when an item is explicitly dropped.