ScriptableFactoryDatabase

Generic singleton ScriptableObject that provides hash-based lookup for factories of a specific type. Subclass this to create game-specific databases that allow retrieving individual factory instances by their hash code.

Definition

Namespace: Paragon.Core.ScriptableFactory Assembly: Paragon.dll

public class ScriptableFactoryDatabase<TDatabase, TFactory> : SingletonScriptableObject<TDatabase>
    where TDatabase : ScriptableFactoryDatabase<TDatabase, TFactory>
    where TFactory : IScriptableFactory

Inheritance: ParagonScriptableObjectSingletonScriptableObject<TDatabase>ScriptableFactoryDatabase<TDatabase, TFactory>

Remarks

On OnEnable(), the database queries the ScriptableFactoryRegistry for all factories of type TFactory and indexes them into a Dictionary<int, TFactory> keyed by GetHashCode(). This enables O(1) lookup by hash at runtime.

This class uses the curiously recurring template pattern (CRTP) — TDatabase must be the subclass itself — to enable the SingletonScriptableObject pattern to work with the concrete type.

Quick Lookup

Goal
How

Retrieve a factory by hash

MyDatabase.GetFactory(hashCode)

Create a custom database

Subclass with concrete types (see example)

Properties

No public properties. The internal factories dictionary is displayed read-only in the Inspector via [ReadOnly, ShowInInspector].

Methods

GetFactory

Retrieves a factory by its hash code.

Parameter
Type
Description

index

int

The hash code of the factory (from GetHashCode())

Returns: The factory instance matching the hash.

circle-exclamation

Extension Points

Optional Overrides

Method
Purpose

OnEnable()

Populates the factory dictionary. Override to add custom initialization logic (call base.OnEnable() first).

Implementation Requirements

When subclassing ScriptableFactoryDatabase, you MUST:

  1. Provide the concrete database class as TDatabase (CRTP pattern)

  2. Provide the factory interface/type as TFactory

  3. Apply [SingletonSettings(AssetPath = "...")] to specify the Resources path

Examples

Creating a Game-Specific Database

Using the Database

See Also

Last updated