unity学习(50)——服务器三次注册限制以及数据库化角色信息5--角色信息数据库化收尾

上一节内容结束后确实可以写入文件了,但还有两个问题:

1.一个是players.txt中,每次重启服务器,当注册新账号创建角色时,players.txt之前内容都会清空。

2.players.txt之前已经注册3次的账号,新注册的角色信息不会写入players.txt,猜测与userPlayerIds.txt中的残留内容有关。但userPlayerIds.txt中的内容感觉不全!

先解决第一个问题:上边 userPlayerIds.txt的内容是可以成功读出来并且转化的players.txt中的内容转化失败,所以此时就是空的

就是json转换结构体上出现了问题,账号那可以的,换到这就出问题了!

对比来看AccountModel和PlayerModel基本上大同小异

Newtonsoft.Json.JsonSerializationException:"Unable to find a constructor to use for type GameServer.model.PlayerModel. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute. Path '431b0501-457f-46ba-91dd-353c8a3c8c0c.id', line 3, position 9."

Newtonsoft。Json。JsonSerializationException:"找不到用于类型GameServer.model.PlayerModel的构造函数。类应该有一个默认构造函数、一个带参数的构造函数或一个用JsonConstructor属性标记的构造函数。路径'431b0501-457f-46ba-91dd-353c8a3c8c8c.id',第3行,位置9。"

就是****PlayerModel类没有构造函数!

cs 复制代码
namespace GameServer.model
{
  internal class PlayerModel
  {
    public string id { get; set; }

    public string name { get; set; }

    public int job { get; set; }

    public int level { get; set; }

    public int exp { get; set; }

    public int atk { get; set; }

    public int def { get; set; }

    public int hp { get; set; }

    public int maxHp { get; set; }

    public Vector3 point { get; set; }

    public Vector4 rotation { get; set; }

    public int map { get; set; }

    public PlayerModel(PlayerModel model)
    {
      this.job = model.job;
      this.level = model.level;
      this.exp = model.exp;
      this.atk = model.atk;
      this.def = model.def;
      this.hp = model.hp;
      this.maxHp = model.maxHp;
    }

    public PlayerModel(
      int job,
      int level,
      int exp,
      int atk,
      int def,
      int map,
      int hp,
      int maxHp)
    {
      this.job = job;
      this.level = level;
      this.exp = exp;
      this.atk = atk;
      this.def = def;
      this.map = map;
      this.hp = hp;
      this.maxHp = maxHp;
    }
  }
}

很明显是有构造函数的,还有2个,没想到再加一个

public PlayerModel()

{ }

的确是解决了!:

相关推荐
李小星同志32 分钟前
高级算法设计与分析 学习笔记6 B树
笔记·学习
霜晨月c44 分钟前
MFC 使用细节
笔记·学习·mfc
小江湖19941 小时前
元数据保护者,Caesium压缩不丢重要信息
运维·学习·软件需求·改行学it
dot.Net安全矩阵1 小时前
.NET内网实战:通过命令行解密Web.config
前端·学习·安全·web安全·矩阵·.net
微刻时光2 小时前
Redis集群知识及实战
数据库·redis·笔记·学习·程序人生·缓存
潮汐退涨月冷风霜4 小时前
机器学习之非监督学习(四)K-means 聚类算法
学习·算法·机器学习
GoppViper4 小时前
golang学习笔记29——golang 中如何将 GitHub 最新提交的版本设置为 v1.0.0
笔记·git·后端·学习·golang·github·源代码管理
羊小猪~~4 小时前
深度学习基础案例5--VGG16人脸识别(体验学习的痛苦与乐趣)
人工智能·python·深度学习·学习·算法·机器学习·cnn
Charles Ray5 小时前
C++学习笔记 —— 内存分配 new
c++·笔记·学习
我要吐泡泡了哦6 小时前
GAMES104:15 游戏引擎的玩法系统基础-学习笔记
笔记·学习·游戏引擎