27 lines
557 B
GDScript
27 lines
557 B
GDScript
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()
|
|
var type := packet.decode_u8(0)
|
|
var handler := handlers[type]
|
|
|
|
if handler == null:
|
|
push_warning("Unknown packet type %d" % type)
|
|
return
|
|
|
|
handler.handle_packet(packet.slice(1))
|
|
|
|
func add_handler(packet: int, node: Node):
|
|
assert(node.has_method("handle_packet"))
|
|
handlers[packet] = node
|