Sign in to follow this  

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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

So the request is to please help me find the VK for @.

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 this post


Link to post
Share on other sites
Quote:
Original post by WinRad
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?

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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by WinRad
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.

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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by WinRad
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 :).

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 this post


Link to post
Share on other sites
Quote:
Original post by WinRad
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.


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

#define PI 3.14
const 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 this post


Link to post
Share on other sites
Quote:
Original post by Spoonbender
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 '@'.

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 this post


Link to post
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.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this