How To: Handle command line arguments (launch parameters) in XNA.

Sometimes games use command line arguments such as “-window” so that games can easily be launched with these options for whatever reason. (In my case, they like to launch in stupid resolutions that my monitor doesn’t support ¬¬).
It’s always nice to allow for this type of launch option for those that want it so here I’ll explain how to handle these options.

First thing you’re going to want to do is open up your Game1.cs file and find the constructor Game1(). What you’ll want to do is change it to look like this:





If you try to build now, you’ll get an error, because you haven’t told the program the arguments Game1 needs. To solve this, head over to Program.cs and make change the new Game1() to look like this:









Now we’re all set up to pass the arguments to the Game1 class where we can handle them when the game is constructed.
Everything will run fine now, but we have no way of handling any commands that get passed to it which is useless, so I’ll explain how that works now so you can handle them, I’ll be using the option “-skipsplash” which skips all the introduction stuff before the menu screen.

Go back to Game1.cs and to our new Game1(string[] args), what were going to do is convert the array of strings to a List so we can loop through it and check the arguments provided against our coded in ones, so to do that add this:






First we check if there are any arguments added, if so create a list. If not we don’t bother because there’s no point.
Then we use a foreach loop to go through the list we just made, we then use an if statement to check if the string equals our launch option, here it’s “-skipsplash”, if it does, we tell the game what to do, in my case it’s to change the GameState to the menu as it’s initialised as the intro.
Obviously you can have as many launch options as you want you just had to add more if statements to the foreach loop to handle them.

Now that you’re done, build your project, create a shortcut to the .exe and add one of your launch options to test it out.