Wednesday, January 10, 2018

xna - Console-type interface in game


I am currently programming a very simple 2d game in C# with XNA.


I was wondering how to implement a console-type interface in a game or graphical software like there is in Skyrim or Counter-strike or some many more games?


These "consoles" can allow you through various command to change many parameters directly in-game.


I find this very interesting for debugging purposes and I was hoping to implement something similar in my program. Is there a particular way to achieve this?



Answer



The graphical side should be easy, so i'll omit that.


You have to be able to parse your input properly. If you want to go all out you can read on compiler theory, though i don't think this is what you're looking for.


In general what you want to be able to do is lexical analysis of your input. This means being able to take a string and output tokens. Tokens are objects that say what a particular text is, so you'll have an "id" object for variables or an "operator" object, etc. It doesn't have to be so "official", but you want to be able to tell what each part of the string does/is. Then, you'd build a syntax tree, maybe do syntax analysis to check for errors and put that through your handling code. If you were hardcore, that is. I don't think this is what you're looking for, so i'll just suggest that for now you try coding an FSM and look for key words.


For example, you could just read a line one word at a time (you could tokenize the string and put each token in a list, it's the same thing, but safer) and for the first word check if it's a key word like "bind" or "modify" using if or switch statements. Then in each if/case statement you'd just have the code that handles this command.



So a string "modify fov 90" would create a list of 3 tokens "modify", "fov" and "90" and in if( stringlist[0] == "modify" ) you could have more if statements or you could have a global map that takes a string and returns a pointer to the fov variable (or the variable itself) and you'd stringtofloat the 3rd token and use that to set fov. Just don't forget to error check everything, like if stringtofloat returns an error or if you enter this "if" and the stringlist has only 2 elements, etc.


If you want a robust parser, there are a few availible, for example http://www.antlr.org/. The site looks like any terribly boring IT site, but the library is easy to use, just i don't think you actually need that.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...