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()

{ }

的确是解决了!:

相关推荐
星火开发设计6 小时前
类型别名 typedef:让复杂类型更简洁
开发语言·c++·学习·算法·函数·知识
Gorgous—l6 小时前
数据结构算法学习:LeetCode热题100-多维动态规划篇(不同路径、最小路径和、最长回文子串、最长公共子序列、编辑距离)
数据结构·学习·算法
Hello_Embed7 小时前
libmodbus 移植 STM32(基础篇)
笔记·stm32·单片机·学习·modbus
●VON8 小时前
Flutter for OpenHarmony 21天训练营 Day03 总结:从学习到输出,迈出原创第一步
学习·flutter·openharmony·布局·技术
香芋Yu8 小时前
【大模型教程——第四部分:大模型应用开发】第1章:提示工程与上下文学习 (Prompt Engineering & ICL)
学习·prompt
LYS_06188 小时前
寒假学习10(HAL库1+模数电10)
学习
runningshark8 小时前
【项目】示波器学习与制作
学习
€8118 小时前
Java入门级教程24——Vert.x的学习
java·开发语言·学习·thymeleaf·数据库操作·vert.x的路由处理机制·datadex实战
自可乐9 小时前
n8n全面学习教程:从入门到精通的自动化工作流引擎实践指南
运维·人工智能·学习·自动化