Online High Scores

Adapted from mtarzaim tutorial(the credits go for him, please).

Pre-requisites:

  • A website with a PHP server + MySQL server
    • Full Access to the MySQL server, to create tables and then update it
    • Gdevelop
    • The variables (global variables usually) to send to your website

    Variables used in my game: Note: all these variables are Global Variables inside Gdevelop

  • Name
  • TimeLeft
  • Deaths
  • Fire
  • Energy
  • Oxygen
  • Score

Part 1- Setting Up The Database

Create a new database “GDProject”. In my case, I have a hosting service with cpanel, so, it is easier. First, create the Database; than the two users. Take a look at the image below(cpanel):

Using the “root” account is too dangerous, since the password will be hardcoded in a php page. Go in the “Privileges/Rights” tab in PHPMyAdmin.

Create a new user “readonly” with a password, whose rights will only be SELECT, for the database “GDProject”. It will be used for SELECT queries (read data in the database). Create a new user “update” with a password, whose rights will be INSERT, for the database “GDProject”.

It will be used for modifying data in tables of the database.

Like this, even if your php pages are hacked, they will only get low-level access to your database, unable to destroy the core of your system.

Using PHPMyadmin With PhpMyAdmin is easy to create a table and the fields. Create inside the table “player” with the following settings :

  • column 1 : ID, parameter BIGINT unsigned, AUTOINCREMENT, PRIMARY . The index of your table. It's an unique identifier, automatically increased with each INSERT * column 2 : name, parameter TINYTEXT . Where the name of your player is saved * column 3 : date, parameter TIMESTAMP, default value CURRENTTIMESTAMP . Where the date of the INSERT is saved. Assigned automatically with the current date and time.
  • column 4 : score, parameter BIGINT, default value 0 . Where the score of your player is saved

Note: In my case, I did not use Timestamp or date! I have more columms as the columm 2 example.

Note: Insert a few dummy rows, to be sure everything works, and to have some inputs to display later.

PhpMyAdmin:

Part 2- External Event For Data Sending

In GDevelop, create an action “Send request to a web page”.

The parameters are:

Parameter 1 : Host “http://www.mywebsite.com/

Parameter 2 : Path to page “/GDProject/hiscoresToUpdate.php”

Parameter 3 : Request body content “&gname=”+GlobalVariableString(name)+“&gtimetoend=”+GlobalVariableString(time) +“&gdeaths=”+GlobalVariableString(deaths)+“&gfire=”+GlobalVariableString(fire) +“&genergy=”+GlobalVariableString(energy)+“&goxygen=”+GlobalVariableString(oxygen) +“&gscore=”+GlobalVariableString(score)

Take a look:

Part 3- PHP Pages

You will need two php pages, stored on your website, preferably in the same own directory. You might also add a third empty page, index.htm, to prevent hackers from reading this directory's tree. ex : http://www.mywebsite.com/GDProject

  • index.htm
  • HiscoresToUpdate.php
  • Hiscores.php

Why two php pages ? Couldn't one just be enough? Because we will need to trick a little the user.

The first page will update the database with the variables sent by GD. But we must prevent the user to access this page manually (and modify the variables in the address bar, so he could get a higher score or mess with the database). So immediatly after he accesses it, he will be redirect to the second page, designed for display only. It will be harder for him to mess with the variables. It's still possible with sniffer tools. But why wasting time using those for a little amateur game?..

Connect to your website and create a directory “GDProject”, via FTP.

Note: In my case, had to create a .htaccess file with some instructions to make it work. Take a look:

Add two new files : A) “HiscoresToUpdate.php”, the PHP page to update the database . Don't forget to replace “db user”, “user pass”, and “db name” to yours.

B) “Hiscores.php”, php page to display to everyone. Building a basic HTML table displaying the 100 highest scores, with name and date.

Note: In my case, I did create a styled page with the use of Bootstrap( teached by my friend Bruno Cochito. Thanks!).

Part 3- Build a Test Scene(simple way)

Just create a simple scene with a push button that triggers the action to “send a request to a web page”. Remember to create the Global Variables with some values inside it. Press button, than check the “Hiscores.php” page to view results.

Well, below you can see the Final Game Screen, with the option to save score: (with text entry object ). And the High score Page working.