- Home /
Adding Score & Level to Database / Dreamlo
Hello, I have a good high score system working which does also upload and download perfectly to and from the database. I now want to show the Level number that the player managed to reach. If they finally get GameOver, it add's their Username, Score and LevelNumber to the Database. So far, Username and Score is working.
I have NO errors in the code and it shows me the level number in game successfully when it loads up the leaderboards and even puts the level number.
The only part that is NOT working is that it doesn't upload the level number to the database and just shows Name and Score. It seems like the entries length is maxed out to two? That's my guess..
As you can see, I want to add a third column which is called 'Level'. I am very very new to all this database stuff, and only started around a week ago. If you are willing to educate me on this subject, that would be awesome!
using UnityEngine;
using System.Collections;
public class HighScores : MonoBehaviour {
const string privateCode = "//SECRET";
const string publicCode = "//SECRET";
const string webURL = "http://dreamlo.com/lb/";
public Highscore[] highscoresList;
static HighScores instance;
DisplayHighscores highscoresDisplay;
void Awake() {
instance = this;
highscoresDisplay = GetComponent<DisplayHighscores> ();
}
public static void AddNewHighscore(string username, int score, int levelLoaded)
{
instance.StartCoroutine (instance.UploadNewHighscore (username, score, levelLoaded));
}
IEnumerator UploadNewHighscore(string username, int score, int levelLoaded)
{
WWW WWW = new WWW (webURL + privateCode + "/add/" + WWW.EscapeURL (username) + "/" + score + "/" + levelLoaded);
yield return WWW;
if (string.IsNullOrEmpty (WWW.error)) {
print ("Upload Successful");
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(highscoresList);
}
else {
print ("Error uploading: " + 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]);
int levelLoaded = int.Parse(entryInfo[2]);
highscoresList[i] = new Highscore(username,score, levelLoaded);
print (highscoresList[i].username + ": " + highscoresList[i].score + ": " + highscoresList[i].levelLoaded);
}
}
}
public struct Highscore {
public string username;
public int score;
public int levelLoaded;
public Highscore(string _username, int _score, int _levelLoaded) {
username = _username;
score = _score;
levelLoaded = _levelLoaded;
}
}
@DimitriU$$anonymous$$ Did you figure this out? I had no idea people were posting this stuff to Unity Answers ins$$anonymous$$d of contacting me directly :(
Same issue, im trying to upload 2 strings and one int, but (entryInfo[2]); returns '0' even though it´s a string.
Did you solve it?
Answer by TX Manager · Feb 25, 2016 at 03:42 PM
You can manully editing it by making a UI prefab or void OnGUI ()
on the script it self then get one Text UI prefab and Instantiate it on the right place on the canves then decleere String[]
and set it to add Elemenates by every time the data base get a new "High Score" (do this by your own script knowledge). Assigne this string to the Application that the player managed to get to it. like this "String Name" = Application.loadedLevel
(don't forget that Application.loadedLevel
is class int). then assigen it to the text "Text Name".text = "String Name"
(don't forget that this "String Name" is the string[] Elemenete) Then you can Instantiate this text on the canves on it's right transform. Set this transform to chanage every time there's a text already Instantiated note :(I don't know how to assigen String[]
to Text.text
) :( try it, it may work :)
I think that he want to make the new high score appear in the list. plz Explian more, I may help
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Downsize/compress image on upload to Firebase? 0 Answers
System.DateTime truncate Milliseconds 1 Answer
imeCompositionmode 0 Answers