跳转到内容

状态与 RPC

状态与 RPC 属于 联机大厅 的子能力。状态用于同步房间内的共享数据或玩家数据;RPC 用于在房间内触发一段命名逻辑。两者都需要在玩家进入房间后使用。

引擎状态说明
UnityPreview支持状态 set / get / reset 和 RPC register / call / unregister。
GameMaker暂不支持当前 GameMaker 首期不包含状态和 RPC。
Godot开发中预览 runtime 已有 JSON 状态和 RPC 调用。
  • 已完成 房间 的最小流程。
  • 已进入房间。
  • 状态 key 和 RPC 名称需要保持稳定。
using System;
using UnityEngine;
using IGP.Multiplayer;
public sealed class IGPStateRpcDriver : MonoBehaviour
{
[SerializeField] private IGPMultiplayerRuntime runtimeManager;
public async void SetGlobalScore()
{
await runtimeManager.SetGlobalStateAsync("match.score", new
{
red = 1,
blue = 0,
updatedAt = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
});
}
public async void RegisterEchoRpc()
{
await runtimeManager.RegisterRPCAsync("sample.echo");
}
public async void CallEchoRpc()
{
var requestId = await runtimeManager.CallRPCAsync(
"sample.echo",
new { text = "hello" },
"all");
Debug.Log($"RPC request id={requestId}");
}
}

通过 IGPMultiplayerRuntime.MessageReceived 接收状态和 RPC 消息,并按 state_setstate_getstate_resetrpc_callrpc_response 类型分派。对应的 Content 已由 SDK 转换为 StateSetContentStateGetResponseContentStateResetResponseContentRPCCallContentRPCResponseContent;使用 GetValue<T>() / GetData<T>() 读取游戏数据,不需要解析 JObject。回复 RPC 时使用 RespondRPCAsync,目标玩家传入 RPCCallContent.CallerPlayerId

引擎检查点
Unity设置状态后能收到状态变化;注册 RPC 后能调用并收到响应。
GameMaker当前不做状态与 RPC 验收。
GodotSetStateJsonAsyncGetStateAsyncCallRpcJsonAsync 能顺序完成。
  • 无法读取状态:确认 scope 和 key 完全一致。
  • RPC 未触发:先确认已经注册对应 RPC 名称。
  • Unity 未收到事件:确认已监听 IGPMultiplayerRuntime.MessageReceived,并检查收到的 MessageType
  • Godot JSON 失败:先用简单对象字符串验证,例如 {"value":1}