In Focus

How to use LaunchParameters in XNA

In this mini-article I will show you how to use LaunchParameters.

In this mini-article I will show you how to use LaunchParameters.

LaunchParameters is a new addition in XNA 4.0 Beta.It helps you to store datas across the game project.Works just like Application and Session objects in ASP.NET.

As its a Dictionary object showing the structure is pointless.Lets make an example and see how to use it.

Add these codes on Initialize or Game1 constructor

LaunchParameters.Add("name", "Ibrahim Ersoy");
LaunchParameters.Add("profession", "XNA");
LaunchParameters.Add("salary", "No Way :)");
LaunchParameters.Add("country", "Turkey");

You can change the values as you like :)

Then to get them use something like that:

Window.Title = "Name: " + LaunchParameters["name"] +
               " Profession: " + LaunchParameters["profession"] +
               " Salary: " + LaunchParameters["salary"] +
               " Country: " + LaunchParameters["country"];

Thats it!

You can now store these parameters and call them anywhere you want.

To give a nice example you can add a GameComponent:

There you can define LaunchParameters using:

public SampleComponent1(Game game)  : base(game)
   game.LaunchParameters.Add("Name", "Ibrahim Ersoy");

And through your Game1 class you can call it:

SampleComponent1 sm1 = new SampleComponent1(this);
Window.Title = LaunchParameters["Name"];

And you can get the result in your Window's Title.

LaunchParameters is nice to use where you want to store variables across the application or game.