Playingcardsmanager
Overview
This component serves as the central manager for playing card resources in the game. It maintains a global pool of unique card IDs (representing combinations of suits and pips), assigns them to newly created cards or decks, and handles their reuse when entities are removed. It operates exclusively on the master simulation server and persists ID state across saves.
Dependencies & Tags
- Requires the entity to be the master (
TheWorld.ismastersim); throws an assertion if instantiated on a client. - Adds
"playingcardsmanager"component to the entity (implicitly viainst:AddComponent("playingcardsmanager")). - Listens to
"onremove"events on cards and decks it manages. - Does not explicitly add or remove tags.
Properties
No public properties are exposed as self.property. All internal state is held in local closures (available_card_ids, decks). The only public attribute is self.inst, inherited from the component constructor.
Main Functions
MakeDeck(size)
- Description: Spawns a new
deck_of_cardsentity and populates it withsizeuniquely assigned card IDs. Uses IDs from the available pool if possible; otherwise generates random IDs as fallback (typically only during world-gen or emergency reuse). - Parameters:
size(number): Number of cards to include in the deck. Defaults to1if omitted or non-positive.
MakePlayingCard(id, facedown)
- Description: Spawns a new
playing_cardentity, assigns it an ID (either explicitly provided or drawn from the pool), and optionally sets it face-down. Registers it for automatic ID return on removal. - Parameters:
id(number, optional): Specific card ID to assign. If omitted, draws from the available pool.facedown(boolean, optional): Iftrue, sets the card as face-down.
RegisterPlayingCard(card)
- Description: Assigns a unique card ID to an already existing playing card entity (e.g., loaded from save or spawned externally) and registers it for lifecycle management.
- Parameters:
card(Entity): A valid playing card entity with aplayingcardcomponent.
OnSave()
- Description: Serializes the current state of available card IDs. Optionally includes GUIDs of active decks if any exist, for post-load restoration.
- Returns:
data(table) — containingavailable_ids, and optionallyliving_decks(array of GUIDs). May also returndata, entsif decks are present.
OnLoad(data)
- Description: Restores the
available_card_idspool from saved data during load. - Parameters:
data(table): Saved state fromOnSave.
LoadPostPass(newents, data)
- Description: Re-registers deck entities after they are instantiated from save data, re-attaching their
"onremove"listeners. - Parameters:
newents(table): Map of GUID → entity reference for newly loaded entities.data(table): Containsliving_decksarray of GUIDs.
GetDebugString()
- Description: Returns a string listing all currently available card IDs for debugging.
- Returns:
s(string) — Human-readable list of IDs.
Debug_ResetAvailableIDs()
- Description: Reinitializes the
available_card_idspool using currentTUNING.PLAYINGCARDS_NUM_SUITSandTUNING.PLAYINGCARDS_NUM_PIPSconstants. Useful for development/testing without world restart. - Note: Uses
NUM_SUITSandNUM_PIPS, likely defined constants in scope (possibly aliases forTUNING.PLAYINGCARDS_NUM_*).
Events & Listeners
- Listens for
"onremove"on decks — to return all contained card IDs to the available pool viadeck_onremove. - Listens for
"onremove"on individual cards — to return the card’s ID to the available pool viacard_onremove. - Listens for
"onremove"on decks duringLoadPostPass— to ensure post-load cleanup is handled. - Does not push or emit any events itself.