Skip to main content
gamedev.net
🔒 Locked

Calling a function/variable by name ? string ?

Started by cippyboy Jan 2, 2003 at 4:21 PM 4 replies 300+ views
Original Post
cippyboy
cippyboy
Can I Call a function/variable by name ? Something like a string or something ? I think that`s posible in vb but I have no idea aout C++... The debugger can tell you some 0x0002b3 adress where the function is... can you call it that way ? Any other way ?
Relative Games - My apps
Fruny
Fruny
The short answer : you can''t.

The long answer : you can''t, short of parsing the symbol table yourself, or finding some API function (like GetProcAddress for Win32 DLLs or dlsym for Unix dsos) that support it. Then you''ll get into the wonderful world of name mangling...

What you can do, is to manually keep a table (e.g. std::map) matching function names to function addresses, and use it to call the functions.


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Sneftel
Sneftel
What are you trying to do?


Don''t listen to me. I''ve had too much coffee.
cippyboy
cippyboy
Well actually I havent tryed yet but i want to make Game Scripts and the best way seems to be the calling of a function by name or some switch if it doesnt work ...it should look like this : Intro.gsc
NPC Level[0].People[0] SetPosition 10,0,0
NPC Level[0].People[1] SetPosition 0,0,5
PlayCameraScript Scripts\\Intro.scp
PlayMusic Music\\Intro.wav
//This is probably what I think of it but it may also include :
Talk NPC Level[0].People //which will have its own "speech"
GiveItem Potion
GiveGold 100
MovePlayer Player1 1,0,5
SetPlayerAnimation Animations\\GoThere.ani

And the list can continue...Although this is the first time I
ve tryed to see how they should be done...
Relative Games - My apps
Sneftel
Sneftel
Yep, the best way to do this is with a map<string, function_pointer> or with if..else if...else if...else blocks.


Don''t listen to me. I''ve had too much coffee.
xiros
xiros
I havent had to much success with passing information to functions/variables in the way you explained. I just made another function that recieves all the information in my own format and it passes the information in the proper converted format to your other functions/variables. The problem is that you will have to write quite a bit more code that includes all the functions and variables.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.