How to Check is server is already created?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Deadscythe

I have a multiplayer game that can create a local server, but if I open it again and try to create a server, console will throw me and error because net IP is already connected. I ask if there is a way to check if my IP is free to use, or something similar before start to host, so game can avoid player from get this error

:bust_in_silhouette: Reply From: Wakatta

Unless you use set_bind_ip() its really only the port number that matters
And good old error checking is the way to resolve your issue

var check = peer.create_server(lan_port)
if check == ERR_ALREADY_IN_USE:
    use_another_port()

So maybe have a loop like this

var lan_port = 8080

func connect_server(lan_port : int):
    var check = peer.create_server(lan_port)
    if check == ERR_ALREADY_IN_USE:
        if lan_port < 8090: #prevent infinite loop
            lan_port += 1
            connect_server(lan_port)
    get_tree().set_network_peer(peer)

Thank you Wakatta

Deadscythe | 2023-01-07 02:33