Search code examples
c#unity-game-engineoculus

Basic Oculus Leaderboards.GetEntries API usage throws errors


Trying to do a simple call to the leader board API fails. I even tried copy and pasting some of the example code for the documentation but they also all fail with the same error. Below is the most simple version I could come up with and it also fails. My Oculus developer settings are set up in unity.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Oculus.Platform;
using Oculus.Platform.Models;

public class leaderBoard : MonoBehaviour {

    void Start () {
        Leaderboards.GetEntries("POC-Score", 10, LeaderboardFilterType.None, LeaderboardStartAt.Top).OnComplete(GetEntriesCallback);
    }

    void GetEntriesCallback(Message<LeaderboardEntryList> msg)
    {
        Debug.Log("-----");
        Debug.Log(msg.Data);
        Debug.Log("-----");
    }
}

And I get the following error

NullReferenceException: Object reference not set to an instance of an object leaderBoard.Start () (at Assets/leaderBoard.cs:10)


Solution

  • Before calling Leaderboards.GetEntries, you must initialize the SDK using the App ID then do an entitlement check. If that passes, you can then call the Leaderboards.GetEntries function. Let's say that your App ID is 857362746724, initialize it as:

    Core.Initialize("857362746724");
    

    Now, do an entitlement check. If successful, call the Leaderboards.GetEntries function:

    void checkEntitlement()
    {
        Entitlements.IsUserEntitledToApplication().OnComplete(
        (Message msg) =>
        {
            if (msg.IsError)
            {
                // User is NOT entitled.
                Debug.Log("Error: User not entitled");
            } else 
            {
                // User IS entitled
                Debug.Log("Success: User entitled");
                checkEntry();
            }
        }
    );
    }
    
    void checkEntry()
    {
        Leaderboards.GetEntries("POC-Score", 10, LeaderboardFilterType.None, LeaderboardStartAt.Top).OnComplete(GetEntriesCallback);
    }
    
    void GetEntriesCallback(Message<LeaderboardEntryList> msg)
    {
        Debug.Log("-----");
        Debug.Log(msg.Data);
        Debug.Log("-----");
    }
    

    With the Start function, it should look like something below:

    void Start()
    {
        Core.Initialize("857362746724");
        checkEntitlement();
    }