C# Console Window

Started by
3 comments, last by Moe 12 years ago
Hey guys I'm new to the forum so sorry if this is a re-post or anything like that. So I was working on trying to make a console window for my games. I'm working in XNA but that shouldnt matter much for what I'm doing. Basically what I want is to make a way that you can edit and check on variables in game by typing stuff into this console like "get variable1" or "variable1 = 10". So I was wondering if there was a way to process this string name "variable1" into a reference to variable1 in my code without specifically going 'if (name == "variable1") { return variable1; }' because I want to make the console as versatile as possible.

Any ideas on what to do? Let me know if you need more info.Thanks in advance!

p.s. Oh also if there is a way to get a reference this way that also includes a class name or namespace for example: "StaticClass.varaible1 = 10". If not I can work around that.
Advertisement
If the functionality you want is simple, you could probably roll your own parser and use reflection to capture the class and member names. Read up on reflection in C#, you'll find it's pretty powerful.

Here's some stuff to maybe get you started: http://www.dotnetperls.com/reflection

If you wanted a more general or extinsible solution, such as scripting, perhaps you could leverage something like CS-Script.
From what i understand, using System.Reflection functions takes a relatively long time to execute. If you're concerned with performance, you might want to consider that.
"Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law."


-- Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid
Unless I was using reflection every iteration in a tight loop somewhere, I wouldn't worry about perfomance. I probably wouldn't worry about it anyway, until it became a problem.

What the OP seems to indicate is that his parser would execute only in response to typed input, and would be fairly simple. I highly doubt that the performance characteristics of reflection would come into play under such a scenario.
I found this to be an interesting start at writing a console:

http://bittermanandy.wordpress.com/2008/07/28/efficient-development-part-two/

This topic is closed to new replies.

Advertisement