Skip to main content

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.

Constructors​

MemberWhat it is
GameTimeManager()Unity makes components itself; use AddComponent or spawn a prefab instead of new.

Fields​

MemberWhat it is
static GameTimeManager InstanceThe current manager, set on Awake.
18 private and internal fields
MemberWhat it is
private GameTimeManager.TimeName[] availableKeyWordsNamed times of day (for example dawn) that UpdateTimeByKeyWord rushes to.
private float dayDurationMinutesReal minutes in one game day. Defaults to 20.
private float dayProgressProgress through the current day, 0 to 1.
private float dayToNightRatioPoint in the day where day turns to night (before the small-hours adjustment). Defaults to 0.5.
private Func<float, float> deltaOperationPer-tick hook: ServerUpdate on the server, ClientUpdate on clients.
private float initialTimeStarting time of a new world, as a fraction of a day. Defaults to 0.1.
private bool isRushingTrue while time is running faster towards targetTime.
private bool isSettingInstanceIf true (default), this manager becomes Instance on Awake.
private static Logger loggerNLog logger for this class.
private float monthDurationDaysGame days in one month. Defaults to 30.
private float monthProgressProgress through the current month, 0 to 1.
private float nextSyncReal time of the next server sync. Set to 0 to sync at once.
private float offsetFromServerClient: remaining seconds of difference from the server clock to blend in.
private float rushingTimeMultiplierSpeed-up while rushing. Defaults to 2.
private float smallHoursNightPercentageShare of the night counted as day at each end (the small hours). Defaults to 0.25.
private MethodSyncStruct<bool> syncMethod sync used for time updates.
private float syncRefreshRateSeconds between server time syncs. Defaults to 10.
private float targetTimeDay progress that rushing stops at.

Properties​

MemberWhat 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​

MemberWhat it is
static event Action DawnReachedRaised when night ends. Static.
static event Action DuskReachedRaised when night starts. Static.
static event Action<bool> NightChangedRaised with the new night state when it changes. Static.

Methods​

MemberWhat 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
MemberWhat 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: