GameTimeManager
A class in Root.Township, the global namespace.
Keeps the time of day and month and decides when it's night. One timeline second is one game day. The server runs it and syncs clients every few seconds; get it from GameTimeManager.Instance.
- Listed in Global namespace: E to H
- Inherits or implements
NetworkEntityBehaviour,ISavable<GameTimeManagerSaveData>,ISavableSave<GameTimeManagerSaveData>,ISavableSaveBase - Derived types
GameTimeManagerSaveData - Nested types
TimeName - Documented in Time and environment
Constructors
| Member | What it is |
|---|---|
GameTimeManager() | Unity makes components itself; use AddComponent or spawn a prefab instead of new. |
Fields
| Member | What it is |
|---|---|
static GameTimeManager Instance | The current manager, set on Awake. |
18 private and internal fields
| Member | What it is |
|---|---|
private GameTimeManager.TimeName[] availableKeyWords | Named times of day (for example dawn) that UpdateTimeByKeyWord rushes to. |
private float dayDurationMinutes | Real minutes in one game day. Defaults to 20. |
private float dayProgress | Progress through the current day, 0 to 1. |
private float dayToNightRatio | Point in the day where day turns to night (before the small-hours adjustment). Defaults to 0.5. |
private Func<float, float> deltaOperation | Per-tick hook: ServerUpdate on the server, ClientUpdate on clients. |
private float initialTime | Starting time of a new world, as a fraction of a day. Defaults to 0.1. |
private bool isRushing | True while time is running faster towards targetTime. |
private bool isSettingInstance | If true (default), this manager becomes Instance on Awake. |
private static Logger logger | NLog logger for this class. |
private float monthDurationDays | Game days in one month. Defaults to 30. |
private float monthProgress | Progress through the current month, 0 to 1. |
private float nextSync | Real time of the next server sync. Set to 0 to sync at once. |
private float offsetFromServer | Client: remaining seconds of difference from the server clock to blend in. |
private float rushingTimeMultiplier | Speed-up while rushing. Defaults to 2. |
private float smallHoursNightPercentage | Share of the night counted as day at each end (the small hours). Defaults to 0.25. |
private MethodSyncStruct<bool> sync | Method sync used for time updates. |
private float syncRefreshRate | Seconds between server time syncs. Defaults to 10. |
private float targetTime | Day progress that rushing stops at. |
Properties
| Member | What it is |
|---|---|
IEnumerable<GameTimeManager.TimeName> AvailableKeyWords { get; } | Named times (such as dawn or noon) for UpdateTimeByKeyWord. |
float CurrentNormalizedTime { get; } | Time of day from 0 to 1 where the day half and night half are equal length. |
float CurrentTime { get; } | Time of day from 0 to 1 as the fraction of the current day. |
float DayDuration { get; } | Real seconds of daylight in one game day, counting the small hours as day. |
float DayDurationMinutes { get; } | Real minutes in one game day. |
float DayProgress { get; } | Progress through the current day, 0 to 1. |
float DayToNightRatio { get; } | Share of the day (0 to 1) that is the day part before night starts. |
Timeline DefaultTimeline { get; private set; } | Timeline of game days: the whole part counts days, the fraction is the time of day. |
static bool IsNight { get; private set; } | Whether it is night now. Static. |
float MonthProgress { get; } | Progress through the current month, 0 to 1. |
float NightDuration { get; } | Real seconds of night in one game day, not counting the small hours. |
Timeline NormalizedTimeline { get; private set; } | Timeline with the day and night halves stretched to equal length. |
Events
Methods
| Member | What it is |
|---|---|
string GetKeywords() | Text listing the available time keywords. |
override void Initialize() | Sets up the sync and picks the server or client update; clients start a timeline at the initial time. |
override void InitializeAsServer(SpawnData spawnData) | Server: makes the timeline if loading didn't, and starts following the main timeline. |
override void OnConnected(Player player) | Server: sends the time to a new player and makes them snap to it. |
void RushToTime(float time) | Server: fast-forwards to a normalized time (0 to 0.5 day part, 0.5 to 1 night part) at the rushing speed. |
void UpdateTimeByKeyWord(string name) | Server: rushes to a named time; logs an error for an unknown name. |
12 private and internal methods
| Member | What it is |
|---|---|
private void Awake() | Sets the static Instance if isSettingInstance is on. |
private float ClientUpdate(float dayDelta) | Client: adds a share of the remaining offset from the server time to each frame's delta, so the clock eases into sync. |
private void CreateTimeline(TimeSpan startTime) | Creates the default and normalized timelines from a start time and updates day and month progress. |
private TimeSpan GetNormalizedTime() | Converts default time so day and night each take half of the normalized day, whatever the day to night ratio. |
private float GetTimeFromTimeline(Timeline timeline) | Fraction of the current day on a timeline (one timeline second is one game day). |
private void OnDestroy() | Unhooks from the global timeline and clears Instance if it's this one. |
private void OnNightChanged(bool wasNight) | Raises NightChanged, then DawnReached or DuskReached. |
private float ServerUpdate(float dayDelta) | Server: sends a time sync to the chunks every syncRefreshRate seconds. |
private void Sync(IPlayer player, Stream stream, bool isForcingSnapping) | Syncs the time, day to night ratio and rush state. Clients snap if forced or more than 0.05 off; skipped in local tests. |
private void UpdateNormalizedTime() | Moves the normalized timeline to match the default one. |
private void UpdateProgressFromTimeline() | Recomputes day and month progress from the default timeline. |
private void UpdateTimeline(TimeSpan deltaTime) | Advances game time each tick (faster while rushing until the target is reached), updates night state and fires dawn or dusk. |
Used in
Members of other types that take, return or hold a GameTimeManager: