Confusion
IP.get_local_addresses()
as the name suggests returns addresses available to the calling device (e.g wifi, hotspot, Bluetooth, data, USB, localhost) and not addresses available on the connected network.
Answer
Automating the process of finding a host is a really bad and error prone rabbit hole idea, but understand the need and will share a hint.
Hint
Once connected to the network your device will have the same IP scheme as the host
There are only 255 possible options minus the requesting device's
It's possible to ping
an address and it will respond if it exists or poll
for packets and respond to it's sender
Code Snippet
# Server.gd
var udp_server = UDPServer.new()
var udp_port = 6969
func _ready():
udp_server.listen(udp_port, "0.0.0.0")
# do enetmultiplayer setup for host
func _process(delta):
udp_server.poll()
if udp_server.is_connection_available():
var udp_peer : PacketPeerUDP = udp_server.take_connection()
var packet = udp_peer.get_packet()
print("Recieved : %s from %s:%s" %
[
packet.get_string_from_ascii(),
udp_peer.get_packet_ip(),
udp_peer.get_packet_port(),
]
)
# Reply to valid udp_peer with server IP address example
udp_peer.put_packet(IP.get_local_addresses()[0])
#Client.gd
var udp_client := PacketPeerUDP.new()
var udp_server_found = false
var udp_requests = 3
var delta_time = 0.0
var udp_port = 6969
func _ready():
udp_client.set_broadcast_enabled(true)
udp_client.set_destination_address("255.255.255.255", udp_port)
func _process(delta):
delta_time += delta
if delta_time >= 2.0: #every 2 seconds send request
delta_time = 0.0
if not udp_server_found: # Try to contact server
udp_client.put_packet("Valid_Request".to_ascii())
udp_requests -= 1
if udp_requests == 0:
#start as server or stop sending request
pass
if udp_client.get_available_packet_count() > 0:
udp_server_found = true
var server_address_to_connect_to = udp_client.get_packet()
#connect to enetmultiplayer server using address
Notes
The above code example does weird things with ipv6 and you need to translate the recieved packet into a valid IP string on the client side before using.
The udp_port can be any port you desire that's not the same as your enet multiplayer one
To make this truly intuitive start every peer as a client and search for a host if a host is not found within a certain time that client becomes the host instead, this way the faster of the two will always be the host and subsiquent peers will connect seemlessly