27 lines
557 B
GDScript3
Raw Normal View History

2024-01-16 00:22:14 +01:00
extends Node
var udp := PacketPeerUDP.new()
var handlers: Array[Node] = []
func _init():
handlers.resize(256)
udp.connect_to_host("127.0.0.1", 4242)
func _process(_delta):
if udp.get_available_packet_count() <= 0:
return
var packet := udp.get_packet()
2024-01-16 23:01:16 +01:00
var type := packet.decode_u8(0)
2024-01-16 00:22:14 +01:00
var handler := handlers[type]
if handler == null:
push_warning("Unknown packet type %d" % type)
return
2024-01-16 23:01:16 +01:00
handler.handle_packet(packet.slice(1))
2024-01-16 00:22:14 +01:00
2024-01-16 23:01:16 +01:00
func add_handler(packet: int, node: Node):
2024-01-16 00:22:14 +01:00
assert(node.has_method("handle_packet"))
handlers[packet] = node