Sign In Register

How can we help you today?

Start a new topic
Answered

PostScore and Leaderboards.

TRANSFERRED FROM TICKETING SYSTEM AS TICKETS RESERVED FOR SERVICE OUTAGES, SERVICE ISSUES AND BUGS.



Clau Lup reported 23 minutes ago (Thu, 29 Jan at 9:50 am) via Portal Meta

Hey, I seem to have a problem with posting the score on GameSparks, I'm using this function, but any score made in the game it is not shown in the GameSparks Test Harrness, also I am trying to make a leaderboard (in the game) and I can't get to show me anything, note that I do not use NGUI, just the Unity new UI tools (4.6), can you guys help me out ? I will post the codes for both the score posting and the Leaderboard:


This one is for the score:


public void PostScores(string levelName, int score)

{

new GameSparks.Api.Requests.LogEventRequest_postScore().Set_score(score).Send((response)=>{

if(response.HasErrors){

Debug.Log("Sum Ting Wong");

}

else{

Debug.Log("it's ok");

}

});

}



And this one is for the Leaderboard:


public void GetLeaderboard()

{

for(int i=0;i<entries.Count;i++)

{

Destroy(entries[i]);

}

entries.Clear ();


new LeaderboardDataRequest_highScoreleaderboard().SetEntryCount(5).Send((response)=>{

foreach(var entry in response.Data)

{

Instantiate(leaderboardEntryPrefab, transform.position,transform.rotation);

leaderboardGrid.GetComponent<LeaderboardEntry>().nameString=entry.UserName.ToString();

leaderboardGrid.GetComponent<LeaderboardEntry>().scoreString=entry.GetNumberValue("score").ToString();

entries.Add(leaderboardEntryPrefab);

}

});

}


Note That I already have a script on the leaderboardEntryPrefab that makes it a child of the Leaderboard Grid.


Thanks !


Best Answer

Hi Clau,


I've written a script that connects to your GameSparksTest game and manages to post to the leaderboard receive the leaderboard entries, here's the code:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using GameSparks.Api.Requests;

public class ScoreLeaderboard : MonoBehaviour
{
	List<string> leaderboardEntries = new List<string>();
	

	public void OnGUI()
	{


		GUILayout.BeginArea(new Rect(0, 0, Screen.width, Screen.height));

		if (GUILayout.Button("Login In to GameSparks"))
		{
			new DeviceAuthenticationRequest().SetDisplayName("Shane").Send((response) =>
			{

			});
		}

		if (GUILayout.Button("Post a score of 20"))
		{
			new LogEventRequest_postScore().Set_score(20).Send((response) =>
			{
				if (response.HasErrors)
				{

				}
				else
				{
					Debug.Log("Score posted Succesfully!");
				}
			});
		}

		if (GUILayout.Button("Get leaderboard entries"))
		{
			new LeaderboardDataRequest_highScoreleaderboard().SetEntryCount(100).Send((response) =>
			{
				if (response.HasErrors)
				{

				}
				else
				{
					Debug.Log("Got leaderboards successfully!");

					foreach (var entry in response.Data_highScoreleaderboard)
					{
						leaderboardEntries.Add("Rank: " + entry.Rank + "Name: " + entry.UserName + "Score: " + entry.score);
					}
				}
			});

		}

		foreach (string entry in leaderboardEntries)
		{
			GUILayout.Label(entry);
		}

		GUILayout.EndArea();
	}
}

 

The key thing is to make sure you have the GameSparksUnity.cs script attached to something in your scene, preferably an empty gameObject to itself. It is also super important to wait for GameSparks to be available before requesting anything from it. By putting code into a start function  and hitting play, GameSparks wont have enough time to establish a connection before the request is fired off.


You can check the state of GameSparks with the following:


  

GameSparks.Core.GS.Available; //This is equal to true when GameSparks is ready to recevie requests
GameSparks.Core.GS.Authenticated; //This is equal to true when a user is logged in, it is required that users be logged in to send requests

  

