• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

# How to declare different const variable when lpCmdLine is different?

## Recommended Posts

Hi. I recently learn how to use lpCmdLine like game.exe -easy , etc... // mode=0=easy ... void whatmode() { if (mode==0) { #define MaxEnermy 1 } if (mode==1) { #define MaxEnermy 2 } if (mode==2) { #define MaxEnermy 3 } } Now, when I start the game, MaxEnermy seems to be 3 everytime. So what i want to know is how can I define a constant within a function, but can also be called globally, because I can''t have an if statement without being in a function. In other word, if "game -easy" is executed, I want MaxEnermy=1, and "game -normal" is executed, MaxEnermy=2. Thanks in advance

##### Share on other sites
It''s called a global variable.

If you really want to declare a symbol, though, you can do this at the top of your source file:
#define MaxEnemy 1

and then when you test mode, do this:
...if(mode == 3){#ifdef MaxEnemy  #undef MaxEnemy  #define MaxEnemy 3#endif}...

Personally, I''d advise you to use a simple integer instead. That way, you could use the value of mode directly and change it more easily.

[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!

##### Share on other sites
quote:
Original post by Oluseyi
It''s called a global variable.

If you really want to declare a symbol, though, you can do this at the top of your source file:
#define MaxEnemy 1

and then when you test mode, do this:
...if(mode == 3){#ifdef MaxEnemy  #undef MaxEnemy  #define MaxEnemy 3#endif}...

Personally, I''d advise you to use a simple integer instead. That way, you could use the value of mode directly and change it more easily.

[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!

Err... doesn''t the preprocessor work BEFORE the parser ? Which means that #directives are blissfully ignorant of the control flow of the program outside of their own #ifs

In SledgeHammer''s example, the macro is redefined 3 times (regardless of the value of mode), while you just undefined it temporarily ?

In short, I doubt a macro would ever work. Go for the global int.

##### Share on other sites
#define is a preprocessor directive. Before your source code is compiled, the preprocessor looks through it for directives beginning with the ''#'' character. It then simply does a text replacement based on the symbols you''ve defined, and pretty much ignores everything else (perhaps a bit oversimplified, but it''s sufficient). So, basically, when you use something like this:

...if(mode == 3){    #ifdef MaxEnemy        #undef MaxEnemy        #define MaxEnemy 3    #endif}else if (mode == 4){    #ifdef MaxEnemy        #undef MaxEnemy        #define MaxEnemy 4    #endif}...

The preprocessor will first go through the code...and it basically sees this:

    #ifdef MaxEnemy        #undef MaxEnemy        #define MaxEnemy 3    #endif    #ifdef MaxEnemy        #undef MaxEnemy        #define MaxEnemy 4    #endif

After the preprocessor is done, the compiler sees this:

...if(mode == 3){}else if (mode == 4){}...

Now, if you actually used "MaxEnemy" in an expression, it would get replaced with 4, because that''s exactly what you told it do:

num_enemies = MaxEnemy;

Compiler sees this:

num_enemies = 4;

It''s easy to see that using the preprocessor for something like this is not going to achieve the results you want it to.

##### Share on other sites
So if I can''t use #define , can I use const int MaxEnemy to set value within the function and still be a global variable?

Thanks

##### Share on other sites
You don''t want it to be "const" if you plan on changing it based on a condition (which it looks like you are doing).

Do something like this:

int num_enemies;int WINAPI WinMain (...LPSTR lpCmdLine, ...){    ...    if (strcmp(lpCmdLine, "-easy") == 0)    {        num_enemies = 2;    }    else if (strcmp(lpCmdLine, "-hard") == 0)    {        num_enemies = 3;    }    ...}void DoSomethingInteresting (void){    ...    switch (num_enemies)    {        ...    }    ...}

As long as you declare a variable outside of a function, class, or method, it''s global, and you can use it anywhere in the same source file.

##### Share on other sites
Okay. But i don''t know how to make the other function that use const to change to int. For example, the tutorial provider has

typedef struct _moveobject
{
float x;
float y;
int active;
float xspeed;
float yspeed;
int style;
int energy;
long score;
int fire;
int other;
}moveobject;

moveobject player[MaxPlayer];
moveobject enermy[MaxEnermy];

How can I use int for the above example, because I know how the int can be defined outside of function, but the whole program is written for const variable, and I don''t know how I can change it to int. I get the following errors when I don''t use #define MaxEnermy or const int MaxEnermy outside of function, but it doesn''t acheive what I want to do.

error C2057: expected constant expression
error C2466: cannot allocate an array of constant size 0

##### Share on other sites
@Fruny: thanks for the heads up. Can''t believe I overlooked that.

@SledgeHammer: Your problem with constant values is that the statement you showed is a declaration. You can''t declare a constant-length array with a variable parameter. You''d need to do dynamic memory allocation for that, which is beyond your current scope.

Here''s a workaraound though: always declare the maximum number of enemies possible (say, 5), but only use up to MaxEnemy:
moveobject Enemies[5];...// in your main loop:for(int n = 0; n < MaxEnemy; ++n)  // do something to/with Enemies[n];

Happy Hacking!

[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!

• ### Forum Statistics

• Total Topics
628282
• Total Posts
2981805

• 9
• 10
• 11
• 17
• 14