尝试使用序列化从控制台应用程序 (c#) 保存和加载变量(已回答)
trying to use serializing to save and load variables from console app (c#) ( answered)
Chris 的评论回答了这个问题。
我也必须做 equipedShield = stateVariables.equipedShield; equipedWeapon = stateVariables.equipedWeapon; healthPotionCount = stateVariables.healthPotionCount; playerExpEarned = stateVariables.playerExpEarned;
使其加载,谢谢克里斯。
我的问题是我可以使用变量名来存储值吗?还是必须是实际数字?
我在这里找到了 post C# - Saving Console Game Values
我选择进行序列化,而是保存在 mydocuments 上,这样我才能真正找到 txt 文件(我做到了)
在主加载:
string folder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
folder = Path.Combine(folder, "RNGgameSaveFiles");
Directory.CreateDirectory(folder);
string dataFile = Path.Combine(folder, "RNGgameSaveFileV1");
if (File.Exists(dataFile))
{
using (Stream stateStream = File.OpenRead(dataFile))
{
BinaryFormatter deserializer = new BinaryFormatter();
stateVariables = (AppState)deserializer.Deserialize(stateStream);
}
}
保存起来好像还好,加载的时候全是空的
赞:
static int roundsPlayed;
static decimal money = 0;
static decimal totalMoneyEarned;
static double totalScore;
static int totalKills;
static int totalLosses;
另存为:
stateVariables.totalLosses = totalLosses;
stateVariables.totalKills = totalKills;
stateVariables.totalScore = totalScore;
stateVariables.totalMoneyEarned = totalMoneyEarned;
stateVariables.money = money;
stateVariables.roundsPlayed = roundsPlayed;
或者是什么让我把事情搞砸了?
我让 class 调用了 appstate:
namespace rngGame
{
[Serializable()]
public class AppState
{
public int roundsPlayed { get; set; }
public decimal money { get; set; }
public decimal totalMoneyEarned { get; set; }
public double totalScore { get; set; }
public int totalKills { get; set; }
public int totalLosses { get; set; }
public bool owned01 { get; set; }
public bool owned02 { get; set; }
public bool owned03 { get; set; }
public bool owneds01 { get; set; }
public bool owneds02 { get; set; }
public bool owneds03 { get; set; }
public double healthPotionCount { get; set; }
public int equipedWeapon { get; set; }
public int equipedShield { get; set; }
public int playerLevel { get; set; }
public int playerExpEarned { get; set; }
保存退出
(这部分确实有效,它确实创建了文件):
static void closeAndSave()
{
string folder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
folder = Path.Combine(folder, "RNGgameSaveFiles");
Directory.CreateDirectory(folder);
string dataFile = Path.Combine(folder, "RNGgameSaveFileV1");
using (Stream stateStream = File.Create(dataFile))
{
BinaryFormatter serializer = new BinaryFormatter();
serializer.Serialize(stateStream, stateVariables);
}
}
您有一些代码将值放入 stateVariables 并对其进行序列化,但您也必须执行相反的操作 - 即从中读取值并将静态成员设置为这些值。
Chris 的评论回答了这个问题。
我也必须做 equipedShield = stateVariables.equipedShield; equipedWeapon = stateVariables.equipedWeapon; healthPotionCount = stateVariables.healthPotionCount; playerExpEarned = stateVariables.playerExpEarned;
使其加载,谢谢克里斯。
我的问题是我可以使用变量名来存储值吗?还是必须是实际数字?
我在这里找到了 post C# - Saving Console Game Values 我选择进行序列化,而是保存在 mydocuments 上,这样我才能真正找到 txt 文件(我做到了) 在主加载:
string folder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
folder = Path.Combine(folder, "RNGgameSaveFiles");
Directory.CreateDirectory(folder);
string dataFile = Path.Combine(folder, "RNGgameSaveFileV1");
if (File.Exists(dataFile))
{
using (Stream stateStream = File.OpenRead(dataFile))
{
BinaryFormatter deserializer = new BinaryFormatter();
stateVariables = (AppState)deserializer.Deserialize(stateStream);
}
}
保存起来好像还好,加载的时候全是空的
赞:
static int roundsPlayed;
static decimal money = 0;
static decimal totalMoneyEarned;
static double totalScore;
static int totalKills;
static int totalLosses;
另存为:
stateVariables.totalLosses = totalLosses;
stateVariables.totalKills = totalKills;
stateVariables.totalScore = totalScore;
stateVariables.totalMoneyEarned = totalMoneyEarned;
stateVariables.money = money;
stateVariables.roundsPlayed = roundsPlayed;
或者是什么让我把事情搞砸了?
我让 class 调用了 appstate:
namespace rngGame
{
[Serializable()]
public class AppState
{
public int roundsPlayed { get; set; }
public decimal money { get; set; }
public decimal totalMoneyEarned { get; set; }
public double totalScore { get; set; }
public int totalKills { get; set; }
public int totalLosses { get; set; }
public bool owned01 { get; set; }
public bool owned02 { get; set; }
public bool owned03 { get; set; }
public bool owneds01 { get; set; }
public bool owneds02 { get; set; }
public bool owneds03 { get; set; }
public double healthPotionCount { get; set; }
public int equipedWeapon { get; set; }
public int equipedShield { get; set; }
public int playerLevel { get; set; }
public int playerExpEarned { get; set; }
保存退出 (这部分确实有效,它确实创建了文件):
static void closeAndSave()
{
string folder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
folder = Path.Combine(folder, "RNGgameSaveFiles");
Directory.CreateDirectory(folder);
string dataFile = Path.Combine(folder, "RNGgameSaveFileV1");
using (Stream stateStream = File.Create(dataFile))
{
BinaryFormatter serializer = new BinaryFormatter();
serializer.Serialize(stateStream, stateVariables);
}
}
您有一些代码将值放入 stateVariables 并对其进行序列化,但您也必须执行相反的操作 - 即从中读取值并将静态成员设置为这些值。