Load player scenes ?

GenoxGenox Posts: 7Member

Hi everyone.

As you know, to play a multiplayer game you need to see your character and other players' characters.
Well, I found the godot's doc about high level multiplayer but I understood nothing as my server and my client are two separated projects.

So, I wanted to ask : How could I create a player list in the server, and everytime someone connects, a player is added on the list and every client connected refreshes its list and a new scene is instanced in every client ?

I tried to use a PoolByteArray for the server's list but I don't know how to make a function for every byte in the pool, instance a scene.


Tags :

Best Answer

  • Ackman6Ackman6 Posts: 3
    edited September 18 Accepted Answer

    There is a function in the SceneTree that gets the peer ID of every connected peer (Including the server). You can call it using;

    get_tree().get_network_connected_peers()

    To synchronize clients they must be able to do three things

    1. Whenever a new player joins the game it must create scenes for every other player
    2. Whenever a player joins the game its scene has to be created on every computer
    3. Whenever a player leaves the game its scene has to be removed on every computer

    The 3 tasks require 3 signals (All on the client). Clients should do as much work as possible. The only work the server should do is the calculations that you don't trust the clients to do for example. Damage calculations or XP calculation.

    1. connected_to_server()

      get_tree().connect("connected_to_server", self, "_connected_to_server")
      
      # This function is called when this player connects to the server
      func _connected_to_server():
          # Loop through peers and instance instance players with their unique peer ID as their name
          for peer_id in get_tree().get_network_connected_peers():
              # If the peer ID = 1 don't instance it because thats the server
              if peer_id != 1:
                  var new_peer = load("player").instance()
                  new_peer.name = peer_id
                  add_child(new_peer)
      
    2. network_peer_connected ( int id )

      get_tree().connect("network_peer_connected", self, "_peer_connected")
      
      # This function is called when a player joins the server
      func _peer_connected(peer_id):
          # Make sure you don't add yourself
          if peer_id != get_tree().get_network_unique_id():
              var new_peer = load("player").instance()
              new_peer.name = peer_id
              add_child(new_peer)
      
    3. network_peer_disconnected ( int id )

      get_tree().connect("network_peer_disconnected", self, "_peer_disconnected")
      
      # This function is called when a player leaves the server
      func _peer_disconnected(peer_id):
          # Make sure you don't kill yourself
          if peer_id != get_tree().get_network_unique_id():
              # Kill the peers node
              get_node(str(peer_id)).queue_free()
      

Answers

  • Ackman6Ackman6 Posts: 3Member
    edited September 18 Accepted Answer

    There is a function in the SceneTree that gets the peer ID of every connected peer (Including the server). You can call it using;

    get_tree().get_network_connected_peers()

    To synchronize clients they must be able to do three things

    1. Whenever a new player joins the game it must create scenes for every other player
    2. Whenever a player joins the game its scene has to be created on every computer
    3. Whenever a player leaves the game its scene has to be removed on every computer

    The 3 tasks require 3 signals (All on the client). Clients should do as much work as possible. The only work the server should do is the calculations that you don't trust the clients to do for example. Damage calculations or XP calculation.

    1. connected_to_server()

      get_tree().connect("connected_to_server", self, "_connected_to_server")
      
      # This function is called when this player connects to the server
      func _connected_to_server():
          # Loop through peers and instance instance players with their unique peer ID as their name
          for peer_id in get_tree().get_network_connected_peers():
              # If the peer ID = 1 don't instance it because thats the server
              if peer_id != 1:
                  var new_peer = load("player").instance()
                  new_peer.name = peer_id
                  add_child(new_peer)
      
    2. network_peer_connected ( int id )

      get_tree().connect("network_peer_connected", self, "_peer_connected")
      
      # This function is called when a player joins the server
      func _peer_connected(peer_id):
          # Make sure you don't add yourself
          if peer_id != get_tree().get_network_unique_id():
              var new_peer = load("player").instance()
              new_peer.name = peer_id
              add_child(new_peer)
      
    3. network_peer_disconnected ( int id )

      get_tree().connect("network_peer_disconnected", self, "_peer_disconnected")
      
      # This function is called when a player leaves the server
      func _peer_disconnected(peer_id):
          # Make sure you don't kill yourself
          if peer_id != get_tree().get_network_unique_id():
              # Kill the peers node
              get_node(str(peer_id)).queue_free()
      
  • GenoxGenox Posts: 7Member
    edited September 22

    Thanks a lot Ackman6, I never thought such a function could exist, you saved me !!
    Now, I should try sync player's movements

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file