• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
BennettSteele

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

13 posts in this topic

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?
0

Share this post


Link to post
Share on other sites
You want a parser. Or tie in a simple scripting language like lua.
0

Share this post


Link to post
Share on other sites
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...
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


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

[code]
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;
}
[/code]

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

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, [url="http://thrift.apache.org/"]Thrift[/url] 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.
0

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

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.
[/quote]
You can't. C++ doesn't support reflection.
0

Share this post


Link to post
Share on other sites
[quote name='Cornstalks' timestamp='1329598922' post='4914301']
[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.
[/quote]
You can't. C++ doesn't support reflection.
[/quote]

Didnt knew that [img]http://public.gamedev.net//public/style_emoticons/default/biggrin.png[/img] Is there no way to do this?
Oh, i forgot java/c# gets interpreted and c++ is an executeable.
0

Share this post


Link to post
Share on other sites
[quote name='IceBreaker23' timestamp='1329749816' post='4914819']
Oh, i forgot java/c# gets interpreted and c++ is an executeable.
[/quote]
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.

[quote]Didnt knew that [img]http://public.gamedev.net//public/style_emoticons/default/biggrin.png[/img] Is there no way to do this?[/quote]

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

Share this post


Link to post
Share on other sites
[quote name='Antheus' timestamp='1329751108' post='4914826']
[quote name='IceBreaker23' timestamp='1329749816' post='4914819']
Oh, i forgot java/c# gets interpreted and c++ is an executeable.
[/quote]
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.

[quote]Didnt knew that [img]http://public.gamedev.net//public/style_emoticons/default/biggrin.png[/img] Is there no way to do this?[/quote]

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.
[/quote]

Really usefull knowledge :D
Thank you.
0

Share this post


Link to post
Share on other sites
[quote name='Antheus' timestamp='1329751108' post='4914826']On Android, Java is statically compiled, exactly the same way as C++.[/quote]
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.
0

Share this post


Link to post
Share on other sites
[quote name='Martins Mozeiko' timestamp='1329856250' post='4915254']
[quote name='Antheus' timestamp='1329751108' post='4914826']On Android, Java is statically compiled, exactly the same way as C++.[/quote]
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.
[/quote]
My bad, I was thinking of something else.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0