Saving Stats

Started by
5 comments, last by haegarr 12 years, 10 months ago
Hello Gamedev.net, well, I'm trying to start a text-based game, and yes, i'm a begginer in C++, I have experience in PAWN, LUA and AutoIT.
I want to save the stats of the player, it can be in a .txt file, in a .ini file, or any other type of file.
I've seen this, but it will re-write the file, any suggestions on a efficient system for saving .txt (or a library for saving ini files, I don't have experience in installing libraries, but I would love to start doing so).
Advertisement
What's wrong with rewriting the file?

You can pass the ios::trunc flag for the second parameter (mode) to truncate the file (clear it out) and start from the beginning.

Or you could pass the ios::ate flag to start writing at the end (e.g. if you want to save multiple "saves" in one file or something). But then you'd have to figure out how to select the correct one to load later on.

You can also use the read() and write() methods of fstream if you want to read or write only parts of an existing file, but that is more complicated (you have to work out your file format exactly so you don't accidentally corrupt it). I'd save that one for a bit later.
--- krez ([email="krez_AT_optonline_DOT_net"]krez_AT_optonline_DOT_net[/email])

What's wrong with rewriting the file?

You can pass the ios::trunc flag for the second parameter (mode) to truncate the file (clear it out) and start from the beginning.

Or you could pass the ios::ate flag to start writing at the end (e.g. if you want to save multiple "saves" in one file or something). But then you'd have to figure out how to select the correct one to load later on.

You can also use the read() and write() methods of fstream if you want to read or write only parts of an existing file, but that is more complicated (you have to work out your file format exactly so you don't accidentally corrupt it). I'd save that one for a bit later.


Agreed,

There is no problem with writing over your current save of stats. You can also use a hacky method if you want and update pipelines.
EX:

|stat1|stat2|stat3|

Using this method would allow you to drop information into buckets if you want. I would reccomend though finding a way to secure it more should you choose to release the game for public use mainly to stop people from tampering with the data, and possible causing your engine to stop working because a stat pipeline doesn't work.

-Mayple
I usually just give my 2 cents, but since most of the people I meet are stubborn I give a 1$ so my advice isn't lost via exchange rate.


[quote name='krez' timestamp='1307686442' post='4821596']
What's wrong with rewriting the file?

You can pass the ios::trunc flag for the second parameter (mode) to truncate the file (clear it out) and start from the beginning.

Or you could pass the ios::ate flag to start writing at the end (e.g. if you want to save multiple "saves" in one file or something). But then you'd have to figure out how to select the correct one to load later on.

You can also use the read() and write() methods of fstream if you want to read or write only parts of an existing file, but that is more complicated (you have to work out your file format exactly so you don't accidentally corrupt it). I'd save that one for a bit later.


Agreed,

There is no problem with writing over your current save of stats. You can also use a hacky method if you want and update pipelines.
EX:

|stat1|stat2|stat3|

Using this method would allow you to drop information into buckets if you want. I would reccomend though finding a way to secure it more should you choose to release the game for public use mainly to stop people from tampering with the data, and possible causing your engine to stop working because a stat pipeline doesn't work.

-Mayple
[/quote]

Thanks for your responses, and again, sorry for my low skills :) I do have a gap in C++ when I tried to learn,
anyways, after doing some investigation I figured out that making a Save() and Load() function, and write the variables on the Load, and Save the variables in the Save() does the job, but is there any encoding possibility to avoid "hacking" the stats?

..., but is there any encoding possibility to avoid "hacking" the stats?

The term "encryption" means to protect data. Be aware that any encryption you may apply can be cracked by somebody. If knowing the decrypted data is it worth, then somebody will hack it.

The only thing you can do is to keep away the average Joe. For this purpose just saving the data in binary form already helps. Saving a varying amount (e.g. more data when the game progresses), using a varying order for blocks of data, and interspersing some arbitrary and some random data may help.

However, generally I would not invest too much effort into such an undertaking until there is a good reason. If someone wants to spoil the own party ... let him/her go. Just my 2 Cents.

[quote name='Deskoft' timestamp='1307714513' post='4821708']
..., but is there any encoding possibility to avoid "hacking" the stats?

The term "encryption" means to protect data. Be aware that any encryption you may apply can be cracked by somebody. If knowing the decrypted data is it worth, then somebody will hack it.

The only thing you can do is to keep away the average Joe. For this purpose just saving the data in binary form already helps. Saving a varying amount (e.g. more data when the game progresses), using a varying order for blocks of data, and interspersing some arbitrary and some random data may help.

However, generally I would not invest too much effort into such an undertaking until there is a good reason. If someone wants to spoil the own party ... let him/her go. Just my 2 Cents.
[/quote]

Thanks for your answer, it has made me think about it.

I am recieving some errors inside my code, I made the two functions Load and Save in the class "PLAYER".

1>------ Build started: Project: Medieval Life, Configuration: Debug Win32 ------
1> PlayerHouse.cpp
1> Generating Code...
1>c:\archivos de programa\lawedan computer, inc\medieval life\medieval life\player.h(64): warning C4715: 'PLAYER::SavePlayer' : not all control paths return a value
1> Compiling...
1> main.cpp
1> Generating Code...
1>c:\archivos de programa\lawedan computer, inc\medieval life\medieval life\player.h(64): warning C4715: 'PLAYER::SavePlayer' : not all control paths return a value
1>PlayerHouse.obj : error LNK2005: "public: void __thiscall PLAYER::SetHealth(int)" (?SetHealth@PLAYER@@QAEXH@Z) already defined in main.obj
1>PlayerHouse.obj : error LNK2005: "public: int __thiscall PLAYER::GetHealth(void)" (?GetHealth@PLAYER@@QAEHXZ) already defined in main.obj
1>PlayerHouse.obj : error LNK2005: "public: void __thiscall PLAYER::SetMoney(int)" (?SetMoney@PLAYER@@QAEXH@Z) already defined in main.obj
1>PlayerHouse.obj : error LNK2005: "public: int __thiscall PLAYER::GetMoney(void)" (?GetMoney@PLAYER@@QAEHXZ) already defined in main.obj
1>PlayerHouse.obj : error LNK2005: "public: void __thiscall PLAYER::SetAge(int)" (?SetAge@PLAYER@@QAEXH@Z) already defined in main.obj
1>PlayerHouse.obj : error LNK2005: "public: int __thiscall PLAYER::GetAge(void)" (?GetAge@PLAYER@@QAEHXZ) already defined in main.obj
1>PlayerHouse.obj : error LNK2005: "public: bool __thiscall PLAYER::SavePlayer(void)" (?SavePlayer@PLAYER@@QAE_NXZ) already defined in main.obj
1>PlayerHouse.obj : error LNK2005: "public: bool __thiscall PLAYER::LoadPlayer(void)" (?LoadPlayer@PLAYER@@QAE_NXZ) already defined in main.obj
1>PlayerHouse.obj : error LNK2005: "void __cdecl Wait(void)" (?Wait@@YAXXZ) already defined in main.obj
1>PlayerHouse.obj : error LNK2005: "void __cdecl ClearScreen(void)" (?ClearScreen@@YAXXZ) already defined in main.obj
1>PlayerHouse.obj : error LNK2005: "public: int __thiscall CHEST::GetChestMoney(void)" (?GetChestMoney@CHEST@@QAEHXZ) already defined in main.obj
1>PlayerHouse.obj : error LNK2005: "char * password" (?password@@3PADA) already defined in main.obj
1>PlayerHouse.obj : error LNK2005: "int option" (?option@@3HA) already defined in main.obj
1>C:\Archivos de programa\Lawedan Computer, Inc\Medieval Life\Debug\Medieval Life.exe : fatal error LNK1169: one or more multiply defined symbols found
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
This


warning C4715: 'PLAYER::SavePlayer' : not all control paths return a value

means that there is a function that is declared to return something (i.e. isn't void) but at least one way through the function exists where not a single return statement is touched.

Something like this


PlayerHouse.obj : error LNK2005: "public: void __thiscall PLAYER::SetAge(int)" (?SetAge@PLAYER@@QAEXH@Z) already defined in main.obj

means that a method, PLAYER::SetAge(int) in this case, is compiled twice or more, or that 2 or more different methods of the same name are compiled. However, both scenarios are not allowed. Make sure that distinct routines have distinct signatures, i.e. are different in either their name, count of arguments, or argument types. And make sure that the definition of a routine (i.e. where the routine has a body enclosed with the curly braces) is compiled only once (err, okay ... if it is not inlined).

This topic is closed to new replies.

Advertisement