# Strings and Definitions

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

## Recommended Posts

Hey guys :), This question seems to be a very easy one but I couldn't find anything online to help me out so that's why I'm posting. Well now for the problem, when you have a definition such as - [code C++] #define Up 'W' and a function such as - [code Win32] GetAsyncKeyState(Up) - would it be the same as just saying/typing - [code Win32] GetAsyncKeyState('W') - ? Thanks a lot.

##### Share on other sites
For better or worse (mostly worse), yes.

##### Share on other sites
Since when did GetAsyncKeyState accept a char argument? Even if it did, you would be better of with const char Up='W'. Actually, if you don't make it const, you can change which key is the up key, ie, user settings.

##### Share on other sites
I'm sure others with expand on this, but keep in mind that in C++ there is (almost) zero reason to use macros.

So whenever you think to use a macro, first consider whether there a way to achieve your goal without using a macro.

For example:

const char kUP = 'W';

##### Share on other sites
Thanks for the answears guys :).

It never did accept chars but a '' is different then a "" which is why '' works and "" doesn't :). So there's one question resolved but now I got another which doesn't reall need another thread :) - How do I let the user select which keys he wants to use(for the game I'm making)? Also what is the virtual keycode for the @ symbol?

##### Share on other sites
Moved to For Beginners.

##### Share on other sites
Well I guess that made sense... although its a little offensive if you think about it :D

Anyway don't answer my question because I just used common sense to figure out that I needed to use INTs to store the key values...

RESOLVED

##### Share on other sites
Oh damn yet another problem... what is the VK key code for @? I keep looking yet I can't find it... hard to believe its no where on the net... the other VKs are... if someone can find it I'm more stupid then a donkey :S

EDIT - I just realized fpsgamers's post. Why wouldn't you use macros? I use them all the time to keep the program somewhat easier for me to use it next time or for me to make simple customizations without having to spend a long time looking for the variable associated with it. Is there some kind of drawback to using macros?

Thank you :(.

##### Share on other sites
Quote:
 Original post by WinRadEDIT - I just realized fpsgamers's post. Why wouldn't you use macros? I use them all the time to keep the program somewhat easier for me to use it next time or for me to make simple customizations without having to spend a long time looking for the variable associated with it. Is there some kind of drawback to using macros?

Yes, there is a drawback to using macros. First, macros don't follow any scoping rules. For example, if you did

#define PI 3.14

It would mean that you could never have any variable named PI, because if you ever wrote PI, it would just get replaced by 3.14. However, if you did

namespace mynamespace
{
const double PI = 3.14;
}

You wouldn't prevent others from using the variable name PI outside of mynamespace. Let's say you are using a physics API in you project called PhysicsAPI. You didn't write PhysicsAPI, but you are including it in your project. If the people that wrote PhysicsAPI used #define PI 3.14, that would totally screw you over if you had your own variable called PI or if you included another API that also tried to #define PI 3.14. However, if the PhysicsAPI people did

namespace PhysicsAPI
{
const double PI = 3.14;
}

Then you could also include another API that does

namespace SomeRandomAPI
{
const double PI = 3.14;
}

And you wouldn't have any problems with the variable names since they are in their own namespaces and variables obey scoping rules, unlike macros.

Another downside to macros can be seen in switch statements. For example, if I have a switch statement that switches based on an enum I give it, my compiler will warn me if I don't handle all possible values in the switch statement (and it will tell me specifically which ones I don't handle, which is great for me incase I miss one). If I use a macro, there are a billion (actually more than that) possible cases for the switch statement, which means my compiler can't give me a nice warning like it did for the enum.

And another downside to macros is that they don't provide the same type safety as const variables do. const char globalChar = 57 tells my compiler a lot more about the variable and how I want to use it than #define globalChar 57 does.

And again, another problem with macros is the infamous max macro. I'll let you research that one though. There should be plenty of posts here on GameDev that explain it.

##### Share on other sites
Well that seems to make sense... Hmm but what if the macros I use are very long and specific and I only need them in one case? For example in my game I use a macro called Player1UpDef which is very specific and most likely would never be used by me again because I add def to the end of every #define macro.

And I already know what MAX is :). Although I have to say it a little dumb of Microsoft to add a macro like that... its just too common(the word max).

Also I need to know the VK for the @ sign :).

Thanks.

##### Share on other sites
Quote:
 Original post by WinRadWell that seems to make sense... Hmm but what if the macros I use are very long and specific and I only need them in one case? For example in my game I use a macro called Player1UpDef which is very specific and most likely would never be used by me again because I add def to the end of every #define macro.

Yes, then you're unlikely to get any name conflicts, so you're probably safe from that particular problem... But what did it buy you?
Why not just use the non-macro alternatives and be safe?

The point is that (almost) anything you do with macros, can be done as easily, and much much safer without them.

Quote:
 Also I need to know the VK for the @ sign :).

As far as I know, all visible characters just use their ASCII code as VK.
A's virtual keycode is the integer value of 'A', and @'s is '@'.

##### Share on other sites
OK cool thank you :).

As with your "be safe" question. The reason I don't simply use variables is because the macros can be defined right at the top without any problems and I just find it easier to customize my app with macros then variables... because the variables end up cluterring so... well that's my reason.

Thanks again :).

##### Share on other sites
Quote:
 Original post by WinRadOK cool thank you :). As with your "be safe" question. The reason I don't simply use variables is because the macros can be defined right at the top without any problems and I just find it easier to customize my app with macros then variables... because the variables end up cluterring so... well that's my reason.Thanks again :).

A good design, most likely one that uses classes, would avoid the clutter that you speak of. Basically, if it works for you, fine, but don't think that clutter is a problem only solvable by an ugly work-around. Just something to remember.

##### Share on other sites
Quote:
 Original post by WinRadAs with your "be safe" question. The reason I don't simply use variables is because the macros can be defined right at the top without any problems and I just find it easier to customize my app with macros then variables... because the variables end up cluterring so... well that's my reason.

That's not a very good reason. Tell me why one of these is more cluttered than the other.

#define PI 3.14const double pi = 3.14;

What "problems" do you have with defining variables at the top? How much harder is it to change the value of a global constant than it is to change a #define? The benefits far outweigh the downsides of using real variables.

##### Share on other sites
Quote:
 Original post by SpoonbenderAs far as I know, all visible characters just use their ASCII code as VK.A's virtual keycode is the integer value of 'A', and @'s is '@'.

Virtual key codes are key codes. Assuming a standard US English keyboard, shift-2 would generate an '@'; however, the virtual key code generated would still be VK_2. If you wanted to check the characters generated by a key press you could try using WM_CHAR or WM_UNICHAR.

##### Share on other sites

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

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628646
• Total Posts
2984030

• 9
• 9
• 9
• 10
• 21