39 lines
911 B
GDScript
39 lines
911 B
GDScript
extends Label
|
|
|
|
const HISTORY_SIZE = 8
|
|
|
|
var pingCount := 0
|
|
var pingSent: Array[float] = []
|
|
|
|
func _ready():
|
|
var timer := Timer.new()
|
|
add_child(timer)
|
|
timer.autostart = true
|
|
timer.wait_time = 1
|
|
timer.connect("timeout", self._ping)
|
|
timer.start()
|
|
|
|
pingSent.resize(HISTORY_SIZE)
|
|
|
|
func _process(_delta):
|
|
if Client.udp.get_available_packet_count() > 0:
|
|
#print("Received: %s" % udp.get_packet().get_string_from_utf8())
|
|
var bytes := Client.udp.get_packet()
|
|
var count := bytes.decode_u8(1)
|
|
var timeSent := pingSent[count]
|
|
var duration := Time.get_unix_time_from_system() - timeSent
|
|
var ping := duration * 1000
|
|
text = str(snapped(ping, 0.01))
|
|
|
|
func _ping():
|
|
var buffer := StreamPeerBuffer.new()
|
|
buffer.put_8(0)
|
|
buffer.put_8(pingCount)
|
|
Client.udp.put_packet(buffer.data_array)
|
|
|
|
pingSent[pingCount] = Time.get_unix_time_from_system()
|
|
pingCount += 1
|
|
|
|
if pingCount >= HISTORY_SIZE:
|
|
pingCount = 0
|