Skip to content

Instantly share code, notes, and snippets.

@Bradshaw
Last active January 17, 2020 16:53
Show Gist options
  • Save Bradshaw/a5a582fe9c4bc97dbd83383f665b820a to your computer and use it in GitHub Desktop.
Save Bradshaw/a5a582fe9c4bc97dbd83383f665b820a to your computer and use it in GitHub Desktop.
Generic Score system for Unity & Node for use with https://generic-scores.glitch.me/

generic-scores

A simple high-score table with a database

Go to this Glitch.com project and select the "Remix on Glitch 🎤" option in the fish menu to set up the database & website server

Include ScorePoster.cs anywhere in your Unity project

Make sure to update the url and game key to point to your Glitch project

Do GenericScores.ScorePoster.PostScore(nickname, score) to post a score to the high-score table If you want to run some code when the request to the server has gone through, do it like this instead:

await GenericScores.ScorePoster.PostScore(nickname, score).ContinueWith((task)=>{
  // The server has finished
  // task.Result contains the response from the server
  // It should be { message: "success" } if all has gone well
});

You can use the TestPostScores.cs script as a basis for your own code

using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
namespace GenericScores
{
public static class ScorePoster
{
private static readonly HttpClient client = new HttpClient();
public static string baseurl = "https://generic-scores.glitch.me/";
public static string postroute = "addScore";
// Use the game key you set in the .env file on glitch.com.
// It looks something like: aJrdV9!t34
public static string gamekey;
public static async Task<string> PostScore(string nickname, int score){
var values = new Dictionary<string, string>
{
{ "nickname", nickname },
{ "score", score.ToString() },
{ "gamekey", gamekey }
};
var content = new FormUrlEncodedContent(values);
var response = await client.PostAsync(
baseurl+postroute,
content
);
var responseString = await response.Content.ReadAsStringAsync();
return responseString;
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[ExecuteInEditMode]
public class TestPostScore : MonoBehaviour
{
[Header("Values to send")]
public string nickname = "Pro Gamer";
public int score = 9001;
[Header("Tick this to trigger sending scores")]
public bool postNow;
[Header("Configuration")]
public string baseurl = "https://generic-scores.glitch.me/";
public string postroute = "addScore";
// Use the game key you set in the .env file on glitch.com.
// It looks something like: aJrdV9!t34
public string gamekey;
void Start()
{
Init();
}
// Update is called once per frame
void Update()
{
if (postNow){
if (!Application.isPlaying){
// We're in the editor, Init() just in case because sometimes 🤷‍♀️
Init();
}
postNow = false;
PostScore();
}
}
async void PostScore()
{
// Ask for the ScorePoster to send the score, then print the result from the server
await GenericScores.ScorePoster.PostScore(nickname, score).ContinueWith((task)=>{
Debug.Log(task.Result);
});
}
void Init()
{
// This only needs to be done once, so the game knows how to send scores
GenericScores.ScorePoster.baseurl = baseurl;
GenericScores.ScorePoster.postroute = postroute;
GenericScores.ScorePoster.gamekey = gamekey;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment