ItemFactory

Abstract ScriptableObject factory that spawns Item instances with networked prefab instantiation, geometry attachment, and outline baking. Subclass to create concrete item factories for specific item types.

Definition

Namespace: Paragon.Townskeep.ItemSystem Assembly: Townskeep.dll

public abstract class ItemFactory<TItem, TData> : NetworkScriptableFactory<TItem, TData>, IItemFactory<TItem, TData>
    where TItem : Item, IFactorable<TData>
    where TData : ItemData

Inheritance: ScriptableObject → ScriptableFactory<TItem, TData> → NetworkScriptableFactory<TItem, TData> → ItemFactory<TItem, TData> Implements: IItemFactory<TItem, TData>, INetworkScriptableFactory<TItem, TData>

Remarks

ItemFactory adds two item-specific behaviors on top of the network factory pipeline:

  1. Geometry setup — A serialized geometry GameObject is baked for smooth normals at initialization and instantiated as a child of each spawned item with the name "Geometry".

  2. Outline baking — After geometry is attached, the item's Outline component is baked for rendering.

The factory's GetHashCode() returns ItemData.ItemID, which is used by ItemDatabase as the dictionary key for factory lookups.

circle-info

Each ItemFactory ScriptableObject asset represents one item type. Variants are handled by the Scriptable Factory variant system — the base ItemData fields marked [Overridable] can be customized per variant.

Quick Lookup

Goal
How

Create a concrete factory

Subclass ItemFactory<MyItem, MyData>, create as ScriptableObject

Spawn locally

factory.Spawn() (inherited)

Spawn networked

factory.NetworkSpawn() (inherited)

Spawn variant

factory.NetworkSpawn(variantId) (inherited)

Get item data

factory.GetData() (inherited)

Hash code = item ID

factory.GetHashCode() returns ItemData.ItemID

Fields

geometry (private, serialized)

The 3D model prefab instantiated as a child of each spawned item.

circle-info

Smooth normals are baked on this geometry during Initialize() via SmoothNormals.Bake(). This is done once per factory, not per spawn.

Methods

Initialize (override)

Calls base.Initialize() (registers network prefab) then bakes smooth normals on the geometry.

InstantiateObject (override)

Creates an item instance via the base prefab handler, then instantiates the geometry as a child and bakes the outline.

Pipeline:

  1. base.InstantiateObject() — instantiates the network prefab

  2. Instantiates geometry as a child with name "Geometry"

  3. Calls item.GetComponent<Outline>().Bake() for outline rendering

Returns: The fully configured TItem instance.

GetHashCode (override)

Returns ItemData.ItemID, used by ItemDatabase for indexing.

Returns: The ItemID field from the factory's ItemData.

Extension Points

Required Overrides

None — ItemFactory is abstract via its generic parameters, but all methods have implementations. Concrete subclasses only need to exist as ScriptableObject assets.

Optional Overrides

Method
Purpose

Initialize()

Additional factory-level initialization (call base.Initialize() first)

InstantiateObject()

Additional per-spawn setup (call base.InstantiateObject() first)

Implementation Requirements

When subclassing ItemFactory, you MUST:

  1. Define concrete TItem and TData type parameters (e.g., ItemFactory<Weapon, WeaponData>)

  2. Ensure TItem extends Item and implements IFactorable<TData>

  3. Ensure TData extends ItemData

  4. Create the factory as a ScriptableObject asset in the project

You SHOULD:

  • Assign the geometry field in the Inspector to the item's 3D model prefab

  • Ensure the item prefab has an Outline component (required by InstantiateObject())

  • Set ItemData.ItemID to a unique value for ItemDatabase indexing

Common Pitfalls

circle-exclamation
circle-exclamation
circle-exclamation

Examples

Minimal Concrete Factory

Custom Factory with Extra Init

See Also

  • IItemFactory — interface this class implements

  • ItemDatabase — registry that indexes factories

  • ItemData — data record defining item properties

  • Item — runtime MonoBehaviour spawned by this factory

Last updated