Itemweigher
Overview
The ItemWeigher component enables an entity (typically a scale in the game, such as the Trophy Scale) to associate a trophy scale type (e.g., "small", "medium", "large") via dynamic tagging, and to support custom weighing behavior via a user-defined function. It is used primarily by scale entities to determine what trophy tier an item belongs to upon being weighed.
Dependencies & Tags
- Tags managed internally: Adds or removes
"trophyscale_<type>"tags on the entity whentypeis set or changed (e.g.,"trophyscale_large"). - No external component dependencies are declared or implied in this script.
Properties
| Property | Type | Default Value | Description |
|---|---|---|---|
inst | Entity | (constructor arg) | Reference to the entity the component is attached to. |
type | string? | nil | The current trophy scale type; nil means no type assigned (no tag applied). |
ondoweighinfn | function? | nil | Optional callback function invoked when DoWeighIn() is called; receives (scale_inst, target_inst, doer_inst) as arguments. |
Note: The
typeproperty is not stored as a direct member onselfin_ctor, but the class definestype = ontypein the metatable, which redirects assignments to theondypefunction. This makesitemweigher.type = "large"trigger theontypefunction, which handles tagging.
Main Functions
SetOnDoWeighInFn(fn)
- Description: Sets the callback function that will be executed when
DoWeighIn()is invoked. This allows external code (e.g., the scale’s interaction logic) to define custom weighing behavior. - Parameters:
fn(function) — A callable function expecting three arguments:(scale_inst, target_inst, doer_inst), where:scale_instis the scale entity,target_instis the item being weighed,doer_instis the player or entity performing the weigh-in.
DoWeighIn(target, doer)
- Description: Executes the stored weighing callback function, if one is set. Returns the result of the callback or
nilif no callback is assigned. - Parameters:
target(Entity) — The item or entity being weighed.doer(Entity) — The entity performing the weigh-in action (typically a player).
Events & Listeners
- Listens for internal property assignment via the
typesetter, which triggersontype(self, type, old_type). - When
typeis set:- If
old_typeis non-nil, removes tag"trophyscale_"..old_type. - If
new typeis non-nil, adds tag"trophyscale_"..type.
- If
- No external event listeners (e.g.,
ListenForEvent) are registered in this component. - The
OnRemoveFromEntity()hook setstype = nilon removal to clear any associated tag.