|
theman515
|
 |
« on: February 13, 2012, 05:57:10 PM » |
|
Hello everyone, I have sort of an odd question. Is there any way in C++ to make a string into a keyword through pre-processor macros? For example I know this is possible #define str(x) #x and that makes x a string, even though it's a keyword into a string. I was wondering if the reverse is possible. thanks in advance 
|
|
|
|
|
Logged
|
|
|
|
|
eigenbom
|
 |
« Reply #1 on: February 13, 2012, 06:01:06 PM » |
|
I'm pretty sure that's impossible. What's the use case for this?
|
|
|
|
|
Logged
|
|
|
|
|
increpare
Guest
|
 |
« Reply #2 on: February 13, 2012, 06:03:07 PM » |
|
That only works if x is a constant string that's known at compile time. If you want to turn string in general into language keywords at runtime, then you're asking for a full language interpreter/compiler (basically not practical if you want to use c++). If you want to access variables by name, then you're looking for some sort of reflection or rtti (probably the more useful search word for c++). Enabling this in the compiler may slow some things down, can't remember, but it allows you to access things about classes from strings. If you're just interested in compile-time stuff, I ... don't think it's possible. I'll second eigenbom's request that you give us a hint what you want this for 
|
|
|
|
|
Logged
|
|
|
|
|
theman515
|
 |
« Reply #3 on: February 13, 2012, 07:19:58 PM » |
|
hmm that reflection or rtti thing sounds like what I need.
What I need it for is that I want a developers console for my game and the ability to change class members through string commands. I could just do however many if statements but that seems like a lot of work for something so procedure like.
|
|
|
|
|
Logged
|
|
|
|
|
increpare
Guest
|
 |
« Reply #4 on: February 13, 2012, 07:21:48 PM » |
|
hmm that reflection or rtti thing sounds like what I need.
What I need it for is that I want a developers console for my game and the ability to change class members through string commands. I could just do however many if statements but that seems like a lot of work for something so procedure like.
yep, rtti to the rescue  (or, if you wanted to do fancy stuff, a proper scripting language binding, but probably overkill for your purposes...)
|
|
|
|
|
Logged
|
|
|
|
|
theman515
|
 |
« Reply #5 on: February 13, 2012, 07:27:26 PM » |
|
alright, thanks again guys.  and yeah I tried boost::python for that but bjam was waaayyyy too complex for my purposes.
|
|
|
|
|
Logged
|
|
|
|
|
eigenbom
|
 |
« Reply #6 on: February 13, 2012, 07:37:51 PM » |
|
Yeh that's a hard thing to do in a compiled language, because variable names get transformed into addresses. The actual names get thrown away.
I'm not sure you can do what you want to with RTTI. You might be able to automate it by compiling with debugging symbols on, then hooking into a debugger. But that is icky..
Probably the simplest approach for now is just to have a big function that maps from string to variable reference, with a bunch of switches, but eventually you'll want to move to a scripting language, or a component-based approach..
|
|
|
|
|
Logged
|
|
|
|
|
theman515
|
 |
« Reply #7 on: February 13, 2012, 08:03:47 PM » |
|
any advice for the scripting language for using classes? python doesnt seem very... simple to integrate classes in to.
|
|
|
|
|
Logged
|
|
|
|
|
eigenbom
|
 |
« Reply #8 on: February 13, 2012, 08:15:23 PM » |
|
Lua. best programming investment you'll ever make.
|
|
|
|
|
Logged
|
|
|
|
|
eigenbom
|
 |
« Reply #9 on: February 13, 2012, 08:16:05 PM » |
|
+ Luabind
|
|
|
|
|
Logged
|
|
|
|
|
mcc
|
 |
« Reply #10 on: February 13, 2012, 11:59:30 PM » |
|
I did this using lua.
In general though if this is the kind of thing you want to do, writing your game in C++ to start with may not be the best approach.
|
|
|
|
|
Logged
|
|
|
|
|
bateleur
|
 |
« Reply #11 on: February 14, 2012, 04:35:56 AM » |
|
Another tip: Depending on how many classes you need to do this to, it's often easier to just write yourself debug helper methods for your classes.
|
|
|
|
|
Logged
|
|
|
|
|
xrabohrok
|
 |
« Reply #12 on: February 14, 2012, 09:00:30 AM » |
|
I'm taking a 300 level college class about just this right now. Not easy stuff. You might want to consider making a static method class, something that doesn't need to be instantiated but collects together recurring useful functions. Just a thought.
|
|
|
|
|
Logged
|
A picture is worth a 1000 words, so naturally they save a lot of time.
|
|
|
|