Hi, I’m trying to create and run a server using this code
var server_port = 1610
var max_servers = 5
@onready var peer = ENetMultiplayerPeer.new()
# Called when the node enters the scene
func _ready():
start_server()
# Function to start the server
func start_server():`Preformatted text`
var result = peer.create_server(server_port, max_servers)
if result != OK:
print("Failed to create server on port - ", server_port, " ", result)
return
# Set the network peer for the multiplayer system
multiplayer.multiplayer_peer = peer
Im getting the err
E 0:00:00:0837 Authenticate.gd:13 @ start_server(): Couldn't create an ENet host.
<C++ Error> Parameter "host" is null.
<C++ Source> modules/enet/enet_connection.cpp:318 @ _create()
<Stack Trace> Authenticate.gd:13 @ start_server()
Authenticate.gd:8 @ _ready()
I have another project that has the identical code (that proj has port 1609 insted of 1610), and it works so I’m stuck
Maybe try getting rid of @onready for the peer, enet object. It could be a race condition, and it should be fine to have it create the entire object on init instead of ready.
I will say that your code looks fine. The error makes sense, but it is very unusual.
Looking at the modules/enet/enet_connection.cpp:318 it calls the enet library to create a host but it returns null which is a failure and Godot throws an error.
Maybe try a different port? Something like 5555 may be more suitable.
Maybe… Are you by chance running multiple instances? If so each will try and create a server and ports can’t be shared. Typically you setup buttons to trigger server creation or client creation.
I tried more ports, but none work.
I’m NOT running multiple instances; I am planning to but made sure they will run on different ports.
I will try to open a new proj and see if this continues.
Yea im kind of at a loss at this point without more clues. My gut is saying its something external to your setup.
Like maybe some local network settings but idk.
Enet docs says it only returns null on failure but doesn’t explain how it could fail. Although I figured it would spit out some kind of log message explaining the failure more… it could be that the enet library log was not captured by Godot? You could potentially test this if you start your project from command line and look at the errors in the terminal. Non-captured logs could show up in the terminal.
hey, I went and looked at the enet source code and here are the reasons why it may return null on the host object.
peer count > 4095
could not allocate memory
could not create a socket
fails to bind socket on port
Since you use a small number for peer count that should be okay.
Memory isn’t probably a concern, I think you would have other issues if it was.
The socket fails. I guess there could be a couple reasons a socket could fail. but I suspect its the last issue…
Failing to bind to a port, due to port collision.
I had another thought, Do you by chance have a @tool script that this multiplayer code is a child of? If so this could be starting a server within the editor and when you try and run the game, it collides with the existing editor game server.
after adding print(result) to the end of start_server() I got this output:
0
Failed to create server on port - 1607 20
20
start_server() is happening twice. This happened because I was running the program from this page, and I had it in my globals in the project setting, so it was starting twice.
TLDR: _ready() happens twice if the script is a global and is the “main” scene