How to make game save code

Hey, I was just wondering how would you make a game save code. Something like a long set of numbers or something like 7182737124 which each number means one thing, for an example 7 would equal something like x amount of a variable. The player can then input his/her game code and it will set all the variables to their corresponding spot in the number string.

Also, would it be easier to do 71 82 73 and just have spaces or dashes in between? (71-82-73) Then it would just times the values. I know how I would make the game code I just don't have an idea on how to decipher the game save code that is what I need help on. My idea on making the game code would be just having the number of that variable, then add a "-" then add the second variable to that string.

Thanks!
Last edited on
typically you would save in binary. Is there a reason you want to use text? Text is harder to work with, and needs a parser and formatting and for large programs can be quite a chore to design the format, and it allows the user to hack on it that much easier (there are pros and cons to this depending on if you care about cheaters (online games) or not (single player) ).

How would you use binary to make and decipher a game save code? No reason to point out why I wanted to use text, I didn't think of using binary as one of my options.
Binary files are called that, but they are not binary as in 1s and 0s. Its just a disk format that allows fixed width storage of data, so that 12, 1, and 123 can all fit in 1 byte instead of 2, 1, and 3 bytes respectively which is messy.

You just read and write it, ... the how depends on your data. It sounds like your data is integers, so a POD struct might be all you need, then it is literally

file.read(struct*, sizeof(struct))
and the reverse write statement.

seems like you just need an in-code lookup table of what values mean to translate to and from. A vector or even an array can do this easily.

I hope this is making sense.... ?
So what you are saying is that I need a secondary file, that has the game code or the data in binary (binary file), and then I have an array or vector that will "translate" what each number will equal. Is that correct? My data will be integers, most of them will, the player will just have to re-input their name and continue.
Last edited on
just 1 file for each saved game is sufficient.

Yes, a simple lookup table of translated = table[value from file] when reading the file or x = index in table for writing the file hopefully will work. If not you can do it other ways, like a map or something.



IMO, text is superior for reasons of transparency.
Text is easier to modify and understand, does not suffer from portability issues inherent with e.g., byte order, and isn't as sensitive to data size.

ESR writes about this in The Art of Unix Programming:
http://www.catb.org/esr/writings/taoup/html/ch05s01.html
The whole book is worth reading, if you have time.

In this vein, my suggestion is to simply use a whitespace delimited text-file.
Perhaps something like this:

character.sav
health    100
location  plains
equipment bandages
equipment knife
equipment food
equipment water
Thanks,
The only problem is that I don't want players changing for example money to like 2.5 million or something like that. Would it be possible to make the file holding all the stuff not be able to be opened or sense if it is edited other other than the game. If it senses its been edited then it won't open. Is there any way to do that? How would you make it so it can read what you have in the save file?

*EDIT*
I was thinking of encrypting the file then decrypting the file as the game loads it. How would I be able to read the file after it is decrypted.
Last edited on
closed account (E0p9LyTq)
Serialization - Boost
http://www.boost.org/doc/libs/1_64_0/libs/serialization/doc/index.html

Serialization - Tutorial - Boost
http://www.boost.org/doc/libs/1_60_0/libs/serialization/doc/tutorial.html
I don't want players changing for example money to like 2.5 million or something like that
Why not?

If there are real security concerns (e.g., this is a multiplayer game) then such attempts at control are perhaps warranted. Otherwise, you shouldn't attempt to control the user. Still, you can (and should) perform your validation of user actions on the server without sacrificing transparency. This is roughly how most anti-cheating systems work.

Would it be possible to make the file holding all the stuff not be able to be opened or sense if it is edited other other than the game.
Not easily. Using a binary format instead of a text format simply means a curious user needs a hex editor instead of a text editor. More effective techniques for control can be borrowed from DRM implementations. For instance, you can encrypt the user's content and require a keypair from a first-party server to access it.

This document contains an overview of how such a process could work:
https://knowledge.kaltura.com/universal-drm-udrm-technical-specification
FWIW, my personal opinion is that these sorts of controls are terribly unethical violations of the rights to information and privacy. But the decision is up to you.

Other sorts of obfuscation are quite possible. Create dummy files, make the format entirely non-intuitive. Take checksums, and write your text files in Yiddish, backwards. Yes, you can embed an encryption key in the executable.

Even with such measures, your program runs on the user's computer. If the user has sufficient expertise, you won't be able to stop them from tinkering. The idea is that the user (in theory) entirely controls the environment in which the program exists. The best you can do is make it not worth their time to break.

How would you make it so it can read what you have in the save file?
Given the trivial format above, you could do something like this:
1
2
3
4
5
6
7
8
9
10
std::string directive, value;
while (file >> directive >> value) {
  if (directive == "equipment") // add equipment to inventory by name
    add_item(value);
  else if (directive == "health") // set health to value
    health_remaining = std::stoi(value);
  else
    std::cerr << "unrecognized directive "
              << std::quoted(directive) << '\n';
}


You can find plenty of examples regarding how to read text files with C++. But the standard streams can be quite awkward.
For parsing more complicated formats, Boost.Spirit has become my preferred choice. Similarly, if you choose to use a serialization library, Boost.Serialization is the way to go.
Thanks, on how to read the file :)
I've figured out a way to do it in just the game file I am wondering how could I make this code a bit more simpler and not so beefy. BTW, all the game save codes will be decrypted and this is just a sample.
Example code: 987|877|981|

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Example program
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main() {
    
    int gold = 0;
    int hp = 0;
    int mana = 0;
    int x = 0;
    int arr [3] = {0,0,0};
    string golds;
    string hps;
    string manas;
    string input = "";
    string code = "";
    
    cin >> input;
    code = input;
    
    compile:
    goldr:
    if (code[x] == '|') { // If the character is a pipe
        x=x+1; // Go to next variable
        goto hpr;
    } else {
        golds += code[x]; // If not add the variables
        x=x+1; // Go to next charcter
        goto goldr;
    }
    hpr:
    if (code[x] == '|') { // Same here
        x=x+1;
        goto manar;
    } else {
        hps += code[x];
        x=x+1;
        goto hpr;
    }
    manar:
    if (code[x] == '|') { // Same here
        x=x+1;
        goto menue;
    } else {
        manas += code[x];
        x=x+1;
        goto manar;
    }
    
    menue:
    
    int value1 = atoi(golds.c_str());
    int value2 = atoi(hps.c_str());
    int value3 = atoi(manas.c_str());
    arr[1] = value1;
    arr[2] = value2;
    arr[3] = value3;
    
    cout << "Arr[1] " << arr[1] << endl;
    cout << "Arr[2] " << arr[2] << endl;
    cout << "Arr[3] " << arr[3] << endl;
    gold = arr[1];
    hp = arr[2];
    mana = arr[3];
    
    cout << endl;
    cout << "Gold: " << gold << endl; // Cout all the values
    cout <<  "HP: " << hp << endl;
    cout << "Mana: " << mana << endl;
    
    return 0;
}
Last edited on
for a low-security program like this, xor encryption will work regardless of the format (binary or text aside).

here is the quick way..

char *filedata;
srand(encryptionkeynumber);
for(... every byte in filedata)
filedata[i] ^= rand();

the exact same code will decrypt. So long as key is a constant! This is hackable, but its a lot more trouble than most people will go to.

The stream from this is however binary, even if the data itself is textual format. Otherwise it will corrupt your data.
Last edited on
Well thanks all!
Thanks,
Handge
Last edited on
Topic archived. No new replies allowed.