Thanks for the link however it takes me to the step I am up to but not past it
I have a fully working monogame on my XboxOne. I can play it perfectly with a controller, it saves to the storage, and works as one would expect. I havenāt had any issues
However the final step is that an XboxOne game must have a signed in user and the gamer tag displayed. This is done by accessing the Xbox SDK. Unfortunately this is the part that I cant find documented anywhere on the web
It will be something like:
Step 1/ Import SDK
Step 2/ Initialise the user
Step 3/ Get the signed in user asynchronously
Step 4/ Handle sign outs, offline etcā¦
However I do not know enough about what is going on to translate it. I also donāt know when to initialise the user and when to start the functions
I would very much appreciate if somebody who has a monogame in the Xbox store to post the code they used to handle the user sign in and obtain the user profile
So this is a pretty low effort response and I apologize about that, but here is what my file for handling Xbox Live sign in looks like.
using System;
using Engine;
using System.Diagnostics;
using Microsoft.Xbox.Services.System;
using Microsoft.Xbox.Services;
using Microsoft.Xbox.Services.Statistics.Manager;
using Microsoft.Xna.Framework;
namespace MonoGameTiles
{
public class XboxLiveObject : Entity
{
public XboxLiveUser CurrentUser { get; private set; }
public XboxLiveContext CurrentContext { get; private set; }
public StatisticManager StatsManager = null;
public string UserId
{
get
{
if (CurrentUser.IsSignedIn)
return CurrentUser.XboxUserId;
else
return null;
}
}
private readonly GameTimeSpan _timer_time_since_last_presence_post = new GameTimeSpan();
public XboxLivePresences CurrentPresence = XboxLivePresences.in_menus;
private XboxLivePresences _old_presence = XboxLivePresences.in_menus;
private bool _has_posted_initial_presence = false;
private bool _currently_attempting_sign_in = false;
public XboxLiveObject()
{
IsPersistent = true;
SignIn();
XboxLiveUser.SignOutCompleted += SignOutCompleted;
}
private void SignOutCompleted(object sender, SignOutCompletedEventArgs e)
{
if (StatsManager != null)
{
StatsManager.RequestFlushToService(e.User);
StatsManager.DoWork();
StatsManager.RemoveLocalUser(e.User);
StatsManager.DoWork();
StatsManager = null;
}
CurrentContext = null;
Action upon_returning_to_menu = delegate
{
UserData.ResetUserData();
SaveLoadSettings.Load();
};
Room_Main.ReturnToMainMenuAndShowMessage(new System.Collections.Generic.List<string>() { "User logged out of Xbox Live" }, 50, upon_returning_to_menu);
}
public async void SignIn(bool attempt_silent = true)
{
if (_currently_attempting_sign_in)
return;
_currently_attempting_sign_in = true;
CurrentUser = new XboxLiveUser();
if (!CurrentUser.IsSignedIn)
{
var coreDispatcher = Windows.ApplicationModel.Core.CoreApplication.GetCurrentView().CoreWindow.Dispatcher;
if (attempt_silent)
{
try
{
await CurrentUser.SignInSilentlyAsync(coreDispatcher);
}
catch
{
Debug.WriteLine("SignInSilentlyAsync Threw Exception");
}
}
if (!CurrentUser.IsSignedIn)
{
Debug.WriteLine("Silent Sign-In failed, requesting sign in");
try
{
await CurrentUser.SignInAsync(coreDispatcher);
}
catch
{
Debug.WriteLine("SingInAsync Threw Exception");
}
}
}
if (CurrentUser.IsSignedIn)
{
CurrentContext = new XboxLiveContext(CurrentUser);
StatsManager = StatisticManager.SingletonInstance;
StatsManager.AddLocalUser(CurrentUser);
StatsManager.DoWork();
UserData.ResetUserData();
SaveLoadSettings.Load();
if (!_has_posted_initial_presence)
{
SetPresenceAsync(XboxLivePresences.in_menus);
_has_posted_initial_presence = true;
}
AchievementSystem.CheckEarnedFavoritesCount();
}
WriteInfo();
_currently_attempting_sign_in = false;
}
private async void SetPresenceAsync(XboxLivePresences presence)
{
if (CurrentContext == null)
return;
try
{
string presence_string = presence.ToString();
Microsoft.Xbox.Services.Presence.PresenceData presenceData = new Microsoft.Xbox.Services.Presence.PresenceData(CurrentContext.AppConfig.ServiceConfigurationId, presence_string);
await CurrentContext.PresenceService.SetPresenceAsync(true, presenceData);
Debug.WriteLine("Posted Presence: " + presence.ToString());
}
catch
{
Debug.WriteLine("Error Setting Presence");
}
}
public bool HasPrivilege(GamingPrivilege privilege)
{
string privilege_string = ((int)privilege).ToString();
var privileges = CurrentUser.Privileges.Split(' ');
foreach (var item in privileges)
{
if (privilege_string == item.Trim())
return true;
}
return false;
}
public void RunExitCleanup()
{
if (CurrentUser.IsSignedIn)
{
StatsManager.RequestFlushToService(CurrentUser);
StatsManager.DoWork();
StatsManager.RemoveLocalUser(CurrentUser);
StatsManager.DoWork();
}
CurrentContext = null;
CurrentUser = null;
}
public override void onUpdate(GameTime gameTime)
{
base.onUpdate(gameTime);
if (_old_presence != CurrentPresence && _timer_time_since_last_presence_post.TotalMilliseconds > 10000)
{
_timer_time_since_last_presence_post.Mark();
_old_presence = CurrentPresence;
SetPresenceAsync(CurrentPresence);
}
}
public void WriteInfo()
{
Debug.WriteLine("############ Xbox Live Info ############");
Debug.WriteLine(CurrentUser.XboxUserId);
Debug.WriteLine(CurrentUser.WebAccountId);
Debug.WriteLine("############ Xbox Live Info ############");
}
}
}
Note that this is a singleton entity (using my own entity system), but thatās really just so it can have itās own update loop where I set the rich presence string if needed.
Thereās a lot of stuff here thatās specific to my game, but I just figured I could give you some sort of idea of how a person might set it up. Also feel free to learn from my mistakes , Microsoft is very liberal with the way it throws exceptions which was really odd for me. Things such as failed sign in attempts or failed rich presence posts will throw exceptions (even if it failed for a standard reason).
Let me know if you need an example of achievements, Iāve got that working too. In fact, Iāve got everything working on the Xbox right now except for the fact that the game closes if a person signs out of their account⦠which was actually the reason I was browsing the forums and stumbled on your question in the first place lol.
Hey there, so Iāve got a couple questions. You mentioned XDP, XDP is not where you set up your game if you are using UWP (which is what my example is for). I actually set up my game in XDP the first time out if confusion and learned the hard way that for UWP games you have to set it up in the Windows Dev Center. Though in my defense they didnāt even have an option for Xbox games in the Dev Center at the time .
Secondly, how often are you attempting to set the rich presence string? Thereās a reason my code checks to see how long itās been since the last set. If I remember right, setting it more than 3 times within 15 seconds will get you throttled by the server, if you get throttled the game will throw a failure to post exception. Personally I donāt agree with the idea of throwing an exception for something not working server side, but Microsoft seems to love exceptions.
Thanks for your reply!!!
Firstly, I did set up my game in the Windows Dev Center , but I created Sandbox and made service configurations in XDP. Then I associated UDC product with the XDP title. Since the rich presence string is a part of service configurations, I set that in XDP.
Secondly, I did set the rich presence strings very frequently⦠However , when I first set that , this exception has appeared.
I want to know if I use this API incorrectly? Or if I need to make other configurations and get data from the server ?
Iām not sure of your exact problem yet, but I would start with setting up your service configuration in Windows Dev Center, if your game is a UWP game you wonāt use XDP at all. Again, I was confused on this too. Also, when you set up your game in Windows Dev Center you have to create it as a sandbox project if I remember right (I just know I had to delete and recreate mine).
I would reach out to IDDS, they are the ones in charge of making sure youāre set up properly in Dev Center. Once thatās done you can see where you stand.
Also, in your code when AddUser(XboxLiveUser user) gets called is that user already logged in? Itās apparently VERY IMPORTANT (for what reason I donāt know) that when you create the XboxLiveContext the user is already signed in. If not, parts of Xbox Live will work and other parts wonāt, I struggled with this for days before figuring it out. https://forums.xboxlive.com/questions/65290/rich-presence-setpresenceasync-throws-bad-request.html
Itās something good to know up front, basically you have to wrap EVERY call to Xbox Live in a try/catch block. Iām really not used to programming in that mindset whatsoever, in practice I rarely use try/catch.
I have set the Rich Presence Strings successfully ! The real reason why my game threw that exception is I did not enable Internet (Client) capability in VS.(As this picture shows)
Since I could set up achievements successfully without this configuration , I ignored that.
I was inspired by an answer in the Xbox Developer Forum and Iām so grateful for your help !!!
Does anybody knows if (and how) a second player can be logged in into the second controller?
In PS4 when you connect a second controller youāre asked to log in the new controller with another PS4 account (or guest), so when playing N-player games itās great because it can display your gamertag.
Iām porting a 2 player game and Iād like to add this feature, but Iām unable to find out how.
(I donāt know if itās even possible as Iāve just had the X1 for a month and havenāt played a 2p game with it yet)
I have a very preliminar support of leaderboards in one of my games.
You first have to query the leaderboard. In example:
StatisticManager statsManager;
var query=new LeaderboardQuery();
query.MaxItems=numScores;
statsManager.GetLeaderboard (user,leaderboardId,query);
afterwards you have to manually query statsManager with DoWork (in example, in every Update cycle, or through a Thread) which returns you different events.
Try/catch it, as DoWork can throw exceptions.
var eventList=live.statsManager.DoWork();
foreach (var ev in eventList)
{
if (ev.EventType==StatisticEventType.GetLeaderboardComplete)
{
var args=ev.EventArgs as LeaderboardResultEventArgs;
if (args!=null && args.Result!=null)
{
processLeaderboardResults (args.Result);
leaderboardAltered=true;
}
}
}
Inside processLeaderboardResults, you do a
foreach (var row in result.Rows)
and you have each leaderboard entry in those rows. row.GamerTag, row.Rank , ā¦
What you do with those (i.e. put them in a list to display them ) itās all up to you.
DoWork() can throw exceptions too!? Dang, everything needs try/catch.
Also @KakCAT , I believe games can be set up in āmulti-user modeā but Iāve never messed with it myself. Essentially your game needs to be structured that way from the start as it will affect how you handle Xbox Live (to my understanding).
It is indeed complicating a lot the sign in process and specially the savegames, I have to think wether using or not. After all, you can also control several gamepads without loging in.
Yeah, my game has a local coop mode but I just stick with the single user mode. The second player is essentially just playing as a guest on the main playerās account and any achievements they unlock together are just unlocked for the primary player.