# constant variables in a .txt file load at run time?

This topic is 2083 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I read somewhere about putting your constant variables in text file and reading them at run time, and setting the constants that way... Is this possible? If so how would you go about this? I didn't think you could do this...

Thanks!

##### Share on other sites

Global consts? No, since by definition you can't change a constant variable once you have defined it (unless you use const_cast, but don't do that).

You can put your variables in a class though, make them const and initialise them in the constructor (but you will have to throw or set variables to a default value if the constructor fails). You need to create the class at runtime then.

##### Share on other sites

I think there is some confusion here between a constant and a const variable.

Numeric constants, also known as magic numbers, are specific values that influence or constrain the behaviour of a system. A const variable is a variable whose contents may not change as enforced by the compiler.

It is generally a bad idea to pepper your code with magic numbers for two main reasons - it can be unclear when reading the code what the purpose of a number is (3.141... rather than M_PI for example) and if the number needs to change, it is better to have a central place to change it.

If you want the facility to alter the contents of a magic number to tweak the behaviour of your system, for example constants used to influence the physics of car handling, or force of gravity, without having to rebuild the system, you can load these constants from an external source.

Placing pi in a text file and loading it at start up is clearly pointless. This sort of magic number may as well be defined and named in source using a const float/double etc.

Edited by Aardvajk

##### Share on other sites

The approach I use to externalizing variables is creating a structure that holds a name string and, for example, a value like a float or a double.

At program start code, I initialize all these 'variable' structures using calls that take a name, and a default value, and returns either a pointer to the new structure added to the linked list of 'variables' or an index into an array of variable structures.

Then, I load any existing config text file, and parse it so that the first part is the name of the variable, and the second part is the value. Do a search for each variable by name, set its new value, and then continue with initialization and then entering the main engine loop.

At shutdown, I dump all those variables out back to the same text file - this allows for anything in the engine, like a user interface, a console system, etc.. to modify those variables and have their changes saved.

The variable structures themselves can then be used inplace of constants.. eg:


typedef struct
{
char name[MAX_VARNAMECHARS];
float value;
} var_t;

...

var_t *player_speed, *player_jumpvel, *player_friction;

...

void Init()    // setup stuff, create variables.. etc
{
player_speed = variable_create("player_speed", "300");
player_jumpvel = variable_create("player_jumpvel", "400");
player_friction = variable_create("player_friction", "0.9");

...

// load any config, set variables to previous values
// otherwise use defaults..
variables_setwithtextfile("variables.txt");
}

..

void Refresh()
{
...

if(player_keys[KEY_FORWARD])
{
player_velocity += player_movespeed->value * player_forwardvector;
}

if(player_keys[KEY_JUMP] && player_onground)
{
player_velocity.z += player_jumpvel->value;
}

...

// please don't actually use any of this code
player_velocity *= player_friction->value;
}



This is an elementary explanation of a console-variable type system. This pseudo code is purely for demonstration purposes of using a variable-structure for labeling values for manipulation by their names. I left out the part that manages the variables themselves, because it should be simple enough to construct, and thought that showing a demonstration of such a system's use would convey what it would be responsible for doing.

Good luck.

##### Share on other sites

Thank you all for your input on the topic. Greatly appreciated.

##### Share on other sites

I load most of my variables from file. Currently rewriting some code to make it more flexible.

There was an article on GameDev.net awhile back: TweakVal

It automatically loads changes to your source files as the game is running, updating your 'constants'. I haven't actually tried it though.

It looks like there is an updated version on github, if you google.

Mostly I just load values from config files.

1. 1
2. 2
3. 3
Rutin
13
4. 4
5. 5

• 26
• 11
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633700
• Total Posts
3013418
×