Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
13 replies to this topic

#1 bennettbugs   Members   -  Reputation: 493

Like
0Likes
Like

Posted 17 February 2012 - 05:19 PM

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?

Sponsor:

#2 Washu   Senior Moderators   -  Reputation: 5362

Like
0Likes
Like

Posted 17 February 2012 - 05:28 PM

You want a parser. Or tie in a simple scripting language like lua.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.
ScapeCode - Blog | SlimDX


#3 bennettbugs   Members   -  Reputation: 493

Like
0Likes
Like

Posted 17 February 2012 - 05:34 PM

Um... what exactly is a parser?

#4 bennettbugs   Members   -  Reputation: 493

Like
0Likes
Like

Posted 17 February 2012 - 05:39 PM

i mean i wouldnt need to write out all the variables and functions, just call a few functions to do what i type in.
maybe i will look into lua...

#5 SimonForsman   Crossbones+   -  Reputation: 6184

Like
0Likes
Like

Posted 17 February 2012 - 08:06 PM

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.
I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!

#6 e‍dd   Members   -  Reputation: 2105

Like
2Likes
Like

Posted 18 February 2012 - 12:45 AM

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...

#7 Cornstalks   Crossbones+   -  Reputation: 6991

Like
0Likes
Like

Posted 18 February 2012 - 02:40 AM

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.
[ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

#8 IceBreaker23   Members   -  Reputation: 614

Like
0Likes
Like

Posted 18 February 2012 - 11:29 AM

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.

#9 Cornstalks   Crossbones+   -  Reputation: 6991

Like
0Likes
Like

Posted 18 February 2012 - 03:02 PM

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.
[ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

#10 IceBreaker23   Members   -  Reputation: 614

Like
0Likes
Like

Posted 20 February 2012 - 08:56 AM


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.


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

#11 Antheus   Members   -  Reputation: 2397

Like
0Likes
Like

Posted 20 February 2012 - 09:18 AM

Oh, i forgot java/c# gets interpreted and c++ is an executeable.

The distinction isn't all that useful, both Java and C# VMs run mostly native code, same as C++ binary. They just defer the compilation until code runs. And even then it's possible to force most compilation upfront.

On Android, Java is statically compiled, exactly the same way as C++.

On most modern x86 CPUs even native code is "interpreted", at very least the instructions that CPU executes are defined via microcode rather than being implemented directly.

Didnt knew that Posted Image Is there no way to do this?


Of course there is, it's just a bit of work. Structure data must either be manually defined or somehow generated by compiler or some other tool.

C++ doesn't offer reflection out-of-box due to optimization. A compiler is free to remove anything it deems redundant, including structures and functions. Full reflection would require preserving even unused parts. Templates in particular cause unacceptable overhead without such optimization.

#12 IceBreaker23   Members   -  Reputation: 614

Like
0Likes
Like

Posted 21 February 2012 - 02:21 PM


Oh, i forgot java/c# gets interpreted and c++ is an executeable.

The distinction isn't all that useful, both Java and C# VMs run mostly native code, same as C++ binary. They just defer the compilation until code runs. And even then it's possible to force most compilation upfront.

On Android, Java is statically compiled, exactly the same way as C++.

On most modern x86 CPUs even native code is "interpreted", at very least the instructions that CPU executes are defined via microcode rather than being implemented directly.

Didnt knew that Posted Image Is there no way to do this?


Of course there is, it's just a bit of work. Structure data must either be manually defined or somehow generated by compiler or some other tool.

C++ doesn't offer reflection out-of-box due to optimization. A compiler is free to remove anything it deems redundant, including structures and functions. Full reflection would require preserving even unused parts. Templates in particular cause unacceptable overhead without such optimization.


Really usefull knowledge :D
Thank you.

#13 Martins Mozeiko   Crossbones+   -  Reputation: 1422

Like
0Likes
Like

Posted 21 February 2012 - 02:30 PM

On Android, Java is statically compiled, exactly the same way as C++.

What do you mean "exactly the same way as C++" ? On Android Java is compiled exactly same as for desktop - to bytecode. During runtime bytecodes gets interpreted and hotspots are JITed to native machine code. So that is not exactly same as C++, which gets compiled completely to machine code on development host.

#14 Antheus   Members   -  Reputation: 2397

Like
0Likes
Like

Posted 21 February 2012 - 05:18 PM

On Android, Java is statically compiled, exactly the same way as C++.

What do you mean "exactly the same way as C++" ? On Android Java is compiled exactly same as for desktop - to bytecode. During runtime bytecodes gets interpreted and hotspots are JITed to native machine code. So that is not exactly same as C++, which gets compiled completely to machine code on development host.

My bad, I was thinking of something else.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS