I was looking for an easy way to define variables for random monster encounters in my game. I'm not sure how to define some without typing out 1000+ lines of code for like 25 monsters. What I was wondering if there was a way to make a config file, or a way to do it in the c++ code itself where I could just define the monster, and set the integer values for it's variables. So something like this, don't know the code, so I'll kinda just write it out. Something like this.

Worm {
damage = 10;
health = 20;
defense = 10;
}
Wolf {
damage = 25;
health = 30;
defense = 15;
}

Something like that would be greatly appreciated.
Bonus points for text editable config file!

In general, this type of thing is done with a markup format. XML and JSON are two common ones with well-known parsers written in C++.

Create a data file where each line defines the values for each monster. Then create a read function that reads the file. This way you can add and modify monsters any time you want and won't have to change the program.

Create a data file where each line defines the values for each monster. Then create a read function that reads the file. This way you can add and modify monsters any time you want and won't have to change the program.

So pretty much filestream for the file, and read it so if it says:

Name:Rat
Attack: 10
Defense: 10
Health: 20

I would get it to read each line specifically , and take the value after the colon? And store it under a string? Or what? If not a string

Can take help of fscanf function too, which will help in retrieving data as per the format.
definition for it
int fscanf ( FILE * stream, const char * format, ... );
Read formatted data from stream
Reads data from the stream and stores them according to the parameter format into the locations pointed by the additional arguments. The additional arguments should point to already allocated objects of the type specified by their corresponding format tag within the format string.

/* fscanf simple example */
#include <stdio.h>

int main ()
{
  FILE * pFile = NULL;
  char szRat[128];
  int nAttack = 0;
  pFile = fopen ("TestFile.txt","r");
  fscanf (pFile, "Name:%s", %&szRat );
  fscanf (pFile, "Attack:%d", &nAttack);
  fclose (pFile);
  return 0;
}

Hope above code will help in you getting idea about it.

commented: Do you know the difference between C and C++? -4

So pretty much filestream for the file, and read it so if it says:

Name:Rat
Attack: 10
Defense: 10
Health: 20

I would get it to read each line specifically , and take the value after the colon? And store it under a string? Or what? If not a string

It's your file. Define it any way you want.
Is "10" useful to you as a string?

It's your file. Define it any way you want.
Is "10" useful to you as a string?

No, so I'll google it and see what I come up with.

I will also suggest you to check windows '.ini' format.
Most of windows enabled application uses this type of config format. It also have supported API like GetPrivateProfileString

sample code can be found here

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.