如何在Android游戏中保存数据并访问它?

时间:2023-01-27 10:57:55

For a project I am making a simple RPG turn based game in Android. At the moment I'm struggling to think how I going to implement the saving and accessing of data. The game being an RPG means that I want to save lots of data like stats, items, creatures and characters. Can I do this using SQlite or would there be a better way?

对于一个项目,我正在Android中制作一个简单的RPG回合制游戏。目前我正在努力思考如何实现数据的保存和访问。作为RPG的游戏意味着我想要保存大量数据,如统计数据,项目,生物和角色。我可以使用SQlite做到这一点还是会有更好的方法?

3 个解决方案

#1


1  

It sounds like the built-in SQLlite would be very appropriate. Seems like you've already laid out in your question, several potential SQL tables. For example, each character could be stored as individual rows of a table, where the columns describe the attributes of each character, many of which may link to rows of other tables. Same goes for users, items etc.

听起来内置的SQLlite非常合适。好像你已经在你的问题中列出了几个潜在的SQL表。例如,每个字符可以存储为表的各个行,其中列描述每个字符的属性,其中许多字符可以链接到其他表的行。对于用户,物品等也是如此。

It can be a bit intimidating getting started with SQLlite if you've little familiarity with it, but once you've worked through a couple of tables and start to form a code base, you'll find yourself reusing the same/similar queries over and over.

如果你对它很熟悉,可能会对使用SQLlite开始有点吓人,但是一旦你完成了几个表并开始构建代码库,你会发现自己重复使用相同/类似的查询并结束。

I'm not a database or SQL expert, but from my own experience, try to form as clear an idea as possible of how the data should be structured - all the tables and associations etc - before writing any code. Then as the development proceeds and your initial ideas get revised, try to keep honestly questioning whether your existing data structures are still appropriate, and make any necessary changes as soon as you identify a different data model that makes more sense.

我不是数据库或SQL专家,但根据我自己的经验,在编写任何代码之前,尝试尽可能清楚地形成数据应该如何构建 - 所有表和关联等。然后随着开发的进行以及您的初始想法的修改,请尽量诚实地质疑您现有的数据结构是否仍然合适,并在识别出更有意义的不同数据模型后立即进行必要的更改。

An alternative method would be store information in files, but then you'll have to invent not just the data structure but also the physical aspects of how that data gets stored within each file.

另一种方法是将信息存储在文件中,但是您不仅要创建数据结构,还要创建数据如何存储在每个文件中的物理方面。

#2


0  

You could do it doing SQLite database easily.

你可以轻松地做SQLite数据库。

I think this would be your best bet. You could also use Shared Preference to store stats if you wanted. I think SQLite fits this scenario better.

我认为这是你最好的选择。如果需要,您还可以使用共享首选项来存储统计信息。我认为SQLite更适合这种情况。

#3


0  

I would go for some kind of file storage. You would probably want to access saved values as objects and save them also as objects - for this you can use some standard marshalling/unmarshalling framework - there are some good for object <-> xml mapping

我会去某种文件存储。您可能希望将已保存的值作为对象访问并将它们另存为对象 - 为此您可以使用一些标准的编组/解组框架 - 对于对象< - > xml映射有一些好处

You probably wouldn't use much of a find operations - and SQL is just good at finding things. You're best guess would be for what you know the best.

您可能不会使用很多查找操作 - 而SQL只是擅长查找内容。你最好的猜测是你最了解的。

#1


1  

It sounds like the built-in SQLlite would be very appropriate. Seems like you've already laid out in your question, several potential SQL tables. For example, each character could be stored as individual rows of a table, where the columns describe the attributes of each character, many of which may link to rows of other tables. Same goes for users, items etc.

听起来内置的SQLlite非常合适。好像你已经在你的问题中列出了几个潜在的SQL表。例如,每个字符可以存储为表的各个行,其中列描述每个字符的属性,其中许多字符可以链接到其他表的行。对于用户,物品等也是如此。

It can be a bit intimidating getting started with SQLlite if you've little familiarity with it, but once you've worked through a couple of tables and start to form a code base, you'll find yourself reusing the same/similar queries over and over.

如果你对它很熟悉,可能会对使用SQLlite开始有点吓人,但是一旦你完成了几个表并开始构建代码库,你会发现自己重复使用相同/类似的查询并结束。

I'm not a database or SQL expert, but from my own experience, try to form as clear an idea as possible of how the data should be structured - all the tables and associations etc - before writing any code. Then as the development proceeds and your initial ideas get revised, try to keep honestly questioning whether your existing data structures are still appropriate, and make any necessary changes as soon as you identify a different data model that makes more sense.

我不是数据库或SQL专家,但根据我自己的经验,在编写任何代码之前,尝试尽可能清楚地形成数据应该如何构建 - 所有表和关联等。然后随着开发的进行以及您的初始想法的修改,请尽量诚实地质疑您现有的数据结构是否仍然合适,并在识别出更有意义的不同数据模型后立即进行必要的更改。

An alternative method would be store information in files, but then you'll have to invent not just the data structure but also the physical aspects of how that data gets stored within each file.

另一种方法是将信息存储在文件中,但是您不仅要创建数据结构,还要创建数据如何存储在每个文件中的物理方面。

#2


0  

You could do it doing SQLite database easily.

你可以轻松地做SQLite数据库。

I think this would be your best bet. You could also use Shared Preference to store stats if you wanted. I think SQLite fits this scenario better.

我认为这是你最好的选择。如果需要,您还可以使用共享首选项来存储统计信息。我认为SQLite更适合这种情况。

#3


0  

I would go for some kind of file storage. You would probably want to access saved values as objects and save them also as objects - for this you can use some standard marshalling/unmarshalling framework - there are some good for object <-> xml mapping

我会去某种文件存储。您可能希望将已保存的值作为对象访问并将它们另存为对象 - 为此您可以使用一些标准的编组/解组框架 - 对于对象< - > xml映射有一些好处

You probably wouldn't use much of a find operations - and SQL is just good at finding things. You're best guess would be for what you know the best.

您可能不会使用很多查找操作 - 而SQL只是擅长查找内容。你最好的猜测是你最了解的。