godotengine / godot-proposals

Godot Improvement Proposals (GIPs)
MIT License
1.14k stars 95 forks source link

Add NetworkSelfConnected and NetworkSelfDisconnected signal to tree #1892

Open Shadowblitz16 opened 3 years ago

Shadowblitz16 commented 3 years ago

Describe the project you are working on: NetworkingTest

Describe the problem or limitation you are having in your project: I find it difficult to initialize the server due to the fact that I have to put my server initialization code right after i create the server.

I actually have multiple places I need to initialize the server and.

Describe the feature / enhancement and how it helps to overcome the problem or limitation: Add a NetworkSelfConnected and NetworkSelfDisconnected signal to tree.

Describe how your proposal will work, with code, pseudocode, mockups, and/or diagrams: Basically it would trigger on the server when the server is created or closed or on a client when the client is connected to the server or disconnected from the server.

func _ready():
get_tree().connect("network_peer_connected", self, "peer_connected")
get_tree().connect("network_peer_disconnected", self, "peer_disconnected")
get_tree().connect("network_self_connected", self, "self_connected")
get_tree().connect("network_self_disconnected", self, "self_disconnected")

func _peer_connected(id):
   #setup peer

func _peer_disconnected(id):
   #cleanup peer

func self_connected(id):
   #setup self

func _self_disconnected(id):
   #cleanup self if client to client

If this enhancement will not be used often, can it be worked around with a few lines of script?: It would be used to setup things in other nodes that don't have networking code and while it can be by creating your own signals it may not be very clear on where to do so.

Is there a reason why this should be core and not an add-on in the asset library?: It compliments the peer functions and makes it easy to initialize things when on the server or client when connected or disconnected.

Note I know the server doesn't connect to itself or disconnect from itself but the idea and code that would be used in these signals would be the same.

Faless commented 3 years ago

This should be doable in GDScript.

Add this script as an Autoload.

# autoload.gd
signal network_self_connected()
func create_server():
    # initialize server
    emit_signal("network_self_connected")

In the function where you would setup the server call:

Autoload.create_server()

where you initialize your game use:

func _read():
    Autoload.connect("network_self_connected", self, "self_connected")

Does this solution fits your needs?

Shadowblitz16 commented 3 years ago

@Faless how would disconnection be handled?

Faless commented 3 years ago

@Shadowblitz16 pretty much the same way:

# autoload.gd
signal network_self_disconnected()
func stop_server():
    # stop server
    multiplayer.network_peer = null
    emit_signal("network_self_disconnected")
Shadowblitz16 commented 3 years ago

@Faless the issue is I have to rely on calling these functions. if the server or client fails or doesn't connect it needs to be taken in account.

Faless commented 3 years ago

@Shadowblitz16 if the client connection fails (or disconnect), either connection_failed or network_peer_disconnected are emitted. https://docs.godotengine.org/en/stable/classes/class_scenetree.html#signals I don't see how the server can fail after being started (it can fail if the port is busy during start, but you will get an Error so you can easily manage that)