_NetworkTime

Inherits: Node

This class handles timing.

Tutorials

Properties

Type Name Default
int tickrate
bool sync_to_physics
int max_ticks_per_frame
float time
int tick
float recalibrate_threshold
float stall_threshold
int remote_tick
float remote_time
float remote_rtt
int local_tick
float local_time
float ticktime
float tick_factor
float physics_factor
float clock_stretch_max
bool suppress_offline_peer_warning
float clock_stretch_factor
float clock_offset
float remote_clock_offset

Methods

Return Type Name
int start()
void stop()
bool is_initial_sync_done()
bool is_client_synced(int peer_id)
float ticks_to_seconds(int ticks)
int seconds_to_ticks(float seconds)
float seconds_between(int tick_from, int tick_to)
int ticks_between(float seconds_from, float seconds_to)

Signals

before_tick_loop ( )

Emitted before a tick loop is run.


before_tick ( float delta, int tick )

Emitted before a tick is run.


on_tick ( float delta, int tick )

Emitted for every network tick.


after_tick ( float delta, int tick )

Emitted after every network tick.


after_tick_loop ( )

Emitted after the tick loop is run.


after_sync ( )

Emitted after time is synchronized.

This happens once the NetworkTime is started, and the first time sync process concludes. When running as server, this is emitted instantly after started.


after_client_sync ( int peer_id )

Emitted after a client synchronizes their time.

This is only emitted on the server, and is emitted when the client concludes their time sync process. This is useful as this event means that the client is ticking and gameplay has started on their end.


on_tickrate_mismatch ( int peer, int tickrate )

Emitted when a tickrate mismatch is encountered, and NetworkTickrateHandshake.mismatch_action is set to NetworkTickrateHandshake.SIGNAL.


Property Descriptions

int tickrate

Number of ticks per second.

read-only, you can change this in the project settings


bool sync_to_physics

Whether to sync the network ticks to physics updates.

When set to true, tickrate will be the same as the physics ticks per second, and the network tick loop will be run inside the physics update process.

read-only, you can change this in the project settings


int max_ticks_per_frame

Maximum number of ticks to simulate per frame.

If the game itself runs slower than the configured tickrate, multiple ticks will be run in a single go. However, to avoid an endless feedback loop of running too many ticks in a frame, which makes the game even slower, which results in even more ticks and so on, this setting is an upper limit on how many ticks can be simulated in a single go.

read-only, you can change this in the project settings


float time

Current network time in seconds.

Time is measured from the start of NetworkTime, in practice this is often the time from the server's start.

Use this value in cases where timestamps need to be shared with the server.

Note: Time is continuously synced with the server. If the difference between local and server time is above a certain threshold, this value will be adjusted.

See _NetworkTimeSynchronizer. See the setting "netfox/time/recalibrate_threshold".

read-only


int tick

Current network time in ticks.

Time is measured from the start of NetworkTime, in practice this is often the time from the server's start.

Use this value in cases where timestamps need to be shared with the server.

Note: Time is continuously synced with the server. If the difference between local and server time is above a certain threshold, this value will be adjusted.

See _NetworkTimeSynchronizer. See the setting "netfox/time/recalibrate_threshold".

read-only


float recalibrate_threshold

Threshold before recalibrating tick and time.

Time is continuously synced to the server. In case the time difference is excessive between local and the server, both tick and time will be reset to the estimated server values.

This property determines the difference threshold in seconds for recalibration.

read-only, you can change this in the project settings


float stall_threshold

Seconds required to pass before considering the game stalled.

If the game becomes unresponsive for some time - e.g. it becomes minimized, unfocused, or freezes -, the game time needs to be readjusted. These stalls are detected by checking how much time passes between frames. If it's more than this threshold, it's considered a stall, and will be compensated against.


int remote_tick

Current network time in ticks on the server.

This is value is only an estimate, and is regularly updated. This means that this value can and probably will change depending on network conditions.

read-only


float remote_time

Current network time in seconds on the server.

This is value is only an estimate, and is regularly updated. This means that this value can and probably will change depending on network conditions.

read-only


float remote_rtt

Estimated roundtrip time to server.

This value is updated regularly, during server time sync. Latency can be estimated as half of the roundtrip time. Returns the same as _NetworkTimeSynchronizer.rtt.

Will always be 0 on servers.

read-only


int local_tick

Current network time in ticks.

