Skip to main content
The proxy pattern is the core architectural pattern of Mythic Framework. It enables modular, extensible development through a component registration and dependency injection system. Understanding this pattern is essential for working with Mythic.
Reference Implementation: MythicFramework/resources/[mythic]/mythic-base/core/sh_proxy.luaThis file contains the complete proxy system implementation.

What is the Proxy Pattern?

The proxy pattern in Mythic Framework provides a centralized component registry that:
  • Registers components from any resource
  • Fetches components for use by other resources
  • Extends existing components with new functionality
  • Resolves dependencies automatically
Think of it as a service container or dependency injection container for FiveM resources.

The COMPONENTS Global Table

At the heart of the proxy system is the global COMPONENTS table:
Every registered component becomes available in this global table.

The Four Core Exports

The proxy system provides four essential exports:

1. RegisterComponent

Purpose: Register a new component or override an existing one Location: mythic-base/core/sh_proxy.lua:32
Parameters:
  • component_name (string) - Unique name for the component
  • component_data (table) - The component object with methods and data
Example:
Protected Components: Components can be marked as _protected = true to prevent other resources from overriding them:
Required Attributes: Components can specify _required to enforce that certain methods must exist:

2. FetchComponent

Purpose: Retrieve a component for use Location: mythic-base/core/sh_proxy.lua:67
Parameters:
  • component_name (string) - Name of the component to fetch
Returns:
  • Component object or nil if not found
Example:
Best Practice:
Always check if component exists before using! A component may not be loaded yet, or the resource providing it may not be started.

3. ExtendComponent

Purpose: Add new functionality to an existing component without modifying it Location: mythic-base/core/sh_proxy.lua:76
Parameters:
  • component_name (string) - Name of component to extend
  • extension_data (table) - Methods/data to add
Example:
Use Cases:
  1. Adding helper methods:
  2. Adding configuration:
  3. Monkey-patching:
ExtendComponent cannot modify protected components. It will fail with a warning.

4. RequestDependencies

Purpose: Asynchronously wait for dependencies to load before initializing Location: mythic-base/core/sh_proxy.lua:96
Parameters:
  • component_name (string) - Name of your component (for logging)
  • dependencies (table) - Array of component names to wait for
  • callback (function) - Called when dependencies are ready (or failed)
Example:
How it Works:
  1. Polls for dependencies every 100ms
  2. Times out after 50 attempts (~5 seconds)
  3. Calls callback with any errors
  4. Tracks which components depend on others for updates
Advanced Usage:

Component Lifecycle

Understanding when components are registered and available:

Real-World Examples

Example 1: Simple Utility Component

Example 2: Complex Feature Component

Example 3: Extending Core Component

Dependency Updates

When a component is extended or re-registered, all resources that depend on it are notified:

Best Practices

Never assume components are available immediately:
Don’t fetch on every use:
Prevent accidental overrides:
Use PascalCase for component names:
Add comments explaining methods:

Common Patterns

Singleton Pattern

Factory Pattern

Next Steps

Component System

Learn more about component-based architecture

Resource Structure

How to structure resources using components

Event System

Complement components with events

API Reference

Complete API documentation
Master the proxy pattern and you’ve mastered Mythic Framework. This is the foundation everything else is built on.