Despawnfader
Overview
This component is responsible for gracefully removing an entity from the game by visually fading it out over time. It handles both the animation of the fade and the eventual destruction of the entity, synchronizing the fade state between the server (master sim) and clients.
Dependencies & Tags
- Dependencies: While not explicitly added as components, this script interacts with
inst.AnimStatefor visual effects and relies on the networking capabilities provided bynet_tinybytefor synchronization. - Tags:
- Adds
NOCLICKwhen fading out to prevent interaction with the entity during its removal process.
- Adds
Properties
| Property | Type | Default Value | Description |
|---|---|---|---|
_fade | net_tinybyte | nil | A networked net_tinybyte variable used to synchronize the fade value between server and clients. |
fadeval | number | 0 | The current normalized fade progress, ranging from 0 (fully transparent/removed) to 1 (fully opaque). |
updating | boolean | false | A flag indicating whether the component is actively running its OnUpdate loop. |
Main Functions
_ctor(self, inst)
- Description: The constructor initializes the component, associating it with the entity instance, setting up the networked fade variable, and initializing internal state flags. On clients, it immediately registers an event listener for
fadedirtyto react to server-side fade changes. - Parameters:
self: TheDespawnFadercomponent instance.inst: The entity instance this component is attached to.
OnFadeDirty(inst)
- Description: This function is called on client machines when the
_fadenetworked variable changes (becomes "dirty"). It triggers the client-side fade process to match the server's state, recalculatingfadevaland starting the component's update loop if not already running. - Parameters:
inst: The entity instance associated with the event.
OnRemoveFromEntity()
- Description: Performs cleanup operations when the component is removed from its entity. Specifically, on client machines, it removes the event callback for
fadedirtyto prevent memory leaks or errors. - Parameters: None.
FadeOut()
- Description: Initiates the fade-out process for the entity. It sets the
fadevalto 1, ensuring the entity is initially opaque, starts the component's update loop if not already active, adds theNOCLICKtag to prevent interaction, and sets the entity'spersistsproperty tofalseto prevent it from being saved. It then immediately callsOnUpdateto start the process. - Parameters: None.
OnUpdate(dt)
- Description: The core update logic for the despawn fader, called periodically. It decrements
fadevalover time, calculates a non-linear transparency value (k), and applies it to the entity'sAnimState:OverrideMultColour. Whenfadevalreaches 0, the entity is either removed on the master sim or the component stops updating on clients. On the master sim, it also synchronizesfadevalwith clients via the_fadenetworked variable. - Parameters:
dt: The time elapsed since the last update, in seconds.
Events & Listeners
- Listens For:
"fadedirty": Listened to on client instances of the entity to synchronize the fade state with the server.