Search code examples
c#xnaxna-4.0

Changing variable values from external files in XNA 4.0


What is the easiest way to change the value of a variable in a XNA 4.0 game?

I have a few objects that I want to be able to show/hide (as a programmer not as a user) without rebuilding the game itself. I was trying to do this with XML but it proved to be to advanced for my level of knowledge of XNA. My idea is to have a file that will have a simple structure (name_of_variable1, value_of_variable1, name_of_variable2, value_of_variable2...). Since there is a limited number of variables that I need to change (4 or 5 bool variables - I can hold my drawing methods inside if-else statements and then control their execution with these bool variables); these file(s) will be standardized.

What I want to accomplish is this: when I publish my program, I want to have a file in it's "Content" folder that I can change in any text editor and when I start my program, it reads that file and sets the values of these variables accordingly (there will be only one file, that I will overwrite when necessary).

What is the simplest way to do this?

Thanks


Solution

  • I would recommend using CSV, XML or INI file formats, depending on the type of data you want stored.

    CSV is great for data in which you have a header, and lots of records of that data, like say Sprite_sheets.csv could contain headers ID, PATH, WIDTH, HEIGHT, and rows of sprite sheets that you use.

    INI is great for single settings variables, like FPS=60, FULLSCREEN=true, etc.

    XML is great for greater data structures, like your MAP, which contains TILES, OBJECTS, SPRITES inside, and those contain some other objects, etc.

    I usually just use CSV, and I found this really great C# CSV reader/writer: http://www.codeproject.com/Articles/86973/C-CSV-Reader-and-Writer

    But you could use whatever works for you.