0 votes

I (thought) had followed the instructions here:
https://docs.godotengine.org/en/3.1/tutorials/networking/high_level_multiplayer.html
but I can't for the life of me get the signals to work. I would like to make sure that the servers are working before I get any further in the project. So my question is how do you work with signals with an autoloaded script like this? I've only ever done signals through the godot UI.

Thanks,
Xofox

Here's my code in case you want to see it. Beware, it's not awesome.

    extends Node

var UserIPAddress = "127.0.0.1"
var UserPort = 31400
var PlayerCount = 4
var Nickname

var players = {}
var self_data = {name = "", position = Vector3(0,0,0)}

func _ready():
    get_tree().connect("player_connected", self, "_player_connected")
    get_tree().connect("player_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_disconnected")

func create_server(player_nickname):
    self_data.name = player_nickname
    players[1] = self_data
    var peer = NetworkedMultiplayerENet.new()
    peer.create_server(float(UserPort),float(PlayerCount))
    get_tree().set_network_peer(peer)

func join_server(player_nickname):
    self_data.name = player_nickname
    var peer = NetworkedMultiplayerENet.new()
    peer.create_client(str(UserIPAddress),UserPort)
    get_tree().set_network_peer(peer)

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)
    print("Successfully connected")
func player_disconnected():
    print("Player has left the lobby")
    pass

func player_connected():
    print('Player connected: ')
    pass

func connection_failed():
    print("connection failed")

func server_disconnected():
    print("Server Disconnected")
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://Scenes/Player.tscn").instance()
    new_player.name = str(id)
    new_player.set_network_master(id)
    get_tree().get_root().add_child(new_player)
    new_player.init(info.name, info.position, true)
remote func kickPlayer():
    get_tree().set_network_peer(null)
in Engine by (93 points)

1 Answer

+1 vote

The names of the functions are wrong.
If you wrote

   get_tree().connect("connected_to_server", self, "_connected_ok")

then you name the func as

func _connected_ok():

and not

func connected_to_server():
by (150 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.
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.