Online Highscore Using VaRest Plugin

Hey There,

For quite some time now I have been searching the internet trying to find any examples/tutorials on how to add an online high score system to my game. Surprisingly there isn’t actually much to find unless you are willing to use some pre-built plugin or know how to code in C++. I want to use the VaRest plugin to create mine, but the only tutorials I can find using this plugin are to do with login systems, and since my PHP skills are essentially non-existent, I don’t have the knowledge to try to adapt the scripts to my needs.

If possible, I would love for someone to give me a detailed (with pictures preferably) rundown on how to make a fairly simple high score system that will utilize a steam unique ID (using the advanced sessions plugin for this) to submit scores as well as retrieve scores.

I don’t need any UI for this, as I’m pretty comfortable I can do this myself once the scores have been imported into unreal, but I would like help with both the unreal engine side of the code and the serverside PHP scripts.

What I want each function to do:

(Submit score)

  • Send Level name, unique ID, and score (time) to PHP script from unreal
  • Php script checks the level name and selects the correct table within a database
  • Php script then checks if Player ID exists
  • If the table does not contain the User ID, simply add the score
  • If the table does contain the User ID, check to see if the score is better (in this case, a lower float value) and either submit the score if it’s better, or don’t if it’s not better
  • returns string depending on which result occurred

(Retrieve Scores)

  • Send Level name and user ID to PHP script from unreal
  • List item
  • Selects correct data table for level and returns top 100 scores
  • Also returns the players own score using User ID (assuming they aren’t in top 100)

I think that is is, but if anyone has any questions please do ask. Also, sorry if this sounds a little demanding, but it is (and has been for a while) the only thing standing in the way of me being able to share my game with friends and have some competitive fun with scores. Also, resources for creating a leaderboard using VaRest plugin are severely lacking, so by doing this you are not only helping me, but you are helping the community as a whole.

Thanks in advance to anyone generous enough to help. I really do appreciate it!

I think your lack of success in finding information is that you’re focused on the VaRest plugin instead of on how to do things using ReST. ReST is just a fancy acronym for a way of handling HTTP requests (GET, PUT, POST, DELETE) which is what Web pages do, except with ReST it’s usually not trying to fetch a web page, it’s just transferring data or performing an operation on the server, so it becomes an API for your server scripts and services rather than a webpage dispensing method.

In your case, your server will be waiting to receive an HTTP request from the Unreal game you made, probably containing a POST or PUT request, the body of which will probably be a JSON string containing the high score data.

So you just need to program your CGI script (PHP sounds like you’re using) on the server side on what to do when such a request hits it.

  1. Oh I just got a POST request. Let’s see what’s in the body.
  2. Oh it’s a JSON string. And there’s high score key-value pairs in it. Let me parse that into an Associative Array or an Object.
  3. same PHP script is going to not respond with HTML or echoes except to report back to the VaRest plugin how the operation went. Instead, it’s going to take that data and put it in a SQL table or store it in a file or something.

Later when somebody hits your web server, another PHP script will generate some HTML by querying the file or database table you stored in the above steps, and display the data that was posted there.

So all I’m saying is you can, on your high score server, simply treat it like a web page action/request, except don’t give a webpage back.

So if you know PHP and HTML then you already know everything you need to do it.

VaRest is just a plugin which gives you a convenient way to cURL and Submit such HTTP requests to wherever you want and receive the response back to your Unreal game (if there is a response at all).

You can get everything working on your high score server first by building a simple HTML form with all the fields you want to store, and getting it working without your Unreal game, just treating it as a manually filled out web page form. Then once it’s all working, you can just use VaRest to get Unreal to programmatically submit the same data that the form would be submitting. TADA. All done.

Thanks for your response.

Without trying to sound rude, you basically just told me what I already know.

As I mentioned in my question, my programming skills are essentially 0 when it comes to actually writing scripts. What I was looking for (and I thought I made this clear, but maybe not) was an actual working example of someone passing data back and forth between unreal using the varest plugin, and a PHP script. I know I asked for a lot more than that, and maybe I was naive in thinking that anyone would help with such a huge task, so at this point, I’m basically just looking for a working example of getting data to and from a PHP script.

