Godot Version
4.2
Question
I’m trying to figure out how to sync the changing of the level in a multiplayer game I’m working on. So far, it spawns each player correctly, but the host can’t see other peers moving and the peers can’t see the host moving. However, peers can see each other moving. So I’m not sure if it’s an issue with my code, my multiplayer synchronizer, or something entirely different. Any help would be appreciated!
The code for the scene I change to:
extends Node3D
const Player = preload("res://player.tscn")
func _ready():
add_player(multiplayer.get_unique_id())
sync_peers()
func sync_peers():
var players = multiplayer.get_peers()
for peer in players:
add_player(peer)
func add_player(peer_id):
var player = Player.instantiate()
player.name = str(peer_id)
add_child(player)
The code for the lobby (scene I change from):
extends Node3D
@onready var main_menu = $CanvasLayer/MainMenu
@onready var address = $CanvasLayer/MainMenu/MarginContainer/VBoxContainer/AddressEntry
const Player = preload("res://player.tscn")
const PORT = 9999
var enet_peer = ENetMultiplayerPeer.new()
func _on_host_button_pressed():
main_menu.hide()
enet_peer.create_server(PORT)
multiplayer.multiplayer_peer = enet_peer
multiplayer.peer_connected.connect(add_player)
add_player(multiplayer.get_unique_id())
func _on_join_button_pressed():
main_menu.hide()
enet_peer.create_client("localhost", PORT)
multiplayer.multiplayer_peer = enet_peer
func add_player(peer_id):
var player = Player.instantiate()
player.name = str(peer_id)
add_child(player)
Player movement and connection code:
func _enter_tree():
set_multiplayer_authority(name.to_int())
func _ready():
if not is_multiplayer_authority():
return
ambience.play()
camera.current = true
audio.current = true
func _physics_process(delta):
if not is_multiplayer_authority():
return
move_harpoons(delta)
var input_dir = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
var direction = (transform.basis * Vector3(-input_dir.y, 0, input_dir.x)).normalized()
if direction:
velocity.x = direction.x * SPEED
velocity.z = direction.z * SPEED
velocity.y = direction.y * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
velocity.z = move_toward(velocity.z, 0, SPEED)
velocity.y = move_toward(velocity.y, 0, SPEED)
var looking_at = ray.get_collider()
if is_multiplayer_authority():
if looking_at != null:
if looking_at.is_in_group("interactable"):
if Input.is_action_just_pressed("interact"):
looking_at.interact.rpc(name)
move_and_slide()
func _unhandled_input(event):
if not is_multiplayer_authority():
return
if event is InputEventMouseButton:
if Input.is_action_pressed("shoot"):
shootHarpoon.rpc()
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
if event is InputEventMouseMotion:
rotate_y(-event.relative.x*SENS)
rotation.z += (-event.relative.y*SENS)
if event is InputEventKey:
if Input.is_action_pressed("ui_cancel"):
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
Code for scene change:
@rpc("call_local")
func interact(interactor):
if interactor.to_int() == 1:
get_tree().change_scene_to_file("res://firstlevel.tscn")
If you need to see more of my code, please let me know. Also, if any of the code looks absolutely terrible to experienced programmers, I apologize. I am very new and don’t have a whole lot of experience in Godot.