Jump to content
  • Advertisement
Sign in to follow this  
BennettSteele

C++ turning "APP.QUIT()" (a string) into actual code

This topic is 2372 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

So i finnished writing some basic network code, and thought about the in game commands. I wanted to be able to turn, lets say

"app.quit()"

and in game make the game quit. I started now by writing

if(ClassDepth[0]=="app")
{
if(ClassDepth[1]=="quit"){app.quit();}
}

but it will take a long time to re write everything. is there any way to make this easier?

Share this post


Link to post
Share on other sites
Advertisement
You could also look at using a map using the command string as the key and a function pointer as the value, it should be enough if you don't have all that many commands to deal with and you don't need to pass parameters to them (If you need to pass parameters to functions them it gets messy really fast), for a large number of commands or more complex commands then something like lua is definitly the way to go. (its extremely easy to execute lua scripts in C or C++ and also quite easy to expose C functions to lua, exposing full C++ classes to lua is a bit more work but there are third party libraries(luabind, tolua for example) that help with that.

Share this post


Link to post
Share on other sites
Assuming C++:


struct action : uncopyable
{
action() { }
virtual ~action() { }
virtual void invoke() = 0;
};

struct quit_action : action
{
quit_action(Application &app) : app(app) { }
virtual void invoke() { app.quit(); }

Application &app;
};

// ...

std::map<std::string, shared_ptr<action> > actions;
actions["quit"].reset(new quit_action(the_app));

// ...

bool do_action(const std::string &name)
{
std::map<std::string, shared_ptr<action> >::iterator f = actions.find(name);
if (f != actions.end())
{
(*f)->invoke();
return true;
}
return false;
}


If your actions need parameters, you might pass those via a stack, once parsed out of the command string, and each action's invoke() method would attempt to pop them as needed before use (with appropriate checking for underflow).

I'd also agree that Lua is a good idea in principle, but it might actually be more work to do it that way, especially to someone that's relatively new to C++. On the other hand, using a proper scripting language opens up your code to all kinds of other possibilities...

Share this post


Link to post
Share on other sites
Well, this'll be complete overkill for your game, but if you really wanted a nice, extendable solution, Thrift is a good option, as it helps manage serializing data (i.e. you give it a data structure and it constructs a compact message representing that data which you can pass through the network) and RPC (Remote Procedure Call, which is exactly what you're asking about now).

[edit]

Google's protocol buffers (protobuf) apparently does RPC too.

Share this post


Link to post
Share on other sites

You could use reflection. It turns strings into real code in runtime. Don´t know how to do this in c++. Java has its own class for this.

You can't. C++ doesn't support reflection.

Share this post


Link to post
Share on other sites

[quote name='IceBreaker23' timestamp='1329586157' post='4914245']
You could use reflection. It turns strings into real code in runtime. Don´t know how to do this in c++. Java has its own class for this.

You can't. C++ doesn't support reflection.
[/quote]

Didnt knew that biggrin.png Is there no way to do this?
Oh, i forgot java/c# gets interpreted and c++ is an executeable.

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!