Hopefully I hear back from you, but I understand if not since this post could probably be easily taken the wrong way.

Well I’m glad we got that out of the way. I am using VaRest to make a simple GET request to a service that responds with my computer’s public IP address. I might be able to adapt it to something more involved and show how I did it tomorrow.

So you know how to do everything on the server side. Just need help with the Unreal side.
Specifically it sounds like you need to know:

  1. How to set headers on the VaRest request,
  2. How to set the request URL,
  3. How to set the rrquest body,
  4. How to actually send the request.
  5. How to pack the data from unreal varisbles into a format that works with HTTP,
  6. How to take HTTP responses and put the data from that into Unreal’s variables.

You probaly have at leadt one or two of those covered by now so let me know and I’ll try and skip them.

Well I put together what I hoped would be an example blueprint, but unfortunately I kept getting a 401 status from the server’s response, but here’s the blueprint of how far I got. Maybe it just doesn’t have TLS 1.2 in the current version of VaRest or something, but I’ve gotten it to work on other things. Unfortunately I am out of time for today and can’t work on creating another example until tomorrow night or later maybe.

Here’s what I got so far (this blueprint pastebin will be up for 1 week before it expires):

The execution path that sets up the Bind Event nodes etc, comes off a Construct event in one of my menus where you click the Test VaRest button (you can see that event node below and to the left if you scroll far enough) to send the request.

Almost everything you do in the blueprints to make the request happen is setting up the request and the success/fail event handlers. Then submitting the actual request is just one dinky little Process URL node from the Request object you made earlier.

So anyway, if you can get a 401 like I did, that’s progress, right? hoping you get a 200 instead.

Getting the response body (which is done with a simple get Response Body node, unlike what I posted on the pastebin), it says “No Authorization Header” which bothers me because I explicitly set the authorization header. Oh well. Anyway I think I’m close to the right approach, I just am having trouble getting the auth header to work. You might try the same thing but tell your server not to require authorization at first to make sure your VaRest/Unreal end of things is working.

Thanks for the example.

I was under the impression that I shouldn’t be using a get request and should instead be using a POST request (you even said this in your initial answer) and as far as I can tell that example isn’t using JSON at all either.

What I would also like to see if possible (in fact this is more important to me right now than the blueprints) is an example of the PHP scrips on the other side receiving the data from unreal.

Oh. I thought you explained that you already knew how to do the PHP and SQL part, and you didn’t know how to get VaRest to send a request to your PHP server.

The PHP and SQL side of things isn’t really Unreal-related at all. VaRest will be sending a JSON string to your server with Content-Type: application/json.

Your server will receive the JSON string and (possibly needing to parse the string into an object on the server side from the php $_REQUEST struct), can then access each of the sent data by its key-value pairing like $_REQUEST[‘myplayername’]
and $_REQUEST[‘mylovelyscore’]
whatever data you put into the request JSON by whatever key names you used to put it in.

My PHP is a bit rusty but that’s the first thing I would try.

So if on the Unreal side, you set you inserted JSON request values and set “myplayername” to whatever value you want, and “mylovelyscore” to whatever value you want (these are being put into the JSON request object you created using that create JSON request node from VaRest plugin), then you Apply URL and it’s now the PHP server’s turn to receive the data and decide what to do with it.
it should come in something like {“myplayername”: “george”, “mylovelyscore”: 1000} or something like that, and you can always inspect the structure of the incoming data with a var_dump, a print_r, or combining those two with error_log if you’re not viewing the php output via a webpage.

Maybe later after my day job I could try creating this myself and see how far I get.

Got a super basic example working.

Here’s the entire blueprint code to post a score:

And here’s some PHP code that just echoes the data back to Unreal. It doesn’t store anything or use SQL, but it’s a working example of two-way communication.

<?php

print_r($_REQUEST);

?>

To get it to store in a SQL table then I’ll need to install SQL, and include the DBO stuff, etc.