Notifications

The notification system provides toast-style notifications with support for multiple types, positions, and animations. Notifications are non-blocking and automatically dismiss after a specified durati

Transparent (Glass) Notifications

Notifications Module

The Notifications module provides a comprehensive toast-style notification system with support for multiple types, positions, animations, and advanced queue management. Notifications are non-blocking, automatically dismiss after a specified duration, and support both client-side and server-side triggering.

Features

  • Multiple Notification Types: Success, error, warning, info with distinct styling and icons

  • Flexible Positioning: 9-position grid system for precise screen placement

  • Auto-dismiss System: Configurable duration with automatic cleanup and queue management

  • Queue Management: Multiple notifications with smart positioning and overflow handling

  • Theme Support: Adapts to dark/light themes with customizable styling

  • Icon Support: Optional type-specific icons for visual clarity

  • Server Integration: Server-side functions to notify specific players or all players

  • State Management: Track active notifications and clear them programmatically

  • Performance Optimized: Efficient rendering and memory management

Available Functions

notify

Display a notification message with customizable options and styling.

Parameters:

  • playerId (number, server-side only): Player server ID to send the notification to

  • options (table): Notification configuration options

    • message (string): The notification message text

    • type (string, optional): Notification type - 'success', 'error', 'warning', 'info' (default: 'info')

    • title (string, optional): Notification title displayed above message

    • duration (number, optional): Display duration in milliseconds (default: from config)

    • position (string, optional): Position on screen (default: from config)

    • showIcon (boolean, optional): Whether to show type-specific icon (default: from config)

    • playSound (boolean, optional): Whether to play notification sound (default: from config)

    • backgroundColor (string, optional): Custom background color override

    • textColor (string, optional): Custom text color override

    • borderColor (string, optional): Custom border color override

Returns: number|boolean - Notification ID on success, false on failure

Example:

clearNotifications

Clear all active notifications immediately with optional fade animation.

Returns: boolean - Success status

Example:

getNotifications

Get an array of all currently active notifications with their properties.

Returns: table - Array of active notification objects

  • id (number): Unique notification identifier

  • type (string): Notification type

  • title (string): Notification title

  • message (string): Notification message

  • duration (number): Display duration

  • position (string): Screen position

  • timestamp (number): Creation timestamp

  • showIcon (boolean): Icon visibility state

Example:

notifyAll (Server-side only)

Send a notification to all online players simultaneously.

Parameters:

  • options (table): Notification configuration options (same as notify function)

Returns: boolean - Success status

Example:

notifyPlayer (Server-side only)

Convenience function to send a notification to a specific player with simplified parameters.

Parameters:

  • playerId (number): Player server ID

  • message (string|table): Message text or full options table

  • type (string, optional): Notification type (default: 'info')

  • title (string, optional): Notification title

Returns: boolean - Success status

Example:

clearNotificationsPlayer (Server-side only)

Clear all notifications for a specific player from server-side.

Parameters:

  • playerId (number): Player server ID

Returns: boolean - Success status

Example:

Notification Types

Success Notifications

Used for positive feedback and successful operations.

Error Notifications

Used for error messages and failed operations.

Warning Notifications

Used for warnings and cautionary messages.

Info Notifications

Used for general information and neutral messages.

Positioning System

Notifications support a comprehensive 9-position grid system for precise placement:

Corner Positions:

  • 'top-left' - Upper left corner

  • 'top-right' - Upper right corner

  • 'bottom-left' - Lower left corner

  • 'bottom-right' - Lower right corner

Center Positions:

  • 'center' - Screen center

  • 'top-center' - Top center

  • 'bottom-center' - Bottom center

Side Positions:

  • 'center-left' - Left side center

  • 'center-right' - Right side center

Server-Side Usage

Notify Specific Player

Notify All Players

Conditional Server Notifications

Bulk Notifications

Configuration

Configure notification defaults in config.lua:

Advanced Examples

Conditional Notifications with Health Monitoring

Progress Notifications with Chaining

Notification with State Tracking and Management

Server-Side Event-Driven Notifications

Custom Styled Notifications

Events

The notification system triggers the following events:

Client-Side Events

  • b2lib:notification:shown - Triggered when a notification is displayed

  • b2lib:notification:hidden - Triggered when a notification is dismissed

  • b2lib:notification:clicked - Triggered when a notification is clicked (if enabled)

Server-Side Events

  • b2lib:server:notify - Internal event for server-to-client notifications

  • b2lib:server:notifyAll - Internal event for broadcasting notifications

Best Practices

  1. Use Appropriate Types - Match notification type to the message context for better user experience

  2. Keep Messages Concise - Short, clear messages are more effective and readable

  3. Consider Duration - Important messages should have longer durations, routine messages shorter

  4. Avoid Notification Spam - Don't overwhelm users with too many notifications at once

  5. Use Positioning Wisely - Place notifications where they won't obstruct important gameplay elements

  6. Provide Context with Titles - Use titles to give immediate context to the message content

  7. Test with Different Themes - Ensure notifications are readable in both dark and light themes

  8. Handle Edge Cases - Always validate input parameters and handle failures gracefully

  9. Use Server-Side for Important Messages - Critical notifications should come from server to ensure delivery

  10. Implement Cleanup - Clear notifications when appropriate to prevent UI clutter

Troubleshooting

Notifications Not Showing

  1. Check Message Parameter: Ensure the message parameter is provided and not empty

  2. Verify B2Lib Initialization: Confirm B2Lib is properly initialized before calling functions

  3. Validate Position: Ensure the position value is valid (check Config.ValidPositions)

  4. Check Console Errors: Look for error messages from B2Lib.Debug in the console

  5. Test with Simple Example: Try a basic notification without optional parameters

Notifications Overlapping or Positioning Issues

  1. Use Different Positions: Assign different positions for concurrent notifications

  2. Adjust maxVisible Config: Modify the maxVisible value in Config.Notifications

  3. Clear Old Notifications: Use clearNotifications() before showing new important ones

  4. Check Spacing Configuration: Verify spacing settings in notification config

Performance Issues

  1. Limit Notification Frequency: Avoid showing too many notifications rapidly

  2. Use Reasonable Durations: Set appropriate durations to prevent buildup

  3. Clear When Appropriate: Clear notifications when no longer needed

  4. Monitor Active Count: Use getNotifications() to monitor active notification count

  5. Implement Queuing: Use the built-in queue system for high-frequency notifications

Server-Side Issues

  1. Check Player Validity: Ensure player ID is valid before sending notifications

  2. Handle Disconnections: Account for players disconnecting during notification sending

  3. Validate Permissions: Check player permissions before sending sensitive notifications

  4. Test Event Triggering: Verify server events are properly registered and triggered

Theme and Styling Issues

  1. Test Both Themes: Verify notifications work in both dark and light themes

  2. Check Custom Colors: Ensure custom colors have sufficient contrast

  3. Validate CSS: Check that custom styling doesn't break the notification layout

  4. Test on Different Resolutions: Verify positioning works on various screen sizes


Last updated