reading Weapons from text file

Hi guys,
This is my first time posting a topic in a forum and I'm pretty new with programming so there might be some stuff I left out that should be mentioned. So do let me know if I did left out anything that you guys would need to know =)

I'm doing a small adventure game. So far I've manage to do all the character, monsters, map and stuff. Now I'm thinking of adding an equipment for the game so I can change swords for my character. I'm not too sure if this is the best way but I am thinking of reading the values like swordName and swordDamage from a text file.

So I'm just wondering if this is the best way to do it and if it is, if I want to include just 2 weapons for now, do I write all the values in a single text file?
You could use an XML type thing e.g:

<damage>
10
</damage>
<name>
cool sword
</name>

Though you'd have to write a parser it wouldn't be hard at all. It'll probably be better for organisation purposes if you had each weapon in a different file.
closed account (zb0S216C)
kfong wrote:
"This is my first time posting a topic in a forum and I'm pretty new with programming so there might be some stuff I left out that should be mentioned."

Welcome to the boards :) Don't be worried about missing information; there's an edit button.

kfong wrote:
"I'm not too sure if this is the best way but I am thinking of reading the values like swordName and swordDamage from a text file."

That's the way I recommend doing it. Hard-coding data into a program means recompiling the program every time a change is made. With files, it's a simple act of changing the file's data.

kfong wrote:
"if I want to include just 2 weapons for now, do I write all the values in a single text file?"

Yeah'p.

Wazzak
closed account (o3hC5Di1)
Purely out of interest - no thread-hijacking intended:

How would/could one go about doing this if you don't want the user to be able to change the "swordDamage" value?
Is it possible to save a sword object to a file for instance or would the object have to be created at program loading time?

Thanks,

NwN
closed account (zb0S216C)
NwN wrote:
"How would/could one go about doing this if you don't want the user to be able to change the "swordDamage" value?"

Before starting the game, you would extract the data from the file and place it inside a buffer of some sort. From there, you'd modify the values. Before exiting the program, you'd overwrite the old file in favour of the new one. For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
struct Weapon
{
    char *Name;
    int Damage;
};

int main()
{
    std::vector<Weapon> WeaponData;
    
    std::ifstream WeaponsFile("NameOfTheDataFile.Ext");
    
    Weapon Temp = {0};
    while(/*not at the end of the file*/)
    {
        WeaponsFile >> Temp.Name;
        WepaonsFile >> Temp.Damage;
        WeaponData.push_back(Temp);
    }
    
    // Alter the weapon data.
    Weapon.at(0).Name = "Modified Sword";
    Weapon.at(0).Damage = 20;
}

NwN wrote:
"Is it possible to save a sword object to a file for instance or would the object have to be created at program loading time?"

If the sword is created during run-time, and doesn't reside within the file, you'd have to create the sword during run-time then append the data to the file before exiting.

Wazzak
Last edited on
Framework:

ew @ char*. Anyway I'm not sure I understand what your'e getting at. How would overwriting the original file prevent someone from modifying it in the first place?

I think NwN is looking for a way to stop "cheaters" from changing the data files so they can make super strong items.

While there is no 100% fullproof way (a dedicated enough hacker can make changes regardless of how protected you make it), there are some common techniques:

1) Encrypt the data somehow so that modifying it is not straightforward.

2) Add redundancies (duplicate the same data several places) and fail to load if all of them do not sync up perfectly.

3) Keep a CRC or hash check of the file(s). If the user modifies them, the CRC will change, and if it doesn't match, you can fail to load.

4) Keep an online server that cannot be modified by users and load your data from there rather than keeping it locally on the target machine.



#4 is a little over the top and requires the user to have internet access, but is probably the most secure. Combinations of 1,2, and 3 are pretty commonplace.


And of course the flip side of this... is do you really want to stop the user from modding these files? As many gamers will tell you, game modding is a lot of fun and adds a lot of replay value to games. It might be more benefitial to make these data files easy to edit to encourage custom mods... rather than going through the extra work of protecting them.
Last edited on
closed account (o3hC5Di1)
Thanks for your quick responses Framework and Disch.

You were right about my intent Disch, so thanks for clarifying that out.

And of course the flip side of this... is do you really want to stop the user from modding these files? As many gamers will tell you, game modding is a lot of fun and adds a lot of replay value to games. It might be more benefitial to make these data files easy to edit to encourage custom mods... rather than going through the extra work of protecting them.


I was just interested in how one would go about it for general purposes, but you do have a point.

All the best,

NwN
Last edited on
Thanks for the kind response guys! I'll see what I can do for now! =)
Topic archived. No new replies allowed.