I want a way to have "global preprocessor definitions" such that I can change a single value before compilation to add or remove functionality of my program. Currently I have a "global script" (called God.cs) with consts such as:
public const bool PRINT_RUN_VALUES = true;
public const bool DEBUG_MOVEMENT = false;
public const bool DOUBLE_SPEED = false;
With all relevant scripts using these values, for example:
in script 1:
if (God.PRINT_RUN_VALUES) {
float averageDist = /* calculate values */
print("Script1: averageDist = " + averageDist);
}
in script 2:
if (God.PRINT_RUN_VALUES) {
print("Script2: radius = " + radius);
print("Script2: time = " + time);
}
The problem with this method is that I get tons of
CS0162: Unreachable code detected
warnings. I can turn off these warnings using:
#pragma warning disable 0162
#pragma warning disable 0429
But I don't want to do that because these warnings can be useful in cases that don't involve these values from God.cs. An alternative might be to use classical preprocessor definitions such as:
#define PRINT_RUN_VALUES
...
#if (PRINT_RUN_VALUES)
float averageDist = /* calculate values */
print("Script1: averageDist = " + averageDist);
#endif
However as far as I know that would require me to add '#define PRINT_RUN_VALUES' at the start of each .cs file, which would become a nightmare when I want to quickly turn off or on these global statements. I want all of these global definitions is a single place so that I can quickly turn them off and on.
How can I define some preprocessor thing such that it isn't included in each script? I know that there are proprocessor definitions that meet this criteria, such as definitions to check what operating system is compiling the software. Or is there another way around this?
You can set these in the project properties, for example this is how the DEBUG
symbol is defined. Right click on your project, select Properties and enter your custom csymbols in the box labelled Conditional compilation symbols:
Now in my code I can do this:
#if CHEESE
public string CheeseName = "Gouda";
#endif
Note that these symbols are defined as part of the configuration. This means you can define different symbols for release versus a debug or UAT environment for example.