The GamerServices
namespace within Microsoft.Xna.Framework
is one of the most, er, entertaining collections of classes available in XNA. It gives you access to the Xbox Live avatar, friends, and player settings! Because this action-packed namespace has so much to offer, we will spend quite a bit of time here and cover every detail. First, this chapter has a quick reference to the GamerServices
namespace with all of its great classes, structures, and enumerations. Then, we jump into the code and experiment with these great features!
As a side note, most of the features covered in this chapter require an Xbox Live Gold account, and it goes without saying that you need to have your Xbox 360 connected to the Net in order to retrieve the information provided by these features in the GamerServices
namespace. If you don’t have your Xbox 360 online, most of the examples in this chapter will not work. As for the Windows version of the examples, these features don’t make much sense on Windows, so there will likely be some null values and connectivity failures even though the code will still compile for the Windows platform. This chapter is primarily focused on the Xbox 360. Here’s what I’ll be covering:
GamerServices
is one of the largest namespaces in the XNA Framework. This namespace provides information about the gamer profile, which is basically an Xbox Live membership account with all the stored data about that player’s experiences on Xbox Live, such as games played, achievements, friends, avatar, gamer profile picture, game rank leaderboards, and related information. There’s a lot of information to cover in this chapter, which begins with a quick reference to the most important details first, followed by a sample program.
Microsoft disclaims the availability of the GamerServices
features on the Windows platform with this comment: “Games for Windows Live is unavailable to finished games. This functionality is not included in the redistributable version of the XNA Framework. A game that attempts to use these components without XNA Game Studio installed will result in a GamerServicesNotAvailableException.”
Following are the classes found within the GamerServices
namespace.
public sealed class Achievement
Describes a single achievement, including the achievement name, description, picture, and whether it has been achieved by the currently signed-in gamer. Unfortunately, you can’t use achievements in indie games unless they have been officially published on Xbox Live (which involves a contract with Microsoft).
Microsoft disclaimer: “This class and all related methods and properties will only retrieve a calculated result for titles approved to access Xbox Live services through the Xbox Live Registered Developer Program. For Xbox Live Indie games, the properties in this class will not return a calculated result and related methods will report a NotSupportedException.”
| Gets the localized achievement description string. |
| Gets whether this achievement should be displayed before it is earned. |
| Gets the date at which this achievement was earned. |
| Gets whether this achievement was earned while online. |
| Gets the amount of gamer score awarded for earning this achievement. |
| Gets the localized description of the steps necessary to earn the achievement. |
| Gets whether the current player has earned this achievement. |
| Gets the achievement key string. |
| Gets the localized achievement name string, for display to the user. |
Public Methods | |
| Gets the image associated with this achievement. |
Collection holding the achievements belonging to a signed-in gamer.
Microsoft disclaimer: “For Xbox Live Indie games, the properties in this class will not return a calculated result and related methods will report a NotSupportedException.”
Public Properties | |
| Gets the number of objects in the collection. |
| Properties that retrieve an achievement from the collection. |
Public Methods | |
| Gets an enumerator that can iterate through this |
Provides methods and properties for animating an avatar using standard animations (for example, celebrate).
Public Properties | |
| Gets the current position of the bones at the time specified by |
| Gets or sets the current time position in the animation. |
Gets the expression of the related animation at the current time position. | |
| Gets the disposed state of the avatar animation. |
| Gets the length of the current animation. |
Public Methods | |
| Updates the current time position of the avatar animation. |
Provides access to the methods and properties of the description data for an avatar.
Public Properties | |
| Gets the body type of the avatar based on the description data. |
| Internal description buffer of the avatar, stored as a byte buffer. |
| Height of the avatar, from the feet to the top of the head. |
| Determines whether the internal data buffer is valid. |
Public Methods | |
| Begins the process of getting an |
| Creates an |
| Ends the process of getting an |
Public Events | |
| Occurs when a gamer’s avatar changes. |
Provides properties and methods for rendering a standard avatar.
Public Fields | |
| Number of bones in the avatar model. |
Public Properties | |
| Gets or sets the color of the ambient light used by the avatar renderer. |
| Gets the collection of bind pose positions for each bone of the avatar model. |
| Gets the loaded state of the avatar. |
| Gets or sets the color of the directional light used by the avatar renderer. |
| Gets or sets the direction of the directional light used by the avatar renderer. |
Collection of the parent indices for each bone in the related | |
| Gets or sets the projection matrix for the avatar. |
| Gets the state of the avatar. |
| Gets or sets the view matrix for the avatar. |
| Gets or sets the world matrix for the avatar. |
Public Methods | |
| Draws the avatar to the current render target. |
Represents the complete friends list of a local gamer.
Public Properties | |
| Gets the number of elements contained in the |
| Gets the element at the specified index. |
Protected Properties | |
| Returns the |
Public Methods | |
| Returns a strongly typed |
Provides the presence information of a friend of the local gamer.
Public Properties | |
| Gets the display name string for the gamer. |
| Gets whether the local gamer who requested the friends list has received a friend request from this gamer. |
| Gets whether the local gamer who requested the friends list has sent a friend request to this gamer. |
| Gets the gamertag string. |
| Gets whether this friend currently has voice capability. |
| Gets whether this friend has accepted an invitation from the local gamer who requested the friends list. |
Gets whether the local gamer who requested the friends list has received an invitation from this friend. | |
| Gets whether this friend has rejected an invitation from the local gamer who requested the friends list. |
| Gets whether the local gamer who requested the friends list has sent an invitation to this friend. |
| Gets whether this friend is currently away from the computer or console. |
| Gets whether this friend is currently busy. |
| Gets whether this friend is currently in a public session that can be joined. |
| Gets whether this friend is currently online. |
| Gets whether this friend is currently playing a game. |
| Gets an object that can be used to write leaderboard statistics for this gamer. |
| Gets a presence string describing what this friend is currently doing. |
| Gets or sets a custom object that can be used to attach arbitrary user-defined data to the gamer. |
Public Methods | |
| Starts an asynchronous profile read operation. |
| Ends an asynchronous profile read operation. |
| Reads profile data for this gamer. |
Describes a gamer’s preferred settings.
Public Properties | |
| Gets whether the gamer prefers to use controller buttons to accelerate in racing games. |
| Gets whether the title should automatically correct the gamer’s aim. |
| Gets whether the title should automatically center the view when the gamer moves. |
| Gets whether the gamer prefers to use controller buttons to brake in racing games. |
| Gets the preferred controller sensitivity setting for this gamer. |
| Gets the preferred difficulty setting for this gamer. |
| Gets whether the gamer prefers to invert the y-axis input of the controller. |
| Gets whether the gamer prefers to drive a manual shift transmission in racing games. |
| Gets whether the gamer prefers to move using the right thumbstick. |
| Gets the preferred color for the player character. |
Gets the gamer’s preferred camera angle for racing games. | |
| Gets a secondary color selection for the player character. |
Abstract base class for types that represent game players (profiles that have an associated gamertag). The concrete types SignedInGamer
and NetworkGamer
derive from this. For the NetworkGamer
class, see Chapter 10, “Net.”
Public Properties | |
| Gets the display name string for the gamer. |
| Gets the gamertag string. |
| Gets an object that can be used to write leaderboard statistics for this gamer. |
| Represents a collection of all gamers on the local system. |
| Gets or sets a custom object that can be used to attach arbitrary user-defined data to the gamer. |
Public Methods | |
| Begins the process of getting a Gamer object for a specified gamertag. |
| Begins the asynchronous process of getting the partner token for this gamer. |
| Starts an asynchronous profile read operation. |
| Ends the process of getting a |
| Retrieves the results from an asynchronous request for the partner token for this gamer. |
| Ends an asynchronous profile read operation. |
| Returns a Gamer object for the specified gamertag. |
| Gets the partner token for this gamer. |
| Reads profile data for this gamer. |
Represents a collection of gamers. This collection cannot be modified and is updated automatically during the call to Update()
. This class inherits from Rea
dOnlyCollection and uses the GamerCollectionEnumerator
structure, but exposes no new properties or methods of its own.
Provides properties to set the rich presence state for a locally signed-in gamer profile.
Public Properties | |
| Gets or sets the current presence mode for this gamer. |
| Gets or sets a custom presence value. |
Thrown if a gamer services or multiplayer API is called without a valid, signed-in profile. This class exposes no new properties or methods beyond its base class.
Describes what operations a gamer is allowed to perform. GamerPrivileges
can be defined by parental control settings, and will also be set automatically in response to things like age, region, and whether the gamer has a Live Gold or Live Silver account. Games do not need to explicitly check privileges because a GamerPrivilegeException
will be thrown if they try to perform an unsupported operation, but these privilege bits may be useful to detect that an operation is unavailable before calling it so the relevant menu option can be grayed out.
Public Properties | |
| Checks whether this gamer is allowed to send and receive communications using voice, text, messaging, or game invites. |
| Checks whether this gamer is allowed to play in online multiplayer sessions. |
| Checks whether this gamer is allowed to access premium content. |
| Checks whether this gamer is allowed to view the profiles of other gamers. |
| Checks whether this gamer is allowed to purchase content from Live Marketplace. |
| Checks whether this gamer is allowed to trade content with other gamers. |
| Checks whether this gamer is allowed to access user content that was created by other gamers. |
Profile settings describing information about a gamer such as the gamer’s motto, reputation, and picture. This data is accessible for both locally signed-in profiles and remote gamers that you are playing with in a multiplayer session.
Public Properties | |
| Gets the |
| Gets the |
| Gets the gamer motto string. |
| Gets the region of this gamer. |
| Gets the gamer reputation, as a number of stars ranging 0 to 5. |
| Gets the number of titles this gamer has played. |
| Gets the total number of achievements this gamer has obtained. |
Public Methods | |
| Gets the gamer picture associated with this profile. |
Wraps the functionality of the GamerServicesDispatcher
.
Public Methods | |
| Initializes the |
| Updates the |
Implements the Windows-specific portion of a GamerServicesDispatcher
class.
Public Properties | |
| Determines whether |
| Gets or sets the handle to the underlying game window. |
Public Methods | |
| Initializes gamer-services functionality for the game, automatically choosing the most appropriate gamer-service type for the platform. |
| Updates the status of gamer services and raises related events. |
The exception thrown when a game (title) update is required in order to use Xbox Live.
Provides access to the Guide user interface.
Public Properties | |
| Gets or sets the current state of the screen saver. |
| Determines whether the game is running in limited trial mode. |
| Determines whether a Guide user-interface screen is active. |
| Determines where notifications appear on the screen. |
| Allows titles to simulate trial mode restrictions when testing using the development configuration. |
Public Methods | |
| Overloads for showing the software keyboard interface asynchronously. |
| Begins the process of displaying a message box with the specified parameters. |
| Delays system notifications for the specified amount of time. |
| Ends the display of the keyboard input dialog box. |
| Ends the display of a message box. |
| Shows the Compose Message user interface. |
| Shows the Friend Request user interface. |
| Shows the Friends user interface. |
| Overloads to display the Game Invitation user interface. |
| Shows the Gamer Card user interface. |
| Displays the Marketplace user interface. |
| Shows the Messages user interface. |
| Shows the Xbox Live Party screen. |
| Shows the Play with Party Member screen. |
| Shows the Player Review user interface. |
| Shows the Players user interface. |
| Shows the user interface a gamer uses for signing into Xbox Live. |
Thrown if an attempt is made to display a component of the Guide user interface when a Guide component is already displayed.
Represents the arguments passed to an InviteAccepted
event.
Public Properties | |
| The signed-in gamer who is accepting the game invitation. |
| Indicates if the invitation is for the current local session. |
Class representing a single row of a leaderboard, holding all the information a specific gamer has uploaded to the board.
Public Properties | |
| Provides access to the columns for this leaderboard entry. |
| Provides access to gamer information for this leaderboard entry. |
| Provides access to the rating associated with this leaderboard entry. |
Reads data from leaderboards.
Public Properties | |
| Provides information about whether there are more leaderboard entries after the current page of entries. |
| Provides information about whether there are more leaderboard entries before the current page of entries. |
| Gets the collection of leaderboard entries for the leaderboard. |
| The identity of the leaderboard. |
| Gets the index of the leaderboard entry at the start of the current page. |
| Provides information about the total number of entries in the leaderboard. |
| Begins an asynchronous read of the page of leaderboard data after the current page. |
| Begins an asynchronous read of the page of leaderboard data before the current page. |
| Methods for reading a leaderboard asynchronously. |
| Checks the result of an asynchronous |
| Checks the result of an asynchronous |
| Checks the result of an asynchronous |
| Reads the next page of leaderboard data synchronously. |
| Reads the previous page of leaderboard data synchronously. |
| Method for synchronous reading of leaderboard data. |
Records leaderboard data for players in a NetworkSession
. Data can be written at any time during gameplay, but is only flushed to the server when the host calls EndGame
. Data may be written for both local and remote gamers using the following rules:
Ranked sessions can write to both arbitrated and non-arbitrated leaderboards.
Other session types can only write to non-arbitrated leaderboards.
Arbitrated statistics should be written by all machines for all gamers.
Non-arbitrated statistics should only be written by their local machine.
In ranked sessions, all machines should report TrueSkill
for all gamers.
In other session types, only the host should report TrueSkill
data.
Leaderboards can only be written while in a NetworkSession
, and while in the gameplay state rather than in the lobby. To write scores from a single-player game, create a session using NetworkSessionType. LocalWithLeaderboards
.
Public Methods | |
| Gets a leaderboard entry that can be used to write to a specified leaderboard. |
Holds a set of properties used to define presence states or leaderboard column values.
Public Properties | |
| Gets the number of elements contained in this |
| Gets or sets the element with the specified key. |
Public Methods | |
| Determines whether the |
| Returns an enumerator that iterates through a collection of key/value pairs that represent elements in the |
| Gets the value of the property with the specified key, as a |
| Gets the value of the property with the specified key, as a |
| Gets the value of the property with the specified key, as a |
| Gets the value of the property with the specified key, as a |
| Gets the value of the property with the specified key, as a |
| Gets the value of the property with the specified key, as a |
| Gets the value of the property with the specified key, as a |
| Gets the value of the property with the specified key, as a |
| Gets the value of the property with the specified key, as a |
| Method to set the value of the property with the specified key. |
| Determines if a value for the specified key exists in the |
Represents the arguments passed to a SignedIn
event.
Represents a gamer (a profile that has an associated gamertag) on the local system. This class inherits from the abstract Gamer
class, so refer to that class for more details.
Public Properties | |
| Describes a gamer’s preferred settings. |
| Determines whether the gamer is the guest of an Xbox Live–enabled profile. |
| Determines whether the gamer has an Xbox Live–enabled profile. |
| Gets the current party size. |
| Gets the index of the gamer. |
| Gets an object that may be used to set the rich presence state for this gamer. |
| Describes what operations a gamer is allowed to perform. |
Public Methods | |
| Starts an asynchronous operation to award an achievement to a locally signed-in gamer. |
| Starts an asynchronous achievement query operation. |
| Ends an asynchronous achievement award operation. |
| Ends an asynchronous achievement query operation. |
| Returns the collection of all achievements that may be earned by this gamer. |
| Reads the friends list of this local gamer. This includes both the gamertags of the friends and their current presence information. |
| Queries whether the specified gamer is a friend of this local gamer. |
| Determines if the microphone associated with this signed-in gamer is a headset. |
Public Events | |
| Occurs when a new gamer signs into the local system. |
| Occurs when a gamer signs out on the local system. |
Represents a collection of gamers on the local system.
The following interface is found in the GamerServices
namespace.
Provides methods and properties for animating an avatar using custom animations.
Public Properties | |
| Gets the current position of the bones at the time specified by |
| Gets or sets the current time position in the animation. |
| Gets the expression of the related animation at the current time position. |
| Gets the length of the current animation. |
Public Methods | |
| Updates the current time position of the avatar animation. |
The following structures are found in the GamerServices
namespace.
Contains the various components of the avatar’s face, such as the left and right eyebrows.
| Gets or sets the current texture for the avatar’s left eye. |
| Gets or sets the current texture for the avatar’s left eyebrow. |
| Gets or sets the current texture for the avatar’s mouth. |
| Gets or sets the current texture for the avatar’s right eye. |
| Gets or sets the current texture for the avatar’s right eyebrow. |
Provides the ability to iterate through the gamers in a GamerCollection
.
Public Properties | |
| Gets the current element in the |
Public Methods | |
| Advances the enumerator to the next element of the |
Following are the enumerations in the GamerServices
namespace.
Defines standard animations for avatars. Note that additional animations can be downloaded from the XNA Web site as a collection, as can a sample bone structure mesh for use in creating your own custom avatar animations.
Gender-neutral, celebrating. | |
| Gender-neutral, applauding. |
| Female, angry. |
| Female, confused. |
| Female, cry. |
| Female, checking nails. |
| Female, fixing shoe. |
| Female, looking around. |
| Female, shifting weight from one foot to another. |
| Female, laughing. |
| Female, shocked or surprised. |
| Female, yawning. |
| Male, angry. |
| Male, confused. |
| Male, crying. |
| Male, checking hand. |
| Male, looking around. |
| Male, shifting weight from one foot to another. |
| Male, stretching. |
| Male, laughing. |
| Male, surprised. |
| Male, yawning. |
| Gender-neutral, standing, variation 0. |
| Gender-neutral, standing, variation 1. |
| Gender-neutral, standing, variation 2. |
| Gender-neutral, standing, variation 3. |
| Gender-neutral, standing, variation 4. |
| Gender-neutral, standing, variation 5. |
| Gender-neutral, standing, variation 6. |
| Gender-neutral, standing, variation 7. |
| Gender-neutral, waving. |
Defines a list of the useful bones of the avatar model.
| Left ankle. |
| Right ankle. |
| Lower back. |
| Upper back. |
| Left collar. |
| Right collar. |
| Left elbow. |
| Right elbow. |
| Left index finger, second joint. |
| Right index finger, second joint. |
| Left index finger, third joint. |
| Right index finger, third joint. |
| Left index finger, first joint. |
| Right index finger, first joint. |
| Left middle finger, second joint. |
| Right middle finger, second joint. |
| Left middle finger, third joint. |
| Right middle finger, third joint. |
| Left middle finger, first joint. |
| Right middle finger, first joint. |
| Left ring finger, second joint. |
| Right ring finger, second joint. |
| Left ring finger, third joint. |
| Right ring finger, third joint. |
| Left ring finger, first joint. |
| Right ring finger, first joint. |
| Left pinky finger, second joint. |
| Right pinky finger, second joint. |
| Left pinky finger, third joint. |
| Right pinky finger, third joint. |
| Left pinky finger, first joint. |
| Right pinky finger, second joint. |
| Left thumb, second joint. |
| Right thumb, second joint. |
| Left thumb, third joint. |
| Right thumb, third joint. |
Left thumb, first joint. | |
| Right thumb, first joint. |
| Head. |
| Left hip. |
| Right hip. |
| Left knee. |
| Right knee. |
| Neck. |
| A separate object held in the left hand. |
| A separate object held in the right hand. |
| Root bone of the avatar skeleton. |
| Left shoulder. |
| Right shoulder. |
| A special bone located near the left hand of the avatar model. |
| A special bone located near the right hand of the avatar model. |
| Left toe. |
| Right toe. |
| Left wrist. |
| Right wrist. |
Defines the standard animation textures for an avatar’s eyes.
| Angry eye position. |
| Blinking eye position. |
| Confused eye position. |
| Happy eye position. |
| Laughing eye position. |
| Looking down eye position. |
| Looking left position. |
| Looking right eye position. |
| Looking up eye position. |
| Neutral eye position. |
| Sad eye position. |
| Shocked eye position. |
| Sleeping eye position. |
| Yawning eye position. |
Defines the standard animation textures for an avatar’s eyebrows.
| Angry eyebrow position. |
| Confused eyebrow position. |
| Neutral eyebrow position. |
| Raised eyebrow position. |
| Sad eyebrow position. |
Defines the standard animation textures for an avatar’s mouth.
| Angry mouth position. |
| Confused mouth position. |
| Happy mouth position. |
| Laughing mouth position. |
| Neutral mouth position. |
| Phonetic “ai” mouth position (for lip sync). |
| Phonetic “dth” mouth position (for lip sync). |
| Phonetic “ee” mouth position (for lip sync). |
| Phonetic “fv” mouth position (for lip sync). |
| Phonetic “l” mouth position (for lip sync). |
| Phonetic “o” mouth position (for lip sync). |
| Phonetic “w” mouth position (for lip sync). |
| Sad mouth position. |
| Shocked mouth position. |
Indicates avatar state.
| The avatar is still loading required assets. |
| Loading is complete. |
| The avatar is unavailable. |
Indicates how sensitive this gamer prefers controller input to be.
Indicates how difficult this gamer likes things to be.
| Below-average difficulty. |
| Above-average difficulty. |
| Average difficulty. |
Settings defining the status string that will appear when you view a friend through the Xbox Live Guide or on Xbox.com. Use the PresenceMode
property to set this option.
| Displays the Arcade Mode status string. |
| Displays the At Menu status string. |
| Displays the Battling Boss status string. |
| Displays the Campaign Mode status string. |
| Displays the Challenge Mode status string. |
| Displays the Configuring Settings status string. |
| Displays the Co-Op: Level status string. Includes a numeric value specified with |
| Displays the Co-Op: Stage status string. Includes a numeric value specified with |
| Displays the Cornflower Blue status string. |
| Displays the Customizing Player status string. |
| Displays the Difficulty: Easy status string. |
| Displays the Difficulty: Extreme status string. |
| Displays the Difficulty: Hard status string. |
| Displays the Difficulty: Medium status string. |
| Displays the Editing Level status string. |
| Displays the Exploration Mode status string. |
| Displays the Found Secret status string. |
| Displays the Free Play status string. |
| Displays the Game Over status string. |
| Displays the In Combat status string. |
| Displays the In Game Store status string. |
| Displays the Level status string. Includes a numeric value specified with |
| Displays the Local Co-Op status string. |
| Displays the Local Versus status string. |
| Displays the Looking For Games status string. |
| Displays the Losing status string. |
| Displays the Multiplayer status string. |
| Displays the Nearly Finished status string. |
| Displays the No Presence String Displayed status string. |
| Displays the On a Roll status string. |
| Displays the Online Co-Op status string. |
| Displays the Online Versus status string. |
| Displays the Outnumbered status string. |
| Displays the Paused status string. |
| Displays the Playing Minigame status string. |
| Displays the Playing With Friends status string. |
| Displays the Practice Mode status string. |
| Displays the Puzzle Mode status string. |
| Displays the Scenario Mode status string. |
| Displays the Score status string. Includes a numeric value specified with |
| Displays the Score is Tied status string. |
| Displays the Setting Up Match status string. |
| Displays the Single Player status string. |
| Displays the Stage status string. Includes a numeric value specified with |
| Displays the Starting Game status string. |
| Displays the Story Mode status string. |
| Displays the Stuck on a Hard Bit status string. |
| Displays the Survival Mode status string. |
| Displays the Time Attack status string. |
| Displays the Trying For Record status string. |
| Displays the Tutorial Mode status string. |
| Displays the Versus Computer status string. |
| Displays the Versus: Score status string. Includes a numeric value specified with |
| Displays the Waiting For Players status string. |
Displays the Waiting In Lobby status string. | |
| Displays the Wasting Time status string. |
| Displays the Watching Credits status string. |
| Displays the Watching Cutscene status string. |
| Displays the Winning status string. |
| Displays the Won the Game status string. |
Describes the conditions in which a privilege is available.
| This privilege is not available for the current gamer profile. |
| This privilege is available for the current gamer profile. |
| This privilege is only available for friends of the current gamer profile. Use the |
The style of social gaming preferred by this Xbox Live member.
| Family-friendly gameplay. |
| Competitive gameplay. |
| Non-competitive gameplay. |
| Alternative approach to gameplay. |
| Unknown. |
Values used with LeaderboardIdentity.Create
to select which leaderboard to access.
| Best lifetime score for this player and his or her Xbox Live friends. |
| Best recent scores for this player and his or her Xbox Live friends. |
| Best lifetime times for this player and his or her Xbox Live friends. |
| Best recent times for this player and his or her Xbox Live friends. |
Values used to provide an outcome for a player’s leaderboard entry.
Defines the different icons for a message box.
| Displays the Alert icon. |
| Displays the Error icon. |
| No icon is displayed. |
| Displays the Warning icon. |
Determines where notifications appear on the screen.
| Positions the message box at the bottom of the screen and centered. |
| Positions the message box at the bottom-left of the screen. |
| Positions the message box at the bottom-right of the screen. |
| Positions the message box at the center of the screen. |
| Positions the message box at the center of the screen and left-aligned. |
| Positions the message box at the center of the screen and right-aligned. |
| Positions the message box at the top of the screen and centered. |
| Positions the message box at the top-left of the screen. |
| Positions the message box at the top-right of the screen. |
To demonstrate the GamerServices
namespace, I’ve prepared the following sample program, called GamerProfile
Demo, to print some information on the screen about the currently logged-in profile. You will need to run this example on your Xbox 360, as it will not display anything under Windows (where no gamer profile exists). You do not need to have an active Xbox Live membership for this to work, but some of the information is only useful for an active account. Figure 6.1 shows the program running on an Xbox 360 with my gamer profile displayed.
Figure 6.1. The GamerProfile
Demo displays information about your Xbox Live gamer profile, but it only runs on an Xbox 360.
The key to accessing the GamerServices
class is to add a new GamerServicesComponent
to the public Components
collection, usually in the constructor of the Game
class:
Components.Add(new GamerServicesComponent(this));
With the component added and running, you can access the currently signed-in gamer profiles—yes, that includes co-players currently using the Xbox. The following code iterates through the currently signed-in profiles to retrieve the name of each one. (Note: Normally, only one profile will be active at a time—your own profile.)
foreach (SignedInGamer gamer in SignedInGamer.SignedInGamers) { }
To grab just the default profile, you can just look at PlayerIndex.One
:
if (Gamer.SignedInGamers[PlayerIndex.One] != null) { }
To keep the profile information up to date, we need to add this line to the Update()
function:
GamerServicesDispatcher.Update();
Based on the reference information covered in this chapter and the SignedInGamer
class, the code in Listing 6.1 should provide some pointers into the type of data available from the gamer profile.
Example 6.1. Data in the Gamer Profile
using System; using System.Collections.Generic; using System.Linq; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Audio; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.GamerServices; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Media; using Microsoft.Xna.Framework.Net; using Microsoft.Xna.Framework.Storage; namespace GamerServices_Demo { public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch; SpriteFont font; GamerProfile profile; SignedInGamer gamer; string labels1 = "", labels2 = ""; string values1 = "", values2 = ""; Texture2D picture; public Game1() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; Components.Add(new GamerServicesComponent(this)); } protected override void Initialize() { base.Initialize(); } protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice); font = Content.Load<SpriteFont>("font1"); } protected override void UnloadContent() { } protected override void Update(GameTime gameTime) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit(); if (gamer == null) { if (Gamer.SignedInGamers[PlayerIndex.One] != null) { gamer = Gamer.SignedInGamers[PlayerIndex.One]; profile = gamer.GetProfile(); } } if (gamer != null) { if (picture == null) { picture = Texture2D.FromStream(this.GraphicsDevice, profile.GetGamerPicture()); } labels1 = "Gamertag: " + "Signed on: " + "Gamer score: " + "Gamer zone: " + "Motto: " + "Region: " + "Reputation: " + "Titles played: " + "Achievements: " + "Gamer picture: "; values1 = gamer.Gamertag + " " + gamer.IsSignedInToLive.ToString() + " " + profile.GamerScore.ToString() + " " + profile.GamerZone.ToString() + " " + profile.Motto + " " + profile.Region.Name + " " + profile.Reputation.ToString() + " " + profile.TitlesPlayed.ToString() + " " + profile.TotalAchievements.ToString() + " "; labels2 = "Privileges " + " AllowCommunication: " + " AllowOnlineSessions: " + " AllowProfileViewing: " + " AllowPurchaseContent: " + " AllowTradeContent: " + " AllowUserCreatedContent: " + "GameDefaults " + " AccelerateWithButtons: " + " AutoAim: " + " AutoCenter: " + " BrakeWithButtons: " + " ControllerSensitivity: " + " GameDifficulty: " + " InvertYAxis: " + " ManualTransmission: " + " MoveWithRightThumbStick: " + " RacingCameraAngle: "; values2 = " " + gamer.Privileges.AllowCommunication.ToString() + " " + gamer.Privileges.AllowOnlineSessions.ToString() + " " + gamer.Privileges.AllowProfileViewing.ToString() + " " + gamer.Privileges.AllowPurchaseContent.ToString() + " " + gamer.Privileges.AllowTradeContent.ToString() + " " + gamer.Privileges.AllowUserCreatedContent.ToString() + " " + " " + gamer.GameDefaults.AccelerateWithButtons.ToString() + " " + gamer.GameDefaults.AutoAim.ToString() + " " + gamer.GameDefaults.AutoCenter.ToString() + " " + gamer.GameDefaults.BrakeWithButtons.ToString() + " " + gamer.GameDefaults.ControllerSensitivity.ToString() + " " + gamer.GameDefaults.GameDifficulty.ToString() + " " + gamer.GameDefaults.InvertYAxis.ToString() + " " + gamer.GameDefaults.ManualTransmission.ToString() + " " + gamer.GameDefaults.MoveWithRightThumbStick.ToString() + " " + gamer.GameDefaults.RacingCameraAngle.ToString(); } GamerServicesDispatcher.Update(); base.Update(gameTime); } protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); spriteBatch.Begin(); if (Environment.OSVersion.Platform != PlatformID.Xbox) { labels1 = "This demo only runs on an Xbox 360"; values1 = ""; labels2 = ""; values2 = ""; } //print first column print(10, 10, labels1, Color.White); print(150, 10, values1, Color.White); //print second column print(340, 10, labels2, Color.White); print(610, 10, values2, Color.White); //draw gamer picture if (picture != null) { Vector2 size = font.MeasureString(labels1); Vector2 pos = new Vector2(150, size.Y); spriteBatch.Draw(picture, pos, Color.White); } spriteBatch.End(); base.Draw(gameTime); } void print(int x, int y, string text, Color color) { spriteBatch.DrawString(font, text, new Vector2((float)x, (float)y), color); } } }
The GamerServices
namespace contains a lot of amazing information about the gamer profile, including the gamer picture, gamer tag, friends list, and even the 3D avatar animations. We only scratched the surface of what’s available in GamerServices
with the sample program, but it should give you an idea about how to use this information in your own projects.
18.219.14.63