Project1

标题: 如何使用json文件(或者字符串)作为存档开始游戏 [打印本页]

作者: 神风炽羽    时间: 2021-1-28 00:19
标题: 如何使用json文件(或者字符串)作为存档开始游戏

这个问题是因为我在写存档存储云端时将其作为字符串存入数据库,获得这个字符串之后,我却无法将其作为存档,求教

我希望实现的是点击继续游戏按钮后使用我提供的字符串,也就是json文件作为游戏的存档,继续游戏,请问这个应该怎么写?
作者: zths    时间: 2021-1-28 00:19
  1. function LoadGameContents(contents){
  2.     DataManager.createGameObjects();
  3.     DataManager.extractSaveContents(JsonEx.parse(contents));

  4.     if(typeof SceneManager._scene.fadeOutAll !== "undefined"){
  5.         if(SceneManager._scene.fadeOutAll){
  6.             SceneManager._scene.fadeOutAll();
  7.         }
  8.     }
  9.    
  10.     if(typeof SceneManager._scene.reloadMapIfUpdated !== "undefined"){
  11.         if(SceneManager._scene.reloadMapIfUpdated){
  12.             SceneManager._scene.reloadMapIfUpdated();
  13.         }
  14.     }
  15.    
  16.     if(typeof $gameSystem.onAfterLoad !== "undefined"){
  17.         if($gameSystem.onAfterLoad){
  18.             try{
  19.                 $gameSystem.onAfterLoad();
  20.             }catch(e){
  21.                 console.error(e);
  22.             }
  23.         }
  24.     }
  25.     SceneManager.goto(Scene_Map);
  26. }
  27. LoadGameContents("json字串");
复制代码

作者: 神风炽羽    时间: 2021-1-28 12:35
zths 发表于 2021-1-28 00:28

遗憾,还是没能解决,可否再看一下,以下为我写的代码
  1. DataManager.maxSavefiles = function () {
  2.     return 1;
  3. };
  4. DataManager.isAnySavefileExists = function() {
  5.     return true;
  6. };
  7. StorageManager.saveToLocalFile = function (savefileId, json) {
  8.     Network.instance.sendSaveJson(json);//发送至服务器
  9.     // // 数据 = LZString 压缩到基准64(json)
  10.     // var data = LZString.compressToBase64(json);
  11.     // // fs = 要求("fs")
  12.     // var fs = require('fs');
  13.     // // 目录路径 = 本地文件目录路径()
  14.     // var dirPath = this.localFileDirectoryPath();
  15.     // // 文件路径 = 本地文件路径( 保存文件id )
  16.     // var filePath = this.localFilePath(savefileId);
  17.     // //如果(不是 fs 存在(目录路径))
  18.     // if (!fs.existsSync(dirPath)) {
  19.     //     //fs 建立目录(目录路径)
  20.     //     fs.mkdirSync(dirPath);
  21.     // }
  22.     // //fs 写入文件(文件路径, 数据 )
  23.     // fs.writeFileSync(filePath, data);

  24.    
  25. };

  26. Scene_Title.prototype.commandContinue = function() {//点击继续游戏的指令
  27.     function LoadGameContents(contents){
  28.         DataManager.createGameObjects();
  29.         DataManager.extractSaveContents(JsonEx.parse(contents));
  30.    
  31.         if(typeof SceneManager._scene.fadeOutAll !== "undefined"){
  32.             if(SceneManager._scene.fadeOutAll){
  33.                 SceneManager._scene.fadeOutAll();
  34.             }
  35.         }
  36.       
  37.         if(typeof SceneManager._scene.reloadMapIfUpdated !== "undefined"){
  38.             if(SceneManager._scene.reloadMapIfUpdated){
  39.                 SceneManager._scene.reloadMapIfUpdated();
  40.             }
  41.         }
  42.       
  43.         if(typeof $gameSystem.onAfterLoad !== "undefined"){
  44.             if($gameSystem.onAfterLoad){
  45.                 try{
  46.                     $gameSystem.onAfterLoad();
  47.                 }catch(e){
  48.                     console.error(e);
  49.                 }
  50.             }
  51.         }
  52.         SceneManager.goto(Scene_Map);
  53.     }
  54.     var json = Network.instance.saveJson;//获取字符串
  55.     LoadGameContents(json);
  56. };
复制代码

以下为报错
  1. TypeError: Cannot read property 'onAfterLoad' of undefined
  2.     at LoadGameContents (SfcySaveOnline.js:45)
  3.     at Scene_Title.commandContinue (SfcySaveOnline.js:57)
  4.     at Window_TitleCommand.Window_Selectable.callHandler (rpg_windows.js:902)
  5.     at Window_TitleCommand.Window_Command.callOkHandler (rpg_windows.js:1426)
  6.     at Window_TitleCommand.Window_Selectable.processOk (rpg_windows.js:1156)
  7.     at Window_TitleCommand.processOk (rpg_windows.js:5763)
  8.     at Window_TitleCommand.Window_Selectable.onTouch (rpg_windows.js:1086)
  9.     at Window_TitleCommand.Window_Selectable.processTouch (rpg_windows.js:1054)
  10.     at Window_TitleCommand.Window_Selectable.update (rpg_windows.js:986)
  11.     at rpg_core.js:7035
复制代码

获取到的字符串
  1. "[null,{"globalId":"RPGMV","title":"test","characters":[["Actor1",0],["Actor1",0],["Actor1",0],["Actor1",0],["Actor1",0]],"faces":[["face1",5],["face1",1],["face1",2],["face1",3],["face1",4]],"playtime":"00:00:47","timestamp":1611762819517}]"
复制代码

作者: zths    时间: 2021-1-28 17:02
本帖最后由 zths 于 2021-1-28 17:13 编辑

你这他不是游戏存档啊。。。
数据根本对不上。。
你这应该是 GlobalInfo。。



真正的存档数据是在 DataManager.saveGameWithoutRescue 里得出的。
第一行 var json = JsonEx.stringify(this.makeSaveContents());
这个json是存档

到saveToLocalFile这里的话 如果id存档不是0 那才是存档。。。
0是GlobalInfo
小于0是config。
作者: 神风炽羽    时间: 2021-1-28 17:51
zths 发表于 2021-1-28 17:02
你这他不是游戏存档啊。。。
数据根本对不上。。
你这应该是 GlobalInfo。。

我的存档机制有问题,感谢指出,已经搞定,已经将你设置为最佳答案,




欢迎光临 Project1 (https://rpg.blue/) Powered by Discuz! X3.1