My multiplayer doesnt work

Godot Version

4.5

Question

So im trying to make a multiplayer for my game and it was working but now when i enter my join code it doesntr work here is my lobby manager:

extends Node
static var lobbies : Dictionary = {}

static func register_lobby(code: String, port: int) → void:
lobbies[code] = port
print(“Registered:”, code, “→”, port)

static func has_lobby(code: String) → bool:
print(“Checking:”, code, “→”, lobbies.has(code))
return lobbies.has(code)

static func get_port(code: String) → int:
return lobbies.get(code, -1)

and this is my main menu:

extends Control

var peer := ENetMultiplayerPeer.new()
var port := 12345   # fixed port for all lobbies

@onready var create_button = $CreateGameButton
@onready var join_button = $JoinGameButton
@onready var code_input = $LobbyCodeInput

func _ready():
print(“MainMenu ready”)
create_button.pressed.connect(_on_create_pressed)
join_button.pressed.connect(_on_join_pressed)

func generate_lobby_code() → String:
var rng = RandomNumberGenerator.new()
rng.randomize()
return str(rng.randi_range(1000, 9999))

func _on_create_pressed():
var code = generate_lobby_code()
LobbyManager.register_lobby(code, port)
print(“Lobby created with code:”, code)

# FIX: use the fixed port, not the code
peer.create_server(port)
multiplayer.multiplayer_peer = peer

get_tree().set_meta("lobby_code", code)
get_tree().change_scene_to_file("res://Game.tscn")

print("Registering lobby:", code, "on port:", port)
print("Current lobbies:", LobbyManager.lobbies)

func _on_join_pressed():
var code = code_input.text
if code == “”:
return

if not LobbyManager.has_lobby(code):
	print("Invalid code:", code)
	return

var join_port = LobbyManager.get_port(code)
if join_port == -1:
	print("No port found for code:", code)
	return

peer.create_client("127.0.0.1", join_port)
multiplayer.multiplayer_peer = peer

print("Trying to join code:", code)
print("Current lobbies:", LobbyManager.lobbies)

print("Joining lobby with code:", code, " on port:", join_port)
get_tree().set_meta("lobby_code", code)
get_tree().change_scene_to_file("res://Game.tscn")


Quite a bit of code there with very little to go on. What exactly does “doesn’t work” mean?

The approach to use when debugging something like this is to click on the side of a line, and let the program run until then. A red spot will appear like this:

In this example, at line 95 of this script, the game will stop when its execution hits that spot and show you the code. You can then hover over values and see what they are and if they are what you expected, or proceed one line at a time with these controls in the ‘debugger’ tab:

image

Rollover them for explanations, but the two important ones are ‘step_into’ which executes just the next line, and skip_over which jumps any function calls etc (it doesn’t jump them, it just executes them and when it comes back stops again at the next current line.)

In that way you can follow through bit by bit what your program is actually doing, and usually it becomes obvious what is going wrong and where the code is failing.

Anyway, I hope that helps in some way.

PS I can’t tell you how many times I have done this only to be like “oh, wait…, what…, why did it go there? …Oh I see…” and to face palm at my own stupidity! It is also a great learning tool!