29 lines
562 B
GDScript3
Raw Normal View History

2024-01-20 21:18:58 +00:00
extends PacketHandler
2024-01-15 23:22:14 +00:00
signal changed(ping: float)
const HISTORY_SIZE = 8
var count := 0
var history: Array[float] = []
func _init():
history.resize(HISTORY_SIZE)
2024-01-29 22:16:06 +00:00
func handle_packet(data: PackedByteArray):
2024-01-25 11:10:29 +00:00
var id := data[0]
var ping := get_time() - history[id]
changed.emit(ping)
2024-01-16 22:01:16 +00:00
func send_ping():
2024-01-15 23:22:14 +00:00
var buffer := StreamPeerBuffer.new()
2024-02-16 14:51:23 +00:00
buffer.put_u8(Packet.PING)
buffer.put_u8(count)
2024-01-26 18:00:59 +00:00
%Client.send(buffer.data_array)
2024-01-25 11:10:29 +00:00
history[count] = get_time()
2024-01-29 11:32:06 +00:00
count = (count + 1) % HISTORY_SIZE
2024-01-15 23:22:14 +00:00
2024-01-25 11:10:29 +00:00
func get_time() -> float:
return Time.get_unix_time_from_system()
2024-01-15 23:22:14 +00:00