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

huangapple go评论91阅读模式
英文:

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

问题

我想在使用libgdx引擎制作的游戏中实现Google Play Games Services。我尝试使用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) {
    }
}

更新:
是的,保存既发生在云端,也发生在设备上,我使用Preferences进行设备上的保存。游戏中有一个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();

接下来是加载。

异常没有被捕获,我将它删除了,一切都和之前一样正常。

英文:

I want to implement Google Play Games Services in my game on the libgdx engine. I tried using gdx-gamesvcs for this. But I am having trouble saving data. I understood from the example that one value is being saved, not the entire state of the game. So I decided to check it out: save and load one value using gsClient.loadGameState and gsClient.saveGameState. I deliberately deleted the game data from the device. But as a result, not only the test value changed, but many others as well. I thought that the state of the entire game is being saved, but the values ​​obtained do not fit into the logic of the game and could not be obtained in it.
How should I use this tool and is it worth it at all, or is it better to use what libgdx itself offers?
Here is a piece of code:

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


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

UPD
Yes, saving occurs both to the cloud and to the device, for saving to the device I use Preferences. I have a Google account login button in the game, it works, I have repeatedly seen this standard bar of my account level, which appears at the top when I log in. Everything is set up in the developer console too, I have an id for achievements and leaderboards. In code, I work with the client like this (In the create() method):

public IGameServiceClient gsClient;

if (gsClient == null) {

            gsClient = new MockGameServiceClient(1) {
                @Override
                protected Array&lt;ILeaderBoardEntry&gt; getLeaderboardEntries() {
                    return null;
                }

                @Override
                protected Array&lt;String&gt; getGameStates() {
                    return null;
                }

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

                @Override
                protected Array&lt;IAchievement&gt; getAchievements() {
                    return null;
                }

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

Next is loading.
The exception is not caught, I removed it and everything works as before.

答案1

得分: 0

好的,以下是您要翻译的内容:

libgdx并未提供内置的云保存功能,因此很难在其中使用它。 :-)

无论如何,您都应该将数据保存到本地和云端,因为从云端加载状态的速度并不是很快。

除了您在代码中捕获了一个`UnsupportedOperationException`异常之外,我没有看到任何问题。该异常会在您未激活云保存功能时抛出。因此,有趣的问题是:如果您不捕获异常会发生什么?您是否使用已启用云保存功能来初始化`GpgsClient`?您是否真正登录到了Gpgs,而且您的开发者控制台中是否也已激活了该功能?
英文:

Well, libgdx offers no built-in cloud-save, it is hard to use it for that. 如何使用gdx-gamesvcs将游戏数据保存到云端?

You should in any case save to local AND to cloud, as the cloud is not very fast to load its state.

I can see no problem in your code besides the fact that you swallow an UnsupportedOperationException that is thrown if you did not activate cloud save feature. So the interesting question is: what happens if you don't swallow the exception, and did you intialize GpgsClient with cloud save enabled? Are you really logged in to Gpgs, and is the feature also activated in your developer console?

答案2

得分: 0

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

英文:

The main problem was that gameState was null, this arose due to the fact that you had to wait 24 hours after enabling the save function in the developer console, and the advice on clearing the memory of google play games on the test device did not help. After a while gameState began to pass the existing values, but I started having problems with the graphics flow, probably due to the asynchronous loading.

huangapple
  • 本文由 发表于 2020年8月21日 02:35:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/63511274.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定