Skip to main content
GameDev.net gamedev.net
🔒 Locked

Calling a function from within a string

Started by Chocoboko May 21, 2003 at 11:42 PM 6 replies 350+ views
Original Post
Chocoboko
Chocoboko
Okay, this may sound crazy, but it is possible to call a function from within a string. I''ll explain what I mean: int main() { char string[50]; string="printf(\"Test\")"; } I want to call the function stored inside of string. The reason I want to do something like this is because I am writing a game which loads all map data from an external file and I don''t want to have to create a scripting language in order to load event data into the game. I know something similar to this can be done because in Win32 programming, you have to point a Window initialization variable to a class, which is store inside of a string. I''d appreciate any help. Thanks.
billybob
billybob
research std::map and function pointers
SabreMan
SabreMan
quote:
Original post by Chocoboko
Okay, this may sound crazy, but it is possible to call a function from within a string.

In C++? No. You have to simulate it.
quote:

The reason I want to do something like this is because I am writing a game which loads all map data from an external file and I don''t want to have to create a scripting language in order to load event data into the game.

If you insist on using C++, that''s too bad.
AndyGeers
AndyGeers
If you use DLLs it''s well easy, because you can just search for the particular function name you want in the DLL as a string. Of course, that way, you have to make your own functions (since printf obviously isn''t going to be found in your own DLL) but that''s not hard - just make a simple wrapper funciton.
RPGeezus
RPGeezus
Calling the function directly is not possible, as the functions exist as pointers, not physical names. You could always fake it though.

Create an array of void *''s and char *s. The char *s will be function names, the void * will the pointers to the corresponding function.

Will


CraZeE
CraZeE
the bad news is, C++ doesn''t support this natively. The good news is u can make something like that happen using C++. The concept: make a simple parser.

all u need is a structure that relates a function name (string) and a function pointer to call that function. Create an array of these structures that refer to all the possible functions that u may call. at runtime, all input strings are tokenized and compared with the function names stored in the array.. the instant a match is found, the associated function pointer is called.

The only downside to this approach is that the function pinter prototype is singular. it means ur parameters list must be same for all the functions u''re gonna call. Very hard to work with at times, and especially difficult if u want to use it to wrap around the existing libraries.
To learn, we share... Give some to take some -
klems
klems
Check out the scripting articles in the Articles & Resources section.



For crying out loud, she has fishes coming out of her head on either side. How can you find this hot?!
Jan Wassenberg
Jan Wassenberg
You can use SymFromName (dbghelp function) to get the address of functions you''ve written (no need for an explicit name <--> address mapping in code), and GetProcAddress for CRT functions (assuming you''re using MT DLL).
E8 17 00 42 CE DC D2 DC E4 EA C4 40 CA DA C2 D8 CC 40 CA D0 E8 40E0 CA CA 96 5B B0 16 50 D7 D4 02 B2 02 86 E2 CD 21 58 48 79 F2 C3

Topic Locked

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

Sign in to reply to this topic.