You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多
文档控制台
免费开始使用

Godot 4.4.1多人游戏故障求助:玩家消失与节点重复错误

问题:Godot 4.4.1多人游戏玩家消失与同步异常

现象描述

  • 客户端加入游戏后,客户端玩家会在主机和客户端会话中立即消失,客户端触发错误:

E 0:00:03:353 on_spawn_receive: Condition "parent->has_node(name)" is true. Returning: ERR_INVALID_DATA
<C++ Source> modules/multiplayer/scene_replication_interface.cpp:603 @ on_spawn_receive()

  • 禁用MultiplayerSpawner后,客户端无法看到主机玩家

相关资源

节点树截图

  • 玩家节点树:玩家节点树
  • 主节点树:主节点树

Main脚本

extends Node

@onready var multiplayerUI = $UI/Multiplayer
@onready var spawner = $MultiplayerSpawner

var PLAYER = preload("res://scenes/main_character.tscn")

var peer = ENetMultiplayerPeer.new()

func _on_host_pressed() -> void:
    peer.create_server(25565)
    multiplayer.multiplayer_peer = peer

    multiplayer.peer_connected.connect(
        func(pid):
            print("Peer " + str(pid) + " has joined the game!")
            addPlayer.rpc(pid)
    )

    print("Peer " + str(multiplayer.get_unique_id()) + " has started hosting the game!")
    addPlayer.rpc(multiplayer.get_unique_id())
    multiplayerUI.hide()

func _on_client_pressed() -> void:
    peer.create_client("localhost", 25565)
    multiplayer.multiplayer_peer = peer
    multiplayerUI.hide()


@rpc("any_peer", "call_local", "reliable")
func addPlayer(pid):
    var player = PLAYER.instantiate()
    player.name = str(pid)
    print(player.name)
    #player.set_multiplayer_authority(pid)
    add_child(player)
    print("multiplayer authority set to: ", player.is_multiplayer_authority(), " for player: ", player.name)
    print("authority for player: ", player.name, " belong to: ", player.get_multiplayer_authority())


func _ready():
    var existing = get_node_or_null(str(multiplayer.get_unique_id()))
    if existing:
        print("🚨 Cleaning up pre-existing node with our ID: ", existing.name)
        existing.queue_free()

    # Listen to spawns
    spawner.spawned.connect(_on_spawned)
    print("✅ MultiplayerSpawner connected")

func _on_spawned(node):
    print("🚀 MultiplayerSpawner just spawned a node: ", node.name, " | Authority: ", node.get_multiplayer_authority(), " | Is this peer: ", node.get_multiplayer_authority() == multiplayer.get_unique_id())

玩家角色脚本

extends CharacterBody2D

@export var SPEED = 300.0
@export var JUMP_VELOCITY = -400.0
@export var WALL_SLIDE_VELOCITY = 50
@export var DASH_SPEED = 900.0
@onready var sprite_2d = $Sprite2D
#@onready var synchronizer: MultiplayerSynchronizer = $MultiplayerSynchronizer
var isJumping = false
var isDashing = false

func _enter_tree():
    set_multiplayer_authority(int(str(name)))
#func _enter_tree():
    # We set the authority after just before the player enters the scene
#   $MultiplayerSynchronizer.set_multiplayer_authority(str(name).to_int())

func _ready():
    #synchronizer.set_multiplayer_authority(int(str(name)))
    print("--- PLAYER SCRIPT _READY ---")
    print("Player Node Name: ", name)
    print("My (This Machine's) Unique ID: ", multiplayer.get_unique_id())
    print("Node's Stored Multiplayer Authority ID: ", get_multiplayer_authority())
    print("Is THIS MACHINE the authority for THIS NODE? (is_multiplayer_authority()): ", is_multiplayer_authority())
    print("----------------------------")

func _physics_process(delta: float) -> void:

    # Check if it's the correct player
    if !is_multiplayer_authority():
        return

    # The rest of the code...

运行输出日志

✅ MultiplayerSpawner connected
✅ MultiplayerSpawner connected
Peer 1 has started hosting the game!
1
--- PLAYER SCRIPT _READY ---
Player Node Name: 1
My (This Machine's) Unique ID: 1
Node's Stored Multiplayer Authority ID: 1
Is THIS MACHINE the authority for THIS NODE? (is_multiplayer_authority()): true
----------------------------
multiplayer authority set to: true for player: 1
authority for player: 1 belong to: 1
Peer 983377457 has joined the game!
983377457
--- PLAYER SCRIPT _READY ---
Player Node Name: 983377457
My (This Machine's) Unique ID: 1
Node's Stored Multiplayer Authority ID: 983377457
Is THIS MACHINE the authority for THIS NODE? (is_multiplayer_authority()): false
----------------------------
multiplayer authority set to: false for player: 983377457
authority for player: 983377457 belong to: 983377457
--- PLAYER SCRIPT _READY ---
Player Node Name: 1
My (This Machine's) Unique ID: 983377457
Node's Stored Multiplayer Authority ID: 1
Is THIS MACHINE the authority for THIS NODE? (is_multiplayer_authority()): false
----------------------------
🚀 MultiplayerSpawner just spawned a node: 1 | Authority: 1 | Is this peer: false
983377457
--- PLAYER SCRIPT _READY ---
Player Node Name: 983377457
My (This Machine's) Unique ID: 983377457
Node's Stored Multiplayer Authority ID: 983377457
Is THIS MACHINE the authority for THIS NODE? (is_multiplayer_authority()): true
----------------------------
multiplayer authority set to: true for player: 983377457
authority for player: 983377457 belong to: 983377457

求助说明

我是Godot新手,已尝试查阅论坛、官方文档及AI协助,但仍未解决问题,恳请各位提供帮助。

内容的提问来源于stack exchange,提问作者traponsix

火山引擎 最新活动