Help with high score SQL table and the C# scripts
I am searching for a way to connect an SQL database with a high score system for a game. I have been literally spent hours and hours on a solid tutorial or a walk-through, but everything that looks kind of useful is ancient history, like this https://wiki.unity3d.com/index.php?title=Server_Side_Highscores As a lot of things have changed (like commands etc), does anyone else knows some more details about this process in 2020? A tutorial or anything?? I have created the SQL database and the PHP files, but the high score controller scripts are messy and not working properly. If anyone has the time and takes a look, i would be very grateful. I tried to fix some things, but i probably broke many others. Here is what i have.
HighScoreController script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class HighScoreController : MonoBehaviour
{
private string secretKey = "xxx"; // Edit this value and make sure it's the same as the one stored on the server
public string addScoreURL = "mywebsite/addscore.php?"; //be sure to add a ? to your url
public string highscoreURL = "mywebsite/display.php";
void Start()
{
StartCoroutine(DownloadHighscoresFromDatabase());
}
public void AddNewHighScore(string userName, int score)
{
StartCoroutine(UploadNewHighscore(userName, score));
}
// remember to use StartCoroutine when calling this function!
IEnumerator UploadNewHighscore(string name, int score)
{
//This connects to a server side php script that will add the name and score to a MySQL DB.
// Supply it with a string representing the players name and the players score.
string hash = Md5Sum(name + score + secretKey);
string post_url = addScoreURL + "name=" + UnityWebRequest.EscapeURL(name) + "&score=" + score + "&hash=" + hash;
// Post the URL to the site and create a download object to get the result.
UnityWebRequest hs_post = new UnityWebRequest(post_url);
yield return hs_post; // Wait until the download is done
if (hs_post.error != null)
{
print("There was an error posting the high score: " + hs_post.error);
}
else
{
DownloadHighscores();
}
}
public string Md5Sum(string strToEncrypt)
{
System.Text.UTF8Encoding ue = new System.Text.UTF8Encoding();
byte[] bytes = ue.GetBytes(strToEncrypt);
// encrypt bytes
System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] hashBytes = md5.ComputeHash(bytes);
// Convert the encrypted bytes back to a string (base 16)
string hashString = "";
for (int i = 0; i < hashBytes.Length; i++)
{
hashString += System.Convert.ToString(hashBytes[i], 16).PadLeft(2, '0');
}
return hashString.PadLeft(32, '0');
}
static public void DownloadHighscores()
{
StartCoroutine(DownloadHighscoresFromDatabase());
}
// Get the scores from the MySQL DB to display in a Text.
// remember to use StartCoroutine when calling this function!
IEnumerator DownloadHighscoresFromDatabase()
{
UnityWebRequest hs_get = new UnityWebRequest(highscoreURL);
yield return hs_get;
int counter = 0;
for (; counter < hs_get.downloadHandler.text.Length; counter++)
if (hs_get.downloadHandler.text[counter] == '#')
break;
string hs_got = hs_get.downloadHandler.text.Substring(0, counter); //Substring(0, counter);
if (hs_get.error != null)
{
print("There was an error getting the high score: " + hs_get.error);
}
else
{
FormatHighScores(hs_got);
DownloadHighscores();
}
}
void FormatHighScores(string TextStream)
{
string[] entries = TextStream.Split(new char[] { '\n' }, System.StringSplitOptions.RemoveEmptyEntries);
//string
HighScore[] highScoresList = new HighScore[entries.Length];
for (int counter = 0; counter < entries.Length; counter++)
{
string[] entryInfo = entries[counter].Split(new char[] { '|' });
if (entryInfo.Length == 2)
{
//---
string userName = entryInfo[0];
int score = int.Parse(entryInfo[1]);
highScoresList[counter] = new HighScore(userName, score);
//---
}
}
}
public struct HighScore
{
public string userName;
public int score;
public HighScore(string inUserName, int inScore)
{
userName = inUserName;
score = inScore;
}
}
public static int lastHighScore = -1;
}
And here is the HighScoreDisplay script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class HighScoreDisplay : MonoBehaviour
{
IEnumerator refreshHighScores()
{
while (true)
{
HighScoreController.DownloadHighscores();
yield return new WaitForSeconds(30);
}
}
public void OnHighScoresDownloaded(HighScoreController.HighScore[] highScoresList)
{
for (int counter = 0; counter < highScoresList.Length; counter++)
{
//---
GameObject go = GameObject.Find("SUI_Tech_Center_Simple/HighScores_Panel");
go.SetActive(true);
Text[] children = go.GetComponentsInChildren<Text>();
children[0].text = (counter + 1).ToString();
children[1].text = highScoresList[counter].userName;
children[2].text = highScoresList[counter].score.ToString();
//---
if (counter == HighScoreController.lastHighScore)
{
string currentColour = "ff0000";
children[0].text = "" + children[0].text + "";
children[1].text = "" + children[1].text + "";
children[2].text = "" + children[2].text + "";
}
}
for (int counter = 0; counter < highScoresList.Length; counter++)
{
GameObject go = GameObject.Find("SUI_Tech_Center_Simple/HighScores_Panel");
go.SetActive(true);
Text[] children = go.GetComponentsInChildren<Text>();
children[0].text = (counter + 1).ToString();
children[1].text = highScoresList[counter].userName;
children[2].text = highScoresList[counter].score.ToString();
if (counter == HighScoreController.lastHighScore)
{
string currentColour = "ff0000";
children[0].text = "" + children[0].text + "";
children[1].text = "" + children[1].text + "";
children[2].text = "" + children[2].text + "";
}
}
}
}
To be honest i don't know what to do next. I am currently stuck at:
error CS0120: An object reference is required for the non-static field, method, or property 'HighScoreController.DownloadHighscoresFromDatabase()'
For reference i tried to follow this guide: https://10printhelloworld.wordpress.com/2016/03/27/adventures-in-unity-1-7b-highscores-in-game-overview/
Your answer
Follow this Question
Related Questions
Leaderboard Unity [C#] Help 1 Answer
Highscore - BestTime 1 Answer
A* Pathfinding and keep the enmy at range 0 Answers
animator and script issue 0 Answers
Save/Load Player System 0 Answers