如何使用高级多人游戏 (ENET) 在不同坐标生成两个节点



目前,我正在使用高等级多人游戏(ENET(和Godot来生成两个玩家节点(使用KinematicBody2D和Collisions(。由于碰撞导致问题的原因,我不能让玩家在同一坐标中生成,所以我试图让他们彼此相邻生成。

我把 Player.gd 放在这个 pastebin 链接上,其中包含我正在尝试调试的代码。

我遇到的问题是试图让客户端和服务器在我在玩家场景初始化中设置的坐标生成玩家。目前,客户端和服务器可以正确定位自己,但另一个玩家在 (0,0( 处挂着,直到我移动播放器。

玩家移动运行完美,因此即使该代码在粘贴中,也与此问题无关。

客户端的标准输出是

Connecting to Server!!!
Player Connected!!!
Network Master: 151057035
Player ID: 151057035
Client Position - Master: 151057035
Player: 151057035 Set Position: (500, 250)
Own ID: 151057035 Player ID: 151057035
Caller ID: 0
Network Master: 1
Player ID: 151057035
Server Position - Master: 1
Player: 1 Set Position: (300, 250)
Player: 1 Set Position: (300, 250)
Player: 151057035 Set Position: (500, 250)
Own ID: 151057035 Player ID: 151057035
Caller ID: 1

服务器的标准输出是

Hosting Server!!!
Player Connected!!!
Network Master: 1
Player ID: 1
Server Position - Master: 1
Player: 1 Set Position: (300, 250)
Own ID: 1 Player ID: 1
Caller ID: 0
Network Master: 959488417
Player ID: 1
Client Position - Master: 959488417
Player: 959488417 Set Position: (500, 250)
Player: 959488417 Set Position: (500, 250)
Player: 1 Set Position: (300, 250)
Own ID: 1 Player ID: 1
Caller ID: 959488417

基本上,要告诉客户端要生成什么坐标,你需要让服务器告诉客户端要使用什么坐标。

我自己的个人代码涉及玩家注册和跟踪,因此仅粘贴完整代码太复杂了。相反,我发布了一个大大简化的生成代码(甚至不包括多世界支持(


示例代码

# Server Code
master func spawn_server(net_id: int, coordinates: Vector2):
    rpc_unreliable_id(net_id, "spawn", coordinates) # Godot's RPC handles ensuring packet delivery just like TCP, but with less overhead.
# Client Code in Same File
puppet func spawn(coordinates: Vector2):
    player_object.position = coordinates

最新更新