Godot Version
4.2.1
Question
I’m adding a multiplayer controller for peer to peer connection on my game but the peer connected wont work or show
neither will my start game function
extends Control
@export var Address = “172.0.0.1”
@export var port = 8910
var peer
Called when the node enters the scene tree for the first time.
func _ready() → void:
multiplayer.peer_connected.connect(peer_connected)
multiplayer.peer_disconnected.connect(peer_disconnected)
multiplayer.connected_to_server.connect(connected_to_server)
multiplayer.connection_failed.connect(connection_failed)
pass # Replace with function body.
Called every frame. ‘delta’ is the elapsed time since the previous frame.
func _process(delta: float) → void:
pass
this get called on the server and clients
func peer_connected(id):
print(“Player Connected:” + str(id) )
pass
this get called on the server and clients
func peer_disconnected(id):
print(“Player Disconnected” + str(id))
pass
called only from clients
func connected_to_server():
print(“Connected To Server!”)
pass
called only from clients
func connection_failed():
print(“Connection Failed!”)
pass
func _on_host_button_down() → void:
peer = ENetMultiplayerPeer.new()
var error = peer.create_server(port, 2)
if error != OK:
print(“Cannot Host:” + str(error))
return
peer.get_host().compress(ENetConnection.COMPRESS_RANGE_CODER)
multiplayer.set_multiplayer_peer(peer)
print(“Waiting For Players!”)
pass # Replace with function body.
func _on_join_button_down() → void:
peer = ENetMultiplayerPeer.new()
peer.create_client(Address, 2)
peer.get_host().compress(ENetConnection.COMPRESS_RANGE_CODER)
multiplayer.set_multiplayer_peer(peer)
pass
func _on_start_game_button_down() → void:
Start_game.rpc()
pass # Replace with function body.
@rpc(“any_peer”,“call_local”)
func Start_game():
var scene = load(“res://Scenes/Basic.tscn”).instantiate()
get_tree().root.add_child(scene)
self.hide()