FactorableSpawner

Serializable spawner that wraps a factory reference with variant selection and data override support. Designed for Inspector-driven configuration — select a factory, choose a variant, add field overrides, and call Spawn() to get a fully configured object.

Definition

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

[Serializable]
public sealed class FactorableSpawner<TObject, TData>
    where TObject : class, IFactorable<TData>
    where TData : FactorableData

Remarks

FactorableSpawner is the primary way game code configures and triggers object spawning from the Inspector. It holds three pieces of state:

  1. Factory reference — which IScriptableFactory<TObject, TData> to spawn from.

  2. Variant index — which variant of the factory's data to use (0 = source data).

  3. Data override — per-spawner field-level overrides applied on top of the selected variant.

On the first call to Spawn(), the spawner "bakes" its data by calling Factory.GetData(variantIndex, dataOverride), which combines the variant data with the override. The baked TData is cached and reused for subsequent spawns until the factory, variant, or bake state changes.

Quick Lookup

Goal
How

Assign a factory

spawner.SetFactory(factory)

Read the factory

spawner.GetFactory()

Select a variant

spawner.SetVariant(index)

Spawn an object

spawner.Spawn()

Pre-bake data

spawner.BakeData()

Invalidate cached data

spawner.ClearBake()

Properties

VariantIndex

The currently selected variant index.

Constructor

Creates a spawner with a new DataOverride initialized for typeof(TData), variant index 0, and no cached spawn data.

Methods

SetFactory

Assigns the factory this spawner will use. Clears all overrides and cached data when the factory changes.

Parameter
Type
Description

scriptableFactory

IScriptableFactory<TObject, TData>

The factory to spawn from

circle-exclamation
circle-info

If the same factory is assigned again, the method returns early without clearing state.

GetFactory

Returns the currently assigned factory.

SetVariant

Selects which data variant to use. Clamped to [0, Factory.VariantCount]. Invalidates cached spawn data.

Parameter
Type
Description

index

int

Variant index (0 = source data)

Spawn

Spawns an object using the configured factory, variant, and overrides. Auto-bakes data on first call if not already baked.

Returns: A new TObject instance spawned with the baked data.

BakeData

Pre-computes the spawn data by calling Factory.GetData(variantIndex, dataOverride). The result is cached for subsequent Spawn() calls.

ClearBake

Invalidates the cached spawn data. The next Spawn() call will re-bake.

Usage Example

Common Pitfalls

circle-exclamation
circle-exclamation
circle-exclamation

See Also

  • DataOverride — field-level override system used by this spawner

  • IScriptableFactory<TObject, TData> — factory interface this spawner delegates to

  • DataVariantSystem — variant management within factories

Last updated