AtmosSystem: API practice
Use this skill when you need to quickly select the right AtmosSystem method and apply it without regressions :)
What to download
references/fresh-pattern-catalog.md- complete API registry with a freshness mark.references/rejected-snippets.md- methods/scenarios with restrictions and TODO.references/docs-context.md- connection of API solutions with documentation and design intent.
Quick method selection
- You need to get gas around the entity:
GetContainingMixture(...). - You need gas for a specific tile:
GetTileMixture(...)or batchGetTileMixtures(...). - We need to check air blocking for runtime logic:
IsTileAirBlockedCached(...). - We need strictly current data immediately after the changes:
IsTileAirBlocked(...). - It is necessary to light/warm up the atmosphere:
HotspotExpose(...). - Registration in barotrauma is required:
TryAddDeltaPressureEntity(...)/TryRemoveDeltaPressureEntity(...). - We need a map-level atmosphere:
SetMapAtmosphere(...),SetMapGasMixture(...),SetMapSpace(...). - We need gas transfer mathematics:
FractionToEqualizePressure(...)+MolesToPressureThreshold(...).
Patterns
- Do atmospheric pre-check via
IsTileSpace + IsTileAirBlockedCached. - For fire/explosions use
HotspotExpose, do not change hotspot manually. - For mass tile requests, use the batch call
GetTileMixtures. - For device-flow, first estimate the target number of moles, then transfer the volume, then
Merge. - For DeltaPressure, maintain lifecycle symmetry: init/add, shutdown/remove, grid-changed remove+add.
- After geometric changes, always send
InvalidateTile. - Change Map atmosphere through the immutable path (
SetMap*), and not through manual mutation map-mixture.
Anti-patterns
- Rely on
GetAdjacentTileMixtures(..., includeBlocked, excite)as a fully working API. - Use
SetSimulatedGridas a working simulation switch. - Set
IsTileAirBlockedin a tight-loop when the cached version is sufficient. - Bypass the API and directly pick
GridAtmosphereComponentcollections from gameplay systems. - Build new business logic on old LINDA/Superconductivity public methods without isolation.
Ways to optimize API calls
- Prefer
IsTileAirBlockedCached(...)for frequent checks, and leaveIsTileAirBlocked(...)for rare ones “immediately after invalidation”. - Where multiple tiles are checked, use
GetTileMixtures(...)instead of theGetTileMixture(...)series. - Don’t set
excite: true“out of habit”: this adds tiles to active processing and disrupts visuals. - After changing the world, use
InvalidateTile(...)and letRevalidatedo the hard work. - For DeltaPressure, use the lifecycle API (
TryAdd.../TryRemove...) instead of manual structures and linear searches. - For map-level changes, update the state in batches (
SetMapAtmosphereorSetMapGasMixture/SetMapSpace), and not in a series of separate operations. - In pipe devices, first count the moles/target pressure, then do one transfer + merge, not a chain of small transfers.
Code examples
1) Pressure regulator: correct transfer calculation
// 1) How many moles need to be removed so that inlet does not exceed the threshold.
var deltaMolesToPressureThreshold = AtmosphereSystem.MolesToPressureThreshold(inlet.Air, threshold);
// 2) How many moles are enough not to invert the pressure gradient.
var deltaMolesToEqualize = _atmosphere.FractionToEqualizePressure(inlet.Air, outlet.Air) * inlet.Air.TotalMoles;
// 3) Take the minimum and transfer the corresponding volume.
var deltaMoles = Math.Min(deltaMolesToPressureThreshold, deltaMolesToEqualize);
var removed = inlet.Air.RemoveVolume(volumeToTransfer);
_atmosphere.Merge(outlet.Air, removed);
2) Lifecycle DeltaPressure API
// Init: if the entity is on the grid, add it to processing.
_atmosphereSystem.TryAddDeltaPressureEntity(gridUid, ent);
// GridChanged: remove from old, add to new.
_atmosphereSystem.TryRemoveDeltaPressureEntity(oldGrid, ent);
_atmosphereSystem.TryAddDeltaPressureEntity(newGrid, ent);
// Shutdown: guaranteed cleaning.
_atmosphereSystem.TryRemoveDeltaPressureEntity(currentGrid, ent);
3) Explosion integration with Atmos API
// The explosion does not change the tile manually, but transfers heat through the canonical API.
if (temperature != null)
{
_atmosphere.HotspotExpose(gridUid, tile, temperature.Value, intensity, causeUid, soh: true);
}
4) Tile invalidation after changing airtight
// After changing the air blockers, mark the tile for the revalidate stage.
_explosionSystem.UpdateAirtightMap(grid, pos, grid);
_atmosphereSystem.InvalidateTile(grid.Owner, pos);
5) Safe selection of tiles for spawn
// We discard space and completely blocked tiles.
if (_atmosphere.IsTileSpace(gridUid, mapUid, tile)
|| _atmosphere.IsTileAirBlockedCached(gridUid, tile))
{
continue;
}
Rule of thumb
- By default, use only methods with the status
Fresh-Use. - Use methods with the status
Legacy-Compatonly for compatibility with tests. - Do not use methods with the status
Risk/TODOas support for new rules/API wrappers ⚠️ - Confirm any API optimization by profiling, and not just by eye.
Keep the API layer thin and predictable: Atmos is “forgiving” little if you bypass the contract 😅