Skip to content

Instantly share code, notes, and snippets.

@Wavesonics
Last active February 7, 2023 22:21
Show Gist options
  • Save Wavesonics/c1586b01e26a622ba2e33f72a10d116a to your computer and use it in GitHub Desktop.
Save Wavesonics/c1586b01e26a622ba2e33f72a10d116a to your computer and use it in GitHub Desktop.
A real-time timer for synchronizing events in Godot across multiple clients to within 1 second of each other, which for my use case was good enough. It's API is meant to be a drop-in replacement for the default Timer node. Additionally it solved the problem of Timers in Godot being effected by poor performance. On a slower computer that is being…
# A drop in replacement for the Timer class
# This timer is completely framerate independent
# It can also be given a unix time stamp instead of a duration
# for when it should fire. This is useful for synchornizing events
# across multiple clients such as in Multiplayer
extends Node
class_name WallClockTimer, 'res://utilities/wallclock_timer/icon.png'
signal timeout
const STOPPED := -1
export (int) var wait_time: int = 0
export (bool) var auto_start: bool = false
export (bool) var one_shot: bool = true
var start_time: int = STOPPED
var end_time: int = STOPPED
var time_left: float setget set_time_left, get_time_left
func _ready():
if auto_start:
start()
func start(specific_end_time: int = STOPPED):
start_time = OS.get_unix_time()
if specific_end_time == STOPPED:
end_time = start_time + wait_time
else:
end_time = specific_end_time
func stop():
start_time = STOPPED
func is_stopped() -> bool:
return start_time == STOPPED
func set_time_left(value):
print('WallClockTimer: set_time_left is not allowed!')
assert(false) # You can't set this!
func get_time_left() -> float:
return (end_time - OS.get_unix_time()) as float
func _process(delta):
if is_stopped():
return
if OS.get_unix_time() >= end_time:
stop()
emit_signal('timeout')
# If this is not a one-shot timer, restart it
if not one_shot:
start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment