Hello everyone,
Thanks to the following tutorials I can now upload and display scores with the [dreamlo][1] asset/service.
https://www.youtube.com/watch?v=9jejKPPKomg&t=696sMany thanks to Carmine Guida and Sebastian Lague for helping me get this far!
Sebastian’s tutorial has introduced me to a variety of concepts I have not used before; namely IEnumerator and the WWW module.
(It seems those 2 things play an integral role in what I’m trying to accomplish and even thought I’ve been able to find some documentation on these I still cannot implement them successfully in my own scripts. My poor understanding of referencing and syntax have undoubtedly played a role )
I also noticed that Sebastian does not make use of the dreamloLeaderBoard.cs class that comes with the dreamlo asset package. (me thinks probably because it wasn’t available at the time)
Following the tutorials, I reproduced this HiScores.cs class.
using UnityEngine;
using System.Collections;
public class HiScores : MonoBehaviour{
const string privateCode = "############# hiden for this post ###############";
const string publicCode = "58508f378af60311f8e8c911";
const string webURL = "http://dreamlo.com/lb/";
public Highscore[] highscoreList;
static HiScores instance;
DisplayHighscores highscoresDisplay;
void Awake(){
instance = this;
highscoresDisplay = GetComponent<DisplayHighscores> ();
}
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 ("Upload Successfull");
DownloadHighscores();
}
else{
print("Error uploading: " + www.error);
}
}
public void DownloadHighscores(){
StartCoroutine("DownloadHighscoresFromDatabase");
}
IEnumerator DownloadHighscoresFromDatabase(){
WWW www = new WWW(webURL + publicCode + "/pipe/");
yield return www;
if (string.IsNullOrEmpty(www.error)){
FormatHighscores(www.text);
highscoresDisplay.OnHighscoresDownloaded(highscoreList);
}
else{
print("Error Downloading: " + www.error);
}
}
void FormatHighscores(string textStream){
string[] entries = textStream.Split(new char[]{'
'}, System.StringSplitOptions.RemoveEmptyEntries);
highscoreList = new Highscore[entries.Length];
for (int i= 0; i <entries.Length; i++){
string[] entryInfo = entries*.Split(new char[]{'|'});*
-
string username = entryInfo[0];*
-
int score = int.Parse(entryInfo[1]);*
_ highscoreList = new Highscore(username,score);_
* }*
* }*
}
public struct Highscore{
* public string username;*
* public int score;*
* public Highscore(string _username, int _score)
{ username = _username;
score = _score;*
* }*
}
I also have an UiInputField.cs class that grabs the user’s name from an input field and stores it in public text nameGrabber;
. This class also has the following SaveName() method that I use to upload the player’s very first score of 1. (I should probably just assign a score of zero but I will explain why I decided to do it this way in a minute…)
* public void SaveName(){*
* PlayPref.userName = nameGrabber.text;*
* PlayerPrefs.SetString(“playerName”, PlayPref.userName);*
* int score = 1;*
* string username = PlayerPrefs.GetString(“playerName”);*
* HiScores.AddNewHighscore(username,score);*
* }*
The simple If statement bellow is in my StateSet.cs “game manager” and it uploads my player’s best score to the database.
*if (stillGuest == false) *
* { int score = PlayerPrefs.GetInt(“TopScore”); *
* string username = PlayerPrefs.GetString(“playerName”);*
* HiScores.AddNewHighscore(username,score);*
* }*
So far so good…
I am currently trying to accomplish 2 things:
1. Delete a username/score entry on the dreamlo DB when I reset the player’s profile.
I have a PlayPref.cs class that acts as a “wrapper” for PlayerPrefs. Inside I have a ProfileReset() method and would like for it to delete the username/score from the database.
On the dreamlo website Carmine says the following:
“Changes and updates to your leaderboard are made through simple http get requests using your private url…Delete Carmine’s score”:
http://dreamlo.com/lb/### private code #### hiden for this post ######/delete/Carmine
I tried putting the following code in my ProfileReset() method…
WWW www = new WWW(“http://dreamlo.com/lb/## private code ## hiden for this post ###/delete/” + PlayerPrefs.GetString(“playerName”));
Nothing happens. I’m sure it’s not as “simple” as that. I don’t know what to do this this new www variable. I feel like I’m missing something. To be safe, I even tried to include the dreamloLeaderBoard.cs class in my project but I don’t think that has anything to do with it…
(I have a feeling the solution for this first task will also provide some insight as to how to approach the second one.)
2. Detect if a username already exists in the dreamlo DB to avoid duplicate names.
In my StateSet.cs class I have the following checkAvailability() method.
public void checkAvailability(){
* if (PlayPref.nameTaken == true){*
* regUnavailableText.SetActive(true);*
* regCheckAvailButton.SetActive(false);*
* clearButton.SetActive(true);*
* }*
* if (PlayPref.nameTaken == false){*
* regAvailableText.SetActive(true);*
* regYesNoButtons.SetActive(true);*
* }*
* }*
In other words, based on the PlayPref.nameTaken boolean I enable/disable the UI that allows the user to “confirm” or “search again.”
It is in this method that I’m trying to search the database to look up (and temporarily store) the score for the UserName that is inside the nameGrabber Text variable.
Then, if the score value for that username is not equal to zero it means that someone has already picked that name. This will set:
PlayPref.nameTaken = true;
if it is 0, or less than 1, the name is available and so:
PlayPref.nameTaken = false;
(I’m sure the whole set-initial-score-to-1 approach is unnecessarily complex but, based on the little that I know so far, it’s the only solution I can think of.)
Thank you for taking the time to read all of this. As always, any help, suggestions, advice or direction you can offer on the subject will be enormously appreciated!
Best regards,
-Don
_*[1]: http://dreamlo.com/*_