0 votes

HEY! i'm new to networking and i'm trying to make a multiplayer game.
The problem is with the host, it can see the client but can't see it's own player and the client has an extra player that's not moving, here's my Network.g file code:

extends Node

const MAX_PEERS    = 10
var   players      = {}
var   player_name
var can_spawn_player = false

func _ready():
    get_tree().connect("network_peer_connected", self, "_player_connected")
    get_tree().connect("network_peer_disconnected", self, "_player_disconnected")
    get_tree().connect("connected_to_server", self, "_connected_ok")
    get_tree().connect("connection_failed", self, "_connected_fail")
    get_tree().connect("server_disconnected", self, "_server_disconnect")

func start_server(name, Port):
    player_name = str(name)
    var host    = NetworkedMultiplayerENet.new()

    var err = host.create_server(Port, MAX_PEERS)

    if (err!=OK):
        print("error")
        return

    get_tree().set_network_peer(host)

func join_server(name, Port, server_ip):
    player_name = str(name)
    var host    = NetworkedMultiplayerENet.new()

    host.create_client(str(server_ip), Port)
    get_tree().set_network_peer(host)

func _player_connected(id):
    get_tree().change_scene("res://scenes/TestScene.tscn")
    if is_network_master():
        spawn_player(1)

func _player_disconnected(id):
    unregister_player(id)
    rpc("unregister_player", id)

func _connected_ok():
    rpc_id(1, "user_ready", get_tree().get_network_unique_id(), player_name)
    can_spawn_player = true

remote func user_ready(id, player_name):
    if get_tree().is_network_server():
        rpc_id(id, "register_in_game")

remote func register_in_game():
    rpc("register_new_player", get_tree().get_network_unique_id(), player_name)
    register_new_player(get_tree().get_network_unique_id(), player_name)

func _server_disconnected():
    quit_game()
    can_spawn_player = false

remote func register_new_player(id, name):
    if get_tree().is_network_server():
        rpc_id(id, "register_new_player", 1, player_name)

        for peer_id in players:
            rpc_id(id, "register_new_player", peer_id, players[peer_id])

    players[id] = name
    spawn_player(id)


remote func unregister_player(id):
    get_node("/root/" + str(id)).queue_free()
    players.erase(id)


func quit_game():
    get_tree().set_network_peer(null)
    players.clear()


func spawn_player(id):
    var player_scene = load("res://entity/player.tscn")
    var player       = player_scene.instance()
    var Label_scene = load("res://gameObjects/Label.tscn")
    var player_label = Label_scene.instance()

    if is_network_master():
        players[1] = player_name

    player.set_name(str(id))

    if id == get_tree().get_network_unique_id():
        player.set_network_master(id)
        player.player_id = id
        player.control   = true

    get_parent().add_child(player)
    player.add_child(player_label)
    player_label.text = players[id]
in Projects by (115 points)

Please log in or register to answer this question.

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 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 webmaster@godotengine.org with your username.

Categories