Time and weather
Run the server's shared clock and calendar, give one player a sky or an hour of their own, and roll a weather cycle from the game's own climates and regions.
Everyone on a server shares one clock, one calendar and one sky. The server owns all three, and every player’s game follows. This page covers reading and setting them, overriding them for a single player, and building a weather cycle that behaves like the game’s own.
The examples come from the default gamemode’s commands, weather and client modules.
The clock and the calendar
Section titled “The clock and the calendar”World holds four pieces of time, each with its own setter:
| Read | Set | What it is |
|---|---|---|
World.hour |
World.setHour(13.5) |
The hour, minutes as the fraction: 13.5 is half past one |
World.timeScale |
World.setTimeScale(20) |
Game minutes per real minute. 20 is the game’s pace, 0 stops the clock |
World.date |
World.setDate(2287, 10, 23) |
What the Pip-Boy shows. No leap years |
World.daysPassed |
World.setDaysPassed(30) |
What the game’s timers count in: vendor restocks, crops |
Each setter leaves the others alone. Moving the date does not move the day count, and the other way around.
A /time command that accepts 13 or 13:30:
Events.on("playerCommand", (player: Player, command: string, args: string[]) => { if (command !== "time") return; const [hours, minutes] = (args[0] ?? "").split(":").map(Number); if (hours === undefined || !Number.isFinite(hours)) { Chat.sendToPlayer(player, "/time <hour> or <hh:mm>", { author: "Server" }); return; } World.setHour(hours + (minutes ?? 0) / 60);});Two events follow the running clock:
Events.on("worldHourChange", (hour: number) => { if (hour === 22) Chat.sendToAll("Curfew. Get indoors.", { author: "Sanctuary" });});
Events.on("worldDayChange", (date: WorldDate, daysPassed: number) => { console.log(`Day ${daysPassed}: ${date.year}-${date.month}-${date.day}`);});Neither is raised by your own setHour, setDate or setDaysPassed. They mean “time passed”, not
“time changed”, so jumping the clock forward does not fire a day’s worth of hourly handlers.
None of this survives a restart. Save characters and the world saves and restores it.
Sleeping and waiting
Section titled “Sleeping and waiting”The game’s bed and wait menu moves the player’s own clock forward and heals them for the hours passed. In a session their game snaps back to the server’s hour right after, so the only lasting effect is free healing. The default gamemode closes that menu as it opens:
// clientEvents.on("menuOpen", (name: string) => { if (name === "SleepWaitMenu") Ui.hideMenu("SleepWaitMenu");});The weather
Section titled “The weather”World.weather is the TESWeather form ID everyone sees. Set it by form ID or editor ID:
World.setWeather("CommonwealthRadstorm"); // fade into itWorld.setWeather("CommonwealthClear", true); // swap it in at onceWorld.setWeather(null); // the server's default, CommonwealthClearA fade runs on the game clock, so every client fades at the same point, including a player who joins
halfway through. While it runs, World.previousWeather is the weather being left; it reads 0 once
the fade is over.
Finding weathers
Section titled “Finding weathers”World.getWeathers() lists every weather in the game’s official masters, with its editor ID and
kind:
const storms = World.getWeathers().filter((record) => record.kind === "rain");for (const record of storms) { console.log(`${record.editorId} ${record.weather.toString(16)}`);}It is empty when the server cannot read the game’s data files, so set mod.game_data_path in
server.json.
Lightning
Section titled “Lightning”World.setLightningEnabled(true); // strike on its own, at the game's pace, while the weather thundersWorld.strikeLightning(); // one strike now, in every sky that thundersIn a session the game only strikes when the server says so, which is why every player sees the same flash at the same moment.
One player’s own sky
Section titled “One player’s own sky”Player.setHour and Player.setWeather override the shared values for one player only. Pass null
to follow World again:
// A nightmare: a radstorm at 2 a.m. for one player, for one minute.function nightmare(player: Player): void { player.setHour(2); player.setWeather("CommonwealthRadstorm", true); setTimeout(() => { player.setHour(null); player.setWeather(null, true); }, 60000);}setHour holds their sun and sky at that hour; their calendar and day count stay the world’s. Both
overrides last until the player leaves. They are not saved and do not follow the player to their
next session.
A weather cycle
Section titled “A weather cycle”The game decides weather with climates and regions. A worldspace has a climate: a list of weathers, each with a chance. Some regions inside it, the Glowing Sea for instance, have weathers of their own that take over while a player stands there. The API reads both from the game’s data:
| Call | Answers |
|---|---|
World.getClimate(worldSpace) |
The worldspace’s climate: { editorId, weathers: [{ weather, chance }] } |
player.getWeatherRegion() |
The region whose weathers apply where the player stands, 0 for none |
World.getRegionWeathers(region) |
That region’s weathers, in the same shape |
Rolling one by its chances:
function roll(climate: Climate | null): number | null { const weathers = climate?.weathers.filter((entry) => entry.chance > 0) ?? []; let pick = Math.random() * weathers.reduce((sum, entry) => sum + entry.chance, 0); for (const entry of weathers) { pick -= entry.chance; if (pick < 0) return entry.weather; } return null;}The Commonwealth
Section titled “The Commonwealth”For the shared sky, roll the Commonwealth’s climate and hold each weather for a few game hours. Keep
the time in game hours (daysPassed * 24 + hour) so it speeds up and slows down with timeScale:
const kCommonwealth = 0x3c;const kMinHours = 4;const kMaxHours = 10;
let until = 0;
function roll(climate: Climate | null): number | null { const weathers = climate?.weathers.filter((entry) => entry.chance > 0) ?? []; let pick = Math.random() * weathers.reduce((sum, entry) => sum + entry.chance, 0); for (const entry of weathers) { pick -= entry.chance; if (pick < 0) return entry.weather; } return null;}
const clock = (): number => World.daysPassed * 24 + World.hour;
function tick(): void { if (clock() < until) return; const next = roll(World.getClimate(kCommonwealth)); if (next === null) return; until = clock() + kMinHours + Math.random() * (kMaxHours - kMinHours); if (next !== World.weather) World.setWeather(next);}
Events.on("worldHourChange", () => tick());tick();Between 4 and 10 game hours is inside the game’s own range for how long a weather lasts.
Regions
Section titled “Regions”A player who walks into a region with weathers of its own should see them, without changing the sky
for everyone else. That is what Player.setWeather is for, and playerWeatherRegionChange tells
you when:
/** Each region's current weather, so players in the same region share it. */const regions = new Map<number, number>();
function roll(climate: Climate | null): number | null { const weathers = climate?.weathers.filter((entry) => entry.chance > 0) ?? []; let pick = Math.random() * weathers.reduce((sum, entry) => sum + entry.chance, 0); for (const entry of weathers) { pick -= entry.chance; if (pick < 0) return entry.weather; } return null;}
function regionWeather(region: number): number | null { const held = regions.get(region); if (held !== undefined) return held; const rolled = roll(World.getRegionWeathers(region)); if (rolled !== null) regions.set(region, rolled); return rolled;}
Events.on("playerWeatherRegionChange", (player: Player, region: number) => { // No region, or one without weathers of its own: back to the shared sky. const own = region !== 0 ? regionWeather(region) : null; player.setWeather(own); // a fade, as the game's own sky does at a region's edge});
Events.on("playerSpaceChange", (player: Player, space: PlayerSpace) => { // Through a door or a fast travel: swap at once, as a load screen would. const region = player.getWeatherRegion(); player.setWeather(space.interiorCell === 0 && region !== 0 ? regionWeather(region) : null, true);});Add an expiry per region, as the Commonwealth has, and the cycle is complete. The default gamemode’s
weather.ts does exactly that, and also keeps the cycle’s state in its save so a restart picks up
the same weather it left.
Related
Section titled “Related”- Save characters and the world: the clock and weather across a restart.
- Server API in the header:
World, andPlayer.setHour,setWeatherandgetWeatherRegion.