设为首页收藏本站|繁體中文

Project1

 找回密码
 注册会员
搜索
查看: 4512|回复: 5
打印 上一主题 下一主题

[有事请教] 如何使用json文件(或者字符串)作为存档开始游戏

[复制链接]

Lv3.寻梦者

梦石
0
星屑
3643
在线时间
314 小时
注册时间
2019-8-9
帖子
206

极短22参与

跳转到指定楼层
1
发表于 2021-1-28 00:19:53 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
124星屑

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

我希望实现的是点击继续游戏按钮后使用我提供的字符串,也就是json文件作为游戏的存档,继续游戏,请问这个应该怎么写?

最佳答案

<a herf="#">monster-phone:restart almost completed<a>
old way end new way start nothing care whenI fall in chair

Lv3.寻梦者

梦石
0
星屑
3461
在线时间
292 小时
注册时间
2020-1-27
帖子
190
2
发表于 2021-1-28 00:19:54 | 只看该作者
  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字串");
复制代码

评分

参与人数 2+2 收起 理由
神风炽羽 + 1 挺好的,可惜未解决
白嫩白嫩的 + 1 z大佬一出手就知有没有~~

查看全部评分

回复

使用道具 举报

Lv3.寻梦者

梦石
0
星屑
3643
在线时间
314 小时
注册时间
2019-8-9
帖子
206

极短22参与

3
 楼主| 发表于 2021-1-28 12:35:10 | 只看该作者

遗憾,还是没能解决,可否再看一下,以下为我写的代码
  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}]"
复制代码
<a herf="#">monster-phone:restart almost completed<a>
old way end new way start nothing care whenI fall in chair
回复

使用道具 举报

Lv3.寻梦者

梦石
0
星屑
3461
在线时间
292 小时
注册时间
2020-1-27
帖子
190
4
发表于 2021-1-28 17:02:36 | 只看该作者
本帖最后由 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:57
回复

使用道具 举报

Lv3.寻梦者

梦石
0
星屑
3643
在线时间
314 小时
注册时间
2019-8-9
帖子
206

极短22参与

5
 楼主| 发表于 2021-1-28 17:51:11 | 只看该作者
zths 发表于 2021-1-28 17:02
你这他不是游戏存档啊。。。
数据根本对不上。。
你这应该是 GlobalInfo。。

我的存档机制有问题,感谢指出,已经搞定,已经将你设置为最佳答案,
<a herf="#">monster-phone:restart almost completed<a>
old way end new way start nothing care whenI fall in chair
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

拿上你的纸笔,建造一个属于你的梦想世界,加入吧。
 注册会员
找回密码

站长信箱:[email protected]|手机版|小黑屋|无图版|Project1游戏制作

GMT+8, 2024-5-10 13:34

Powered by Discuz! X3.1

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表