Sprite Components Guide
This guide explains how to use sprite components to add visual elements behind or in front of sprites in RPGJS.Overview
RPGJS allows you to attach custom components to sprites that render either behind or in front of the main sprite graphics. This is useful for adding visual effects like shadows, health bars, status indicators, auras, or other UI elements that should be positioned relative to sprites.Component Properties
componentsBehind
Components in this array render behind the sprite with a lower z-index. Perfect for:
- Shadow effects
- Aura or glow effects
- Ground-based visual elements
- Background decorations
componentsInFront
Components in this array render in front of the sprite with a higher z-index. Perfect for:
- Health bars
- Status effect indicators
- Damage numbers
- Name tags
- UI overlays
Creating Sprite Components
Sprite components are Canvas Engine components (.ce files) that receive the sprite object as a prop.
Basic Component Structure
Health Bar Component Example
Configuration Methods
Method 1: Module Configuration
Configure components globally for all sprites in your module:Method 2: Engine Methods
Add components dynamically using the engine:Advanced Component Configuration
Sprite components support advanced configuration options that allow you to:- Pass static or dynamic props to components
- Control when components are displayed using dependencies
- Wait for data to be ready before rendering
Component Configuration Object
Instead of passing a component directly, you can use a configuration object with three optional properties:Props Configuration
Theprops property can be either a static object or a function that receives the sprite object and returns props dynamically.
Static Props
Pass a static object that will be used for all sprites:Dynamic Props (Function)
Use a function to compute props based on the sprite’s state. The function receives the sprite object as a parameter:- The function is called for each sprite instance
- You can access sprite signals reactively (e.g.,
object.hp(),object.param.level()) - Props are recalculated when sprite state changes
- The returned object is passed directly to the component as props
Dependencies Configuration
Thedependencies property allows you to control when a component should be displayed. This is useful for waiting until certain data is available before rendering the component.
How Dependencies Work
- The
dependenciesfunction receives the sprite object and returns an array of signals or values - The component only renders when all dependencies are resolved (not
undefined) - This prevents components from displaying with incomplete or missing data
- Dependencies are checked reactively, so the component will appear/disappear as values change
Basic Dependencies Example
Wait for specific sprite properties to be available:Custom Dependencies with Signals
You can create custom signals to control component visibility:Multiple Dependencies
You can wait for multiple conditions:Prebuilt Components
RPGJS includes prebuilt sprite components that you can use directly in your game without creating them from scratch. These components are ready to use and highly customizable.Available Prebuilt Components
- Light Halo - An animated light effect that pulses and glows around sprites, perfect for atmospheric lighting, magical auras, or ambient glow effects.