extends Node var peer = ENetMultiplayerPeer.new() var SCENE = "res://scenes/toyland.tscn" 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): print("NEW CONNECTION FROM ", new_peer_id) await get_tree().create_timer(0.5).timeout ## REQUIRED, THE RPC IS MADE BEFORE THE CLIENT IS FULLY CONNECTED rpc_id(new_peer_id, "sync_names", Global.connectedPlayers) rpc_id(new_peer_id, "sync_respawn_speed", Global.respawnTimeModifier) 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) ) 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(SCENE) 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) 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) player_character.position.y = 10 @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) @rpc func disconnect_peer(peer_id): peer_id.queue_free() @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 @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