34 lines
626 B
GDScript3
Raw Normal View History

2024-01-16 00:22:14 +01:00
extends Node
var udp := PacketPeerUDP.new()
var handlers: Array[Node] = []
enum Packet {
PING = 1,
LOGIN = 2,
LOGOUT = 3,
MOVE = 10,
}
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) as Packet
var handler := handlers[type]
if handler == null:
push_warning("Unknown packet type %d" % type)
return
handler.handle_packet(packet)
func add_handler(packet: Packet, node: Node):
assert(node.has_method("handle_packet"))
handlers[packet] = node