0 votes

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

Godot version V 3.51
in Engine by (15 points)

1 Answer

+1 vote
Best answer

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)
by (6,876 points)
edited by

Thank you Wakatta

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.