Godot Version
4.3.stable
Question
Been trying to setup my game to work on multiplayer but running into multiple issues. The Host can create a server perfectly fine, but when a client tries to join none of the code from their players script runs.
I’ve also setup a multiplayer synchronizer which doesn’t seem to be working at all so i’m not entirely sure what the issue is. if anyone could help that would be great.
Following this tutorial from 24:52 and got up to 30:44 if that helps: https://www.youtube.com/watch?v=n8D3vEx7NAE&t=1888s
Heres the code:
extends Node2D
@onready var main_menu = $canvas_layer/main_menu
@onready var address_entry = $canvas_layer/main_menu/MarginContainer/VBoxContainer/AddressEntry
const Player = preload("res://scenes/player.tscn")
# Port number to run server # Any port above 1024 should be okay to use
const PORT = 9999
var enet_peer = ENetMultiplayerPeer.new()
func _unhandled_input(event: InputEvent) -> void:
if Input.is_action_just_pressed("quit"):
get_tree().quit()
func _on_host_button_pressed() -> void:
main_menu.hide()
# Create Server with PORT variable
enet_peer.create_server(PORT)
# Connects Host to Server
multiplayer.multiplayer_peer = enet_peer
# Adds player whenever a client connects to the server
multiplayer.peer_connected.connect(add_player)
# Adds Host player to scene
add_player(multiplayer.get_unique_id())
func _on_join_button_pressed() -> void:
main_menu.hide()
# Create Client with Address and Port
enet_peer.create_client("localhost", PORT)
# Connects Client to Server
multiplayer.multiplayer_peer = enet_peer
func add_player(peer_id):
# Instantiate Player
var player = Player.instantiate()
# Set Player Name
player.name = str(peer_id)
# Make player a child of the main scene
add_child(player)