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 : FactorableDataRemarks
FactorableSpawner is the primary way game code configures and triggers object spawning from the Inspector. It holds three pieces of state:
Factory reference — which
IScriptableFactory<TObject, TData>to spawn from.Variant index — which variant of the factory's data to use (0 = source data).
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
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.
scriptableFactory
IScriptableFactory<TObject, TData>
The factory to spawn from
Clears state on change
When the factory changes, SetFactory() clears the data override, resets the variant index to 0, and nullifies cached spawn data. This prevents stale data from a previous factory being used.
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.
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
SetFactory clears overrides
Changing the factory clears the DataOverride and resets the variant index. If you need to preserve overrides, configure them after calling SetFactory().
Stale bake after override changes
Modifying the DataOverride fields does not automatically invalidate the baked data. Call ClearBake() after changing overrides to ensure the next Spawn() uses updated data.
SetVariant clamps silently
The variant index is clamped to [0, VariantCount] via Mathf.Clamp. Out-of-range indices do not throw — they are silently clamped.
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