Hope this helps!


Shane


Hi Clau,


Can you try your score posting event directly from the Test Harness.


If the leaderboards are setup correctly you should get a new leaderboard highscore message when you post the score.


Thanks.


Oisin.

Hello, everything works well on the Test Harness, but when I try it from my project, it just doesn't work, I double checked everything in the Configurator (shortcodes, API, etc.), but I am just stumped. Something I think I should mention, the GetLeaderboard() function, doesn't work, I mean I try even to call it from the Start function and when I do, I get a null reference at this line: "new LeaderboardDataRequest_highScoreleaderboard().SetEntryCount(5).Send((response)=>", I will post the whole error at the end of the post. Also with the with the post score function, neither of the Debug.Log show. Am I missing something ? Did I screw up the code ? Any help would be very much apreciated. 


Thanks !


P.S. Here is the whole Error:


NullReferenceException: Object reference not set to an instance of an object

GameSparks.Core.GSRequest.Send (System.Action`1 callback, Int32 timeoutSeconds)

GameSparks.Core.GSRequest.Send (System.Action`1 callback)

GameSparks.Core.GSTypedRequest`2[IN,OUT].Send (System.Action`1 callback)

Leaderboards.GetLeaderboard () (at Assets/Scripts/Leaderboards.cs:29)

Leaderboards.Start () (at Assets/Scripts/Leaderboards.cs:16)

Everything works fine on the Test Harness, I can post scores and check the Leaderboard and everything is there, also I checked the API from Gamesparks to match the one in the app and everything seems fine.

Answer

Hi Clau,


I've written a script that connects to your GameSparksTest game and manages to post to the leaderboard receive the leaderboard entries, here's the code:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using GameSparks.Api.Requests;

public class ScoreLeaderboard : MonoBehaviour
{
	List<string> leaderboardEntries = new List<string>();
	

	public void OnGUI()
	{


		GUILayout.BeginArea(new Rect(0, 0, Screen.width, Screen.height));

		if (GUILayout.Button("Login In to GameSparks"))
		{
			new DeviceAuthenticationRequest().SetDisplayName("Shane").Send((response) =>
			{

			});
		}

		if (GUILayout.Button("Post a score of 20"))
		{
			new LogEventRequest_postScore().Set_score(20).Send((response) =>
			{
				if (response.HasErrors)
				{

				}
				else
				{
					Debug.Log("Score posted Succesfully!");
				}
			});
		}

		if (GUILayout.Button("Get leaderboard entries"))
		{
			new LeaderboardDataRequest_highScoreleaderboard().SetEntryCount(100).Send((response) =>
			{
				if (response.HasErrors)
				{

				}
				else
				{
					Debug.Log("Got leaderboards successfully!");

					foreach (var entry in response.Data_highScoreleaderboard)
					{
						leaderboardEntries.Add("Rank: " + entry.Rank + "Name: " + entry.UserName + "Score: " + entry.score);
					}
				}
			});

		}

		foreach (string entry in leaderboardEntries)
		{
			GUILayout.Label(entry);
		}

		GUILayout.EndArea();
	}
}

 

The key thing is to make sure you have the GameSparksUnity.cs script attached to something in your scene, preferably an empty gameObject to itself. It is also super important to wait for GameSparks to be available before requesting anything from it. By putting code into a start function  and hitting play, GameSparks wont have enough time to establish a connection before the request is fired off.


You can check the state of GameSparks with the following:


  

GameSparks.Core.GS.Available; //This is equal to true when GameSparks is ready to recevie requests
GameSparks.Core.GS.Authenticated; //This is equal to true when a user is logged in, it is required that users be logged in to send requests

  

Hope this helps!


Shane

Hey, thanks for the help I managed to get everything up and running !


Is there a way you could provide a similar generic code for a Store ? (IAP), I really don't know where to start with that. Something really simple, like buying a pack of coins.


Thanks !

Login to post a comment