如何使用gdx-gamesvcs将游戏数据保存到云端?

How to save game data to the cloud using gdx-gamesvcs?

我想在 libgdx 引擎上的游戏中实施 Google Play 游戏服务。我尝试为此使用 gdx-gamesvcs。但是我在保存数据时遇到问题。我从这个例子中了解到,保存的是一个值,而不是游戏的整个状态。所以我决定检查一下:使用 gsClient.loadGameStategsClient.saveGameState 保存和加载一个值。我故意从设备中删除了游戏数据。但结果,不仅测试值发生了变化,其他许多值也发生了变化。以为整个游戏的状态都在保存,但是获取的值不符合游戏的逻辑,在里面获取不到。 我应该如何使用这个工具,它是否值得,还是使用 libgdx 本身提供的更好? 这是一段代码:

if (gsClient.isSessionActive()) {
            try {
                gsClient.saveGameState("data", intToByteArray(testValue), 0, null);
            } catch (UnsupportedOperationException unsupportedOperationException) {
}


if (gsClient.isSessionActive()) {
            try {
                gsClient.loadGameState("data", new ILoadGameStateResponseListener() {
                    @Override
                    public void gsGameStateLoaded(byte[] gameState) {
                        if (gameState != null) {
                            setTestValue(bytesToInt(gameState));
                        }
                    }
                });
            } catch (UnsupportedOperationException unsupportedOperationException) {
            }
}

更新 是的,保存发生在云和设备上,为了保存到我使用首选项的设备。我在游戏里有一个Google账号登录按钮,很好用,我多次看到这个标准的账号等级栏,在我登录的时候出现在最上面。一切都是在开发者控制台设置的,我有成就和排行榜的 ID。在代码中,我像这样与客户端一起工作(在 create() 方法中):

public IGameServiceClient gsClient;

if (gsClient == null) {

            gsClient = new MockGameServiceClient(1) {
                @Override
                protected Array<ILeaderBoardEntry> getLeaderboardEntries() {
                    return null;
                }

                @Override
                protected Array<String> getGameStates() {
                    return null;
                }

                @Override
                protected byte[] getGameState() {
                    return new byte[0];
                }

                @Override
                protected Array<IAchievement> getAchievements() {
                    return null;
                }

                @Override
                protected String getPlayerName() {
                    return null;
                }
            };
        }
        gsClient.setListener(this);
        gsClient.resumeSession();

接下来正在加载。
没有捕获到异常,我删除了它,一切都像以前一样。

好吧,libgdx 不提供 built-in cloud-save,因此很难使用它。 :-)

在任何情况下,您都应该保存到本地和云端,因为云端加载其状态的速度不是很快。

我看不出你的代码有什么问题,除了你吞下了一个 UnsupportedOperationException 如果你没有激活云保存功能就会抛出的问题。所以有趣的问题是:如果您不吞下异常会发生什么,并且您是否在启用云保存的情况下初始化了 GpgsClient?你真的登录了 Gpgs,你的开发者控制台中是否也激活了该功能?

主要问题是gameStatenull,这是因为在开发者控制台开启保存功能后需要等待24小时,清除提示google 的内存在测试设备上玩游戏没有帮助。一段时间后 gameState 开始传递现有值,但我开始遇到图形流问题,可能是由于异步加载。