使用ObjectInputStream从文件加载保存的游戏 [英] Load saved game from file using ObjectInputStream

查看:117
本文介绍了使用ObjectInputStream从文件加载保存的游戏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在试图找到一种方法来处理将游戏状态保存到文件(游戏中常见的保存/加载游戏功能)。到目前为止,ObjectOutput / InputStream似乎最有希望。


但是,我保存的对象包含许多其他对象,而这些对象又包含其他对象。所有对象都实现Serializable(我已经给出了所有serialVersionUID = 1,不确定它是否重要)。


它似乎正确地写了对象但加载它有点像问题。


这是我正在使用的功能:

I''m trying to figure a good way to handle saving game state to file (common save/load game function in games). ObjectOutput/InputStream seems most promising so far.

However,the object I''m saving contains lots of other objects which in turn contain other objects. All objects implement Serializable (I''ve given them all serialVersionUID = 1, not sure whether it matters).

It seems to write the object properly but loading it is a bit of a problem.

This is the function I''m using:

展开 | 选择 | 换行 | 行号

推荐答案

@Humakt


Care to el对问题有所了解?它不是从Stream中读取对象吗?确保Game对象的所有成员对象都是Serializable或使它们成为瞬态。


顺便说一下,如果您使用的IDE最有可能为您生成VersionID值。


亲切的问候,


Jos
@Humakt
Care to elaborate a bit on the ''bit of a problem''? Doesn''t it read the object from the Stream? Make sure that all your member objects of a Game object are Serializable or make them transient.

btw, if you''re using an IDE most likely it can generate a VersionID value for you.

kind regards,

Jos


@JOSAH


嗯,我认为问题在于文件中有多个对象,但它们都是这个游戏对象的一部分(单向或其他)。我尝试在循环中读取对象,但它也返回null。


我在第二个句子中更正了第一个帖子。


这是函数我用来写对象:

@JosAH
Well, I think the problem is that there are multiple objects in the file, but they are all (one way or other) part of this game object. I tried reading object in loop as well but it also returned null.

I correct the first post in sec.

This is the function I use to write the object:

展开 | 选择 | Wrap | 行号


@Humakt

对于调试,使所有成员对象成为瞬态并再试一次;你应该能够写/读一个(几乎)空的Game对象。接下来再次添加成员对象并为其提供另一个SerialVersionUID编号,然后重试。你的代码看起来很合理。


亲切的问候,


Jos
@Humakt
For debugging make all your member objects transient and try again; you should be able to write/read an (almost) empty Game object. Next add the member objects again and give them another SerialVersionUID number and try again. Your code looks reasonable.

kind regards,

Jos


这篇关于使用ObjectInputStream从文件加载保存的游戏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