On clients, this value is synced to the server only once when joining the game. After that, it will increase monotonically, incrementing every single tick.

When hosting, this value is simply the number of ticks since game start.

This property can be used for things that require a timer that is guaranteed to be linear, i.e. no jumps in time.

read-only


float local_time

Current network time in seconds.

On clients, this value is synced to the server only once when joining the game. After that, it will increase monotonically, incrementing every single tick.

When hosting, this value is simply the seconds elapsed since game start.

This property can be used for things that require a timer that is guaranteed to be linear, i.e. no jumps in time.

read-only


float ticktime

Amount of time a single tick takes, in seconds.

This is the reverse of tickrate

read-only


float tick_factor

Percentage of where we are in time for the current tick.

0.0 - the current tick just happened
0.5 - we're halfway to the next tick
1.0 - the next tick is right about to be simulated

read-only


float physics_factor

Multiplier to get from physics process speeds to tick speeds.

Some methods, like CharacterBody's move_and_slide take velocity in units/sec and figure out the time delta on their own. However, they are not aware of netfox's time, so motion is all wrong in a network tick. For example, the network ticks run at 30 fps, while the game is running at 60fps, thus move_and_slide will also assume that it's running on 60fps, resulting in slower than expected movement.

To circument this, you can multiply any velocities with this variable, and get the desired speed. Don't forget to then divide by this value if it's a persistent variable ( e.g. CharacterBody's velocity ).

NOTE: This works correctly both in regular and in physics frames, but may yield different values.

read-only


float clock_stretch_max

The maximum clock stretch factor allowed.

For more context on clock stretch, see clock_stretch_factor. The minimum allowed clock stretch factor is derived as 1.0 / clock_stretch_max. Setting this to larger values will allow for quicker clock adjustment at the cost of bigger deviations in game speed.

Make sure to adjust this value based on the game's needs.

read-only, you can change this in the project settings


bool suppress_offline_peer_warning

Suppress warning when calling start with an OfflineMultiplayerPeer active.


float clock_stretch_factor

The currently used clock stretch factor.

As the game progresses, the simulation clock may be ahead of, or behind the host's remote clock. To compensate, whenever the simulation clock is ahead of the remote clock, the game will slightly slow down, to allow the remote clock to catch up. When the remote clock is ahead of the simulation clock, the game will run slightly faster to catch up with the remote clock.

This value indicates the current clock speed multiplier. Values over 1.0 indicate speeding up, under 1.0 indicate slowing down.

See clock_stretch_max for clock stretch bounds.
See _NetworkTimeSynchronizer for more on the reference- and simulation clock.

read-only


float clock_offset

The current estimated offset between the reference clock and the simulation clock.

Positive values mean the simulation clock is behind, and needs to run slightly faster to catch up. Negative values mean the simulation clock is ahead, and needs to slow down slightly.

See clock_stretch for more clock speed adjustment.

read-only


float remote_clock_offset

The current estimated offset between the reference clock and the remote clock.

Positive values mean the reference clock is behind the remote clock. Negative values mean the reference clock is ahead of the remote clock.

Returns the same as _NetworkTimeSynchronizer.remote_offset.

read-only


Method Descriptions

int start ( )

Start NetworkTime.

Once this is called, time will be synchronized and ticks will be consistently emitted.

On clients, the initial time sync must complete before any ticks are emitted.

To check if this initial sync is done, see is_initial_sync_done(). If you need a signal, see after_sync().

Returns OK on success.
Returns ERR_ALREADY_IN_USE if the tick loop is currently active.
Returns ERR_UNAVAILABLE if there's no available MultiplayerPeer.


void stop ( )

Stop NetworkTime.

This will stop the time sync in the background, and no more ticks will be emitted until the next start.


bool is_initial_sync_done ( )

Check if the initial time sync is done.


bool is_client_synced ( int peer_id )

Check if client's time sync is complete.

Using this from a client is considered an error.


float ticks_to_seconds ( int ticks )

Convert a duration of ticks to seconds.


int seconds_to_ticks ( float seconds )

Convert a duration of seconds to ticks.


float seconds_between ( int tick_from, int tick_to )

Calculate the duration between two ticks in seconds

Note: Returns negative values if tick_to is smaller than tick_from


int ticks_between ( float seconds_from, float seconds_to )

Calculate the duration between two points in time as ticks

Note: Returns negative values if seconds_to is smaller than seconds_from