Search code examples
c#windows-phone-8unity-game-enginereloadupdating

High score list doesn't refresh on Windows Phone


I've problem with refreshing/reloading popup with high score list in my game. All works fine in unity 5 emulator but not on Windows Phone device.

Problem: When I finished play my game, it uploads score in cloud. Then when I goes in to high score menu (game downloads high scores here) there is no up to date high score list on Windows Phone (in unity emulator, all it's right). What is interesting, when I closing game and running it again and going to high score menu, the high score list is up to date. The game score is uploading correctly, I checked it on the server site.

Is there any way to solve this problem?

EDIT: I'm using dreamlo.com to store scores. In that way I upload/download scores to/from dreamloo:

using UnityEngine;
using System.Collections;

public class Highscores : MonoBehaviour {

const string privateCode = "myprivatecode (random characters)";
const string publicCode = "mypubliccode(random characters)";
const string webURL = "http://dreamlo.com/lb/";

DisplayHighscores highscoresDisplay;
public Highscore[] highscoresList;
static Highscores instance;


void Awake(){

    highscoresDisplay = GetComponent<DisplayHighscores> ();
    instance = this;

}



public static void AddNewHighscore(string username, int score){

    instance.StartCoroutine(instance.UploadNewHighscore(username,score));

}


IEnumerator UploadNewHighscore(string username, int score)
{
    WWW www = new WWW (webURL + privateCode + "/add/" + WWW.EscapeURL (username) + "/" + score);
    yield return www;

    if (string.IsNullOrEmpty (www.error)){
        print ("Uploaded Successful");

        DownloadHighscores();
    }

    else {
        print ("Error uploading: " + www.error);
    }

}

public void DownloadHighscores(){

    StartCoroutine ("DownloadHighscoreFromDatabase");

}


IEnumerator DownloadHighscoreFromDatabase()
{
    WWW www = new WWW (webURL + publicCode + "/pipe/");
    yield return www;

    if (string.IsNullOrEmpty (www.error)) {
        FormatHighscores (www.text);
        highscoresDisplay.OnHighscoresDownloaded(highscoresList);
    }
    else {
        print ("Error downloading: " + www.error);
    }

}

void FormatHighscores(string textStream){

    string[] entries = textStream.Split(new char[] {'\n'}, System.StringSplitOptions.RemoveEmptyEntries);

    highscoresList = new Highscore[entries.Length];

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

        string[] entryInfo = entries[i].Split (new char[] {'|'});
        string username = entryInfo[0];
        int score = int.Parse(entryInfo[1]);
        highscoresList[i] = new Highscore(username, score);
        print(highscoresList[i].username + ": " + highscoresList[i].score);
    }                                      

}

}

public struct Highscore{

public string username;
public int score;

public Highscore(string _username, int _score){

                username = _username;
                score = _score;
}

}

High score displays that class:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class DisplayHighscores : MonoBehaviour {

public Text[] highscoreText;
Highscores highscoreManager;


void Start () {

    for (int i = 0; i<highscoreText.Length; i++) {

        highscoreText[i].text = i+1 + ". Fetching...";

    }

    highscoreManager = GetComponent<Highscores>();

    StartCoroutine ("RefreshHighscores");
}


public void OnHighscoresDownloaded(Highscore[] highscoreList){

    for (int i = 0; i < highscoreText.Length; i++) {

        highscoreText[i].text = i+1 + ". ";
        if(highscoreList.Length > i){

            highscoreText[i].text += highscoreList[i].username + " - " + highscoreList[i].score;

        }
    }

}

IEnumerator RefreshHighscores(){

    while (true) {

        highscoreManager.DownloadHighscores();
        yield return new WaitForSeconds(30);

    }

}
}

Solution

  • If you are using the WWW class to retrieve your scores from the server, it will probably store the results in the cache.

    I had the same problem on Windows Phone, and my solution for this is is to use a random number in your URL using GET.

    Here is an example:

        string get_url = "http:yoururl.com/?rnd=" + Random.value;
        WWW hs_get = new WWW(get_url);
        yield return hs_get;
    

    This way it shouldn't be stored in your cache.