–1 vote

HEY!! i'm very new to networking and tried making a multiplayer game, I followed a tutorial on youtube this is what i ended up with i'm sure you can spot very clear errors if you do please tell me!!
here are the problems i encountered:

-the client doesn't see the host 
-the host sees the client but it controls it too (and it has the same name too)

here's my code in my network.gd file:

extends Node

var SERVER_PORT = 31899
var MAX_PLAYERS = 2
var SERVER_IP = "192.168.1.46"  #192.168.1.46

var players = { }
var self_data = { name = "", posiiton = Vector2(360, 180) }


func _ready() -> void:
    get_tree().connect("network_peer_disconnected", self, "_player_disconnected")


func CreateServer(player_nickname):
    self_data.name = player_nickname
    players[1] = self_data
    var peer = NetworkedMultiplayerENet.new()
    peer.create_server(SERVER_PORT, MAX_PLAYERS)
    get_tree().set_network_peer(peer)
    get_tree().change_scene("res://scenes/TestScene.tscn")


func JoinServer(player_nickname):
    self_data.name = player_nickname
    get_tree().connect("connected_to_server", self, "_connected_to_server")
    var peer = NetworkedMultiplayerENet.new()
    peer.create_client(SERVER_IP, SERVER_PORT)
    get_tree().set_network_peer(peer)
    get_tree().change_scene("res://scenes/TestScene.tscn")


func _connected_to_server():
    players[get_tree().get_network_unique_id()] = self_data
    rpc("_send_player_info", get_tree().get_network_unique_id(), self_data)


func _player_connected(id):
    print("Player connected" + str(id))


func _player_disconnected(id):
    print("Player disconnected" + str(id))
    players.erase(id)


remote func _send_player_info(id, info):
    if get_tree().is_network_server():
        for peer_id in players:
            rpc_id(id, "_send_player_info", peer_id, players[peer_id])
    players[id] = info

    var new_player = load("res://entity/player.tscn").instance()
    new_player.name = str(id)
    new_player.set_network_master(id)
    get_tree().get_root().add_child(new_player)


func update_position(id, position):
    players[id].position = position 
asked May 3 in Projects by ROBOTOO007 (99 points)

1 Answer

0 votes

have you followed this guide? if so, check the demo project, it has some hidden scripts. You should start the networking script as an autoload and also spawn a player via your "world" script (it's shown in the tutorial for a brief moment). If it doesn't work and the networking script is the same as the tutorial's one, then your player code has some errors

answered Jun 12 by Emmabelotti (16 points)
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.