Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Right now I have the graphical and input portions of a console for my game (command parsing hasn't been implemented yet). I was wondering how you would go about making changes to properties in game objects. For example, if I typed in the console:

skeletonMonster maxHP 20

That would change all of the existing in-game skeletons' max hit points to 20. After you parse this information what are some ways to change the value? How can I change the that member variable without directly accessing it (by making it public/global)?

I'd like to implement this so I don't have to change variables in the code and recompile every time while playtesting.

share|improve this question
    
Information hiding on what? The skeleton class? I don't understand your question. –  Justin Skiles Oct 14 '12 at 3:28

3 Answers 3

up vote 2 down vote accepted

The broad answer to your question is that you need a metadata/reflection/introspection system of some kind. This is a way for your engine to expose properties on objects to external modules. These systems can be used for a great many things: file serialization, in-game GUI editors, network synchronization, script binding, and of course in-game consoles.

Depending on the language you're using, such a system can range from trivial (say, in C# that already has an excellent reflection system built into the .NET library) to mind-boggling painful (such as in C++, which lacks built-in reflection or any way to portability build a fully featured one).

If you expand your question with some more information on the language you're using, more direct answers would be possible. As is, the best we can do is point you towards those terms (reflection and introspection and metadata) to help you look through your language's documentation or to Google for guides on building custom reflection/metadata systems. (Though be warned you won't get much for C++. About all you'll find are projects like clReflect, which looks amazing though I've yet to give it a whirl, and some incomplete articles on my site.)

In general pseudocode for an object-oriented language, you might end up with some kind of "register" method on all your classes that create an index of member variables (and possibly even methods) that exist, along with metadata about how they are to be used and accessed. Even in languages that have a full reflection system, if they lack user-specific attributes, you may still want a custom metadata system in order to annotate members with additional information (like whether they should be readonly, or what their min/max values are, etc.). In C++, you can also bind getters and setters rather than only directly member variable access, which can be advantages; again, though, it's a very non-trivial task and requires decent knowledge of template metaprogramming to pull off. In the general sense, this should get your wheels turning:

void MyClass.RegisterMeta(inout MetaData meta) {
  meta.AddROMemberVec3(MyClass.velocity, "Unit velocity")
  meta.AddRWMemberVec3(MyClass.position, "Unit position")
  meta.AddRWMemberInt(MyClass.health, "Remaining health", 0, C_MAX_HEALTH)
  meta.AddRWMemberInt(MyClass.maxHealth, "Maximum health", 0, C_MAX_HEALTH)
}

If you're looking to maximize on runtime flexibility and interation time, you might consider using a component based design approach to building game objects. You can also use a component based design approach to resource objects (like monster definitions references by your monster game object instances). The usual introductory text on that topic is Evolve Your Hierarchy. I also personally think it's easier to build a custom metadata system using component based design, since it already gets you into thinking about decoupling data and exposing object metadata to higher-level systems.

Lastly, if you are working in C/C++ and are looking for a quick but messy solution (but which works just fine for the vast majority of indie/hobby games), take a look at AntTweakBar. It's an in-game GUI system for building editors and property tweakers. It can be used for global game config, per-object properties, and so on. I know of more than a few fantastical games that have been released which used AntTweakBar as their sole game content editor GUI, and used a console only for debug output. The API is a little error prone and it's not super flexible or featured, but it gets the job done a heck of a better than any text-oriented console ever could. I would be surprised if there aren't similar tools (or bindings to this one) for other languages, too.

share|improve this answer
    
Holy moly. Thank you so much. I've always wondered what the mysterious C++ metaprogramming was. It was never touched upon in any CS classes. Guess now is a good time to look into it. Yes, I am using C++. Do you have any learning resources for metaprogramming/reflection/introspection you would recommend? –  ShrimpCrackers Oct 17 '12 at 6:11

In some games that I've seen, the debug console is actually the Lua interpreter prompt. That way, anything you can call from script can be done from the console.

share|improve this answer

Depending on what language and framework you are using, there are a couple of options (theoretically all the options will work for all common languages and frameworks...).

My first suggestion would be to store the information you want to change outside of the program on disc, and loading that data into the runtime at the start of execution. That way you do not have to recompile to test out new values. This of course requires that you restart the game each time you want to try new values. Unless... you have something in your runtime that detects changes to this file and reloads it upon an update.

Another solution, like others have suggested, is evaluating the command as part of your runtime and directly setting the new values. As you said though, you'd rather not expose those variables as public, so I would have those classes contain their own handlers for changes to their data. Whatever system is receiving and controlling console input would re-route requests to the appropriate objects (class types).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.