Carnivaldecorranker
Overview
The Carnivaldecorranker component is attached to an entity to measure the decorative appeal of its surroundings. Upon initialization, it scans a radius for entities with the carnivaldecor tag, summing their decor values to calculate a total score. This score is then converted into a numerical rank. The component dynamically updates this rank when decorations are added or removed from its tracking list, and can execute a callback function whenever the rank changes.
Dependencies & Tags
Dependencies
- This component interacts with entities that have the
carnivaldecorcomponent to retrieve their decor value.
Tags
- Adds the
carnivaldecor_rankertag to the entity.
Properties
| Property | Type | Default Value | Description |
|---|---|---|---|
decor | table | {} | A table mapping nearby decor entities to their numerical decor value. |
totalvalue | number | 0 | The sum of all decor values from the decor table. |
rank | number | 0 | The calculated decor rank based on totalvalue. |
onrankchanged | function | nil | A callback function triggered when the rank changes. It receives the entity instance, new rank, previous rank, and a 'snap' boolean as arguments. |
Main Functions
UpdateDecorValue(snap)
- Description: Recalculates the total decor value by summing all values in the
decortable. It then calculates a new rank based on this total. If the new rank is different from the current rank, it updates therankproperty and triggers theonrankchangedcallback if it has been assigned. - Parameters:
snap(boolean): An optional boolean that is passed directly to theonrankchangedcallback.
AddDecor(decor)
- Description: Adds a new decor entity to the internal tracking list and updates its value. After adding, it calls
UpdateDecorValueto recalculate the total score and rank. - Parameters:
decor(Entity): The decor entity to add. It must have acarnivaldecorcomponent.
RemoveDecor(decor)
- Description: Removes a decor entity from the internal tracking list. After removal, it calls
UpdateDecorValueto recalculate the total score and rank. - Parameters:
decor(Entity): The decor entity to remove.
GetDebugString()
- Description: Returns a formatted string containing the current number of tracked decor items, the total decor value, and the current rank. Useful for debugging.
- Parameters: None.