Godot Version
4.3
Question
I’m trying to create a LAN multiplayer following this tutorial. There is a host and clients. To this day I was debugging my project with Debug → Run Multiple Instances (2), and everything worked. But today i decided to try hosting on my PC and joining the game from other device. But client doesnt see any existing servers in the localhost, seemingly.
There are errors being produced that are similar to ones i get when running two instances on one device without starting hosting, and then trying to join non-existent peer.
func host(port:int):
does_hosting = true
peer.create_server(port)
multiplayer.multiplayer_peer = peer
multiplayer.server_relay = true
multiplayer.peer_connected.connect(connect_peer)
multiplayer.peer_disconnected.connect(disconnect_peer)
func disconnect_peer(id:int):
for player in multi.players:
if player.get_multiplayer_authority() == id:
player.queue_free()
break
func join(port:int):
print("Joined status ", peer.create_client("192.168.0.15", port))
multiplayer.multiplayer_peer = peer
var new_player: Player
while 1:
var do_break = false
for p in get_children():
if p is Player: p.show()
if p.name.to_int() == multiplayer.get_unique_id():
new_player = p
do_break = true
multi.player = new_player
break
if do_break: break
await get_tree().process_frame
multi.player.set_multiplayer_authority(new_player.name.to_int())
return true
Are they actually in the same lan? What do the errors say?
Thanks for answering. Yes, they are, they both are connected to the same network. Although it prints that joined status is 0(OK), the following loop just keeps looking for connected players infinitely.
Also, I already did reboot both computers, router, and had checked my firewall settings. Everything should work, but it just doesn’t.
its hard to tell what happens inside the loop since its not indented correctly. Can you paste the correct indentation?
Sorry for the wrong identation.
func join(port:int):
print("Joined status ", peer.create_client("192.168.0.15", port))
multiplayer.multiplayer_peer = peer
var new_player: Player
while 1:
var do_break = false
for p in $m.get_children():
if p is Player: p.show()
if p.name.to_int() == multiplayer.get_unique_id(): #upon entering tree, Player converts its multiplayer authority to name.
new_player = p
do_break = true
multi.player = new_player
break
if do_break: break
await get_tree().process_frame
multi.player.set_multiplayer_authority(new_player.name.to_int())
return true
you are checking the children every loop, but why would the children change during that loop?
I generally recommend to almost never use while-loops
I use MultiplayerSpawner node. When peer joins, it has to spawn Player scene on every peer, including the one joining. This process can take multiple frames, so you have to wait until your player appear.
No changes are seen in other device Remote Tab during the loop.
Once again, everything is fine when running on one device.
Why dont you just use the “spawned”-signal of the multiplayer-spawner. This also provides the spawned node: MultiplayerSpawner — Godot Engine (stable) documentation in English
Thanks, i got rid of while loop. anyway,any ideas how to fix original issue?
i would let it run in a seperate thread maybe. You couldve also used some print statements to check how many children currently are there