Wanderingtraderspawner
Overview
This component is responsible for spawning and managing a single Wandering Trader instance in the game world. It registers and maintains lists of spawn points (separating master Sim and non-master Sim points), handles spawner logic on world initialization, tracks the spawned trader, and supports save/load persistence. It only runs on the master simulation.
Dependencies & Tags
- Component Dependencies: Requires
Transformcomponent onself.inst(used implicitly viaspawnpoint.Transformandself.inst). - Tags Used/Added: No explicit tags are added or removed by this component.
Properties
| Property | Type | Default Value | Description |
|---|---|---|---|
spawnpoints | table | {} | List of non-master spawn point entities (e.g., from client or non-Sim-side registrations). |
spawnpoints_masters | table | {} | List of master Sim spawn point entities (used for authoritative spawning). |
wanderingtrader | Entity? | nil | Reference to the currently spawned Wandering Trader prefab instance. |
OnRemove_wanderingtrader | function | — | Event handler that clears self.wanderingtrader when the trader is removed. |
Note: The constructor (_ctor) is implemented as an anonymous function in the return statement. No public properties are declared before initialization.
Main Functions
TrackWanderingTrader(wanderingtrader)
- Description: Stores a reference to the given Wandering Trader entity and attaches an
"onremove"event listener to clear the reference when the trader is removed. - Parameters:
wanderingtrader(Entity): The Wandering Trader entity instance to track.
SpawnWanderingTrader()
- Description: Instantiates and returns a new
wanderingtraderprefab. CallsTrackWanderingTraderinternally to manage lifecycle. - Parameters: None.
- Returns:
Entity— The newly spawned Wandering Trader.
TryToSpawnWanderingTrader()
- Description: Attempts to spawn a Wandering Trader if one is not already active and spawn points exist. Selects a random spawn point from either
spawnpoints_masters(priority) orspawnpoints, positions the new trader there, and spawns it. - Parameters: None.
- Returns:
boolean—falseif spawning is skipped (either trader exists or no spawn points), otherwise implicitly returnsnilafter spawning.
RemoveWanderingTrader()
- Description: Safely removes the currently tracked Wandering Trader if it exists.
- Parameters: None.
OnSave()
- Description: Prepares save data for persistence. Records the GUID of the current Wandering Trader and includes it in the entities list.
- Parameters: None.
- Returns:
table, table— Save data table (withwanderingtraderkey), and list of tracked GUIDs.
LoadPostPass(newents, savedata)
- Description: Restores the Wandering Trader reference after loading by resolving the saved GUID against
newents. CallsTrackWanderingTraderto reattach event listeners. - Parameters:
newents(table): Mapping of GUIDs to entity data as loaded from save.savedata(table): Saved data fromOnSave.
OnRegisterSpawnPoint(inst, spawnpoint)
- Description: Registers a spawn point entity, distinguishing between master and non-master spawns. Adds it to the appropriate array and attaches an
"onremove"listener to auto-unregister. - Parameters:
inst(Entity): The entity sending the registration (unused).spawnpoint(Entity): The spawn point entity being registered.
UnregisterSpawnPoint(spawnpoint) / UnregisterSpawnPoint_Master(spawnpoint)
- Description: Helper functions to remove a spawn point from the non-master or master list, respectively. Called automatically when the spawn point emits
"onremove".
Events & Listeners
- Listens for:
"ms_registerspawnpoint"onself.inst→ triggersOnRegisterSpawnPoint."onremove"on trackedwanderingtrader→ triggersOnRemove_wanderingtrader."onremove"on each registeredspawnpoint→ triggers correspondingUnregisterSpawnPointorUnregisterSpawnPoint_Masterhandler.
- Emits (via
inst:DoTaskInTime):- Immediately post-construction, schedules
OnWorldPostInitto run once on0tick delay, which attempts spawning or removal based onTUNING.WANDERINGTRADER_ENABLED.
- Immediately post-construction, schedules