Problem with using code from godot 3.x to 4.1.1 with connect()
-
I am trying to make a multiplayer 2D shooter following a tutorial but the tutorial is using version 3.2.3. The code in version 3.2.3 is:
extends Control @onready var multiplayer_config_ui = $Multiplayer_configure @onready var server_ip_address = $Multiplayer_configure/Server_ip_address @onready var device_ip_address = $CanvasLayer/Device_ip_address func _ready() -> void: 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_to_server") device_ip_address = Network.ip_address func _player_connected(id) -> void: print("Player " + str(id) + " has connected") func _player_disconnected(id) -> void: print("Player " + str(id) + " has disconnected") func _on_create_server_pressed(): multiplayer_config_ui.hide() Network.create_server() func _on_join_server_pressed(): if server_ip_address.text != "": multiplayer_config_ui.hide() Network.ip_address = server_ip_address.text_changed Network.join_server()
however I get the error:
Line 9:Invalid argument for "connect()" function: argument 2 should be "Callable" but is "res://Network_setup.gd".
Line 9:Cannot pass a value of type "String" as "int".
Line 9:Invalid argument for "connect()" function: argument 3 should be "int" but is "String".on lines 9, 10 and 11. I have seen some other ways people have fixed it with timers but don't know how they would apply to this:
Gdscript connect() function -
What version you are currently using to follow along the tutorial?
The difference between Godot 3 and Godot 4 in terms of signal connecting is that in Godot 4 it's no longer necessary to use string, but the identifier itself. So normally:
In Godot 3:
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_to_server")
In Godot 4:
get_tree().network_peer_connected.connect(_player_connected) get_tree().network_peer_disconnected.connect(_player_disconnected) get_tree().connected_to_server.connect(_connected_to_server)
But then again you can't really use it since they have completely reworked multiplayer from Godot 3 -> Godot 4. And
network_peer_connected
,network_peer_disconnected
,connected_to_server
, are no longer signals ofget_tree()
-
I'm using 4.1.1 stable,
get_tree().network_peer_connected.connect(_player_connected) get_tree().network_peer_disconnected.connect(_player_disconnected) get_tree().connected_to_server.connect(_connected_to_server)
the first 2 lines seem to have worked (weirdly) and yes I get the error:
Line 11:Identifier "_connected_to_server" not declared in the current scope.What could I use to get around this problem?
This is the tutorial I'm following:
Godot 3 Networked Multiplayer Shooter Tutorial -