-
Inputing Character Data
I have an RPG game and I want to be able to load a character's stats and map position into the game from a text file.
Paladin
HP: 4500
MP: 800
Strength: 200
Vitality: 300
Agility: 100
Magic: 80
Defense = 600
Damage = 400
Magic Damage: 160
Stat Points: 5
Location: 5, 5
Any help is appreciated.
-
So what's your question?
My question is why are you attempting something as complicated as writing an RPG when you haven't mastered the basics of parsing a text file for information.
-
I've done something similar recently.
This is how I did it...
1. Read each line as a string into a vector.
This is easy. This can be done using getline(cin,string), then vector.push_back().
2. Read the first word of each line.
This isn't difficult either. Just check if the first word is something you recognise.
Code:
stringstream ss( line ); // include <sstream>
string firstWord ;
if ( (ss >> firstWord) ) {
// see if word is something you recognise
}
else {
// there are no words on this line; ignore
}
3. If the first word is something you recognise, read the rest of the line into values.
Code:
if ( firstWord == "HP" ) {
int hp ;
if ( ss >> hp ) {
// we got the health points
}
else {
// error
}
My suggestion is not to use =, : or any other characters to delimit strings. Just use spaces.
Code:
HP 400
MP 300
Stat Points 5
Location 2 2
Hope that helps.