extends Node var peer = ENetMultiplayerPeer.new() var connected_peers = [] func create_server(port, maxPlayers): peer.close() var result = peer.create_server(port, maxPlayers) # Second paramater specifies the player limit. Can be anything between 1 and 4095 multiplayer.multiplayer_peer = peer prints("Creating server result:", result) if result == OK: print("Server should be listening on port: ", port) elif result == ERR_ALREADY_IN_USE: printerr("Port is being used by something else!") else: printerr("Server can't start, and idk the problem") peer.peer_connected.connect( func(new_peer_id): await get_tree().create_timer(0.5).timeout ## REQUIRED, THE RPC IS MADE BEFORE THE CLIENT IS FULLY CONNECTED if connected_peers.has(new_peer_id): print("CONFLICT ID FOUND") return elif Global.roundInSession == true: print("GAME IN PROGRESS ", new_peer_id, " was declined entry") return else: print("NEW CONNECTION FROM ", new_peer_id) rpc_id(new_peer_id, "sync_mapPath", Global.currentMapPath) rpc_id(new_peer_id, "sync_names", Global.connectedPlayers) rpc_id(new_peer_id, "sync_respawn_speed", Global.respawnTimeModifier) rpc_id(new_peer_id, "sync_gamemode", Global.gamemode) rpc_id(new_peer_id, "sync_mapName", Global.mapName) rpc_id(new_peer_id, "sync_roundTimer", Global.roundTimer) rpc("add_newly_connected_player_character", new_peer_id) rpc_id(new_peer_id, "add_previously_connected_player_characters", connected_peers) add_player_node(new_peer_id) ) peer.peer_disconnected.connect( func(): peer_disconnected() ) func load_map(map): Global.multiplayerCurrent = true get_tree().change_scene_to_file(map) @rpc func add_newly_connected_player_character(new_peer_id): print("Connected to server!") if Global.teddyAuthorityID == null: load_map(Global.currentMapPath) Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED) add_player_node(new_peer_id) func add_player_node(peer_id): connected_peers.append(peer_id) Global.connectedPlayersByPeerID = connected_peers var player_character = preload("res://scenes/Teddy.tscn").instantiate() player_character.set_multiplayer_authority(peer_id) await get_tree().create_timer(0.5).timeout Global.currentMapNode.add_child(player_character) @rpc func add_previously_connected_player_characters(peer_ids): await get_tree().create_timer(0.5).timeout for peer_id in peer_ids: add_player_node(peer_id) func peer_disconnected(): print("Server connection lost...") Global.HUDStartLabelText = "Connection lost to server!" @rpc("any_peer", "reliable") func sync_names(connectedPlayers): Global.connectedPlayers = connectedPlayers rpc("sync_name_all_peers", Global.playerName) sync_name_all_peers(Global.playerName) @rpc("any_peer", "reliable") func sync_name_all_peers(name): Global.connectedPlayers += [name] print(Global.connectedPlayers) @rpc("any_peer", "reliable") func sync_respawn_speed(speed): Global.respawnTimeModifier = speed Global.playerPleaseRespawn = true @rpc("any_peer", "reliable") func sync_gamemode(gamemode): Global.gamemode = gamemode @rpc("any_peer", "reliable") func sync_mapName(map): Global.mapName = map @rpc("any_peer", "reliable") func sync_mapPath(path): Global.currentMapPath = path @rpc("any_peer", "reliable") func sync_roundTimer(time): Global.roundTimer = time @rpc("any_peer", "reliable") func check_name(name): # Returns true if it finds a matching name for i in range(Global.connectedPlayers.size()): if Global.playerName == Global.connectedPlayers[i]: return true else: return false @rpc("any_peer", "reliable") func check_round_status(status): pass return true