Jump to content
  • Advertisement
Sign in to follow this  
WinRad

Strings and Definitions

This topic is 3629 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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!