Jump to content
  • Advertisement
Sign in to follow this  
Six222

C++ Calling functions from text?

This topic is 2704 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 all, i've been wondering for a while if you could call a function from text for example; say you have a XML file that stores the information about an item in game and it would have a variable called "function" and you could set that too "foo" would it be possible to then be able to read that variable in game and call the function "foo"?

Share this post


Link to post
Share on other sites
Advertisement
Not in C++ without specifying/interpreting the mapping yourself. There's no mechanism to do it automatically, or reflection that would allow you to generate/find the function.

Share this post


Link to post
Share on other sites
It can be done, but C++ has no built in methods to do so. You need to manually parse the text data for the names "function" and "foo" and then manually decide how the function should be called (or use a third party tool or tools that will do some of this work for you). The relevant topics are called lexical analysis (or just lexing) and parsing.

Share this post


Link to post
Share on other sites
You could probably do something like have a stl map of function pointers, where the key to each is a string.

then you can just go

functionslist[ string_from_xml ]( );

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!