Creating your own Leaderboard / Highscores Easy and Free (PHP & MySQL) [UPDATED 2020]

25
  • 69 favourites

Index

Attached Files

The following files have been attached to this tutorial:

.zip
.capx

highscore-example.capx

Download now 180.37 KB

Stats

31,291 visits, 85,266 views

Tools

License

This tutorial is licensed under CC BY 4.0. Please refer to the license text if you wish to reuse, share or remix the content contained within this tutorial.

Highscores in Construct 2

We have our PHPs ready. Now you only need to call these PHPs to read/write scores from your Construct 2 game!

Open your game and do the following steps:

1. Right Click and "Insert new object". Select Web -> AJAX

2. Open the highscore_example.capx to help you

3. You need a "Score" variable

4. You need a "DOMAIN_SCORES" constant. Write here your free domain! Where your phps are uploaded. Example: "http://duquekarlgames.000webhostapp.com/"

Reading Scores

To read scores from your server database, take a look to the example .capx attached to this tutorial.

Line 2:

Here an AJAX Request reads the php file "getscores.php" from your server

Line 3:

"On Completed" the Reading of Scores, we will receive the data in Ajax.LastData. Here is the actions we will do:

a) Set the group "Do Array" active: to show a Top 10 Leaderboard with names and scores

b) Read the Best Score #1 (name and score) and write that in a Text field.

Do Array:

This group reads the 10 scores received and populates the Text fields.

Scores are received as a string with the following format: name1|score1|name2|score2|name3|score3...

Example:

We split this info using the separator "|". To access any of them you can use this:

"Best Score: " & tokenat(AJAX.LastData, 0, "|") & " - " & tokenat(AJAX.LastData, 1, "|")

Example:

"1st best score: " & tokenat(AJAX.LastData, 0, "|") & " - " & tokenat(AJAX.LastData, 1, "|")

"2nd best score: " & tokenat(AJAX.LastData, 2, "|") & " - " & tokenat(AJAX.LastData, 3, "|")

"3rd best score: " & tokenat(AJAX.LastData, 4, "|") & " - " & tokenat(AJAX.LastData, 5, "|")

etc

Sending Scores

For sending scores, you can read the example .capx:

Line 8:

When you click "Submit", your typed name and the score (randomly generated in this example) will be sent to 'savescores.php'

Line 9:

This is an example of doing some action when the score sending is succesfully "Completed". In this case, we will reload again the best scores. And we write a Text message "Score Saved!"

BONUS: Get Player's Rank

Want to know your Rank? By popular demand, a 'getrank.php' file has been included inside this tutorial. You call this file like this: [YOUR-DOMAIN]/getrank.php?score=57 (use the current player's best score)

Example:

https://duquekarlgames.000webhostapp.com/getrank.php?score=57

This will check all the scores in the database higher than '57'. Let's say we have scores '100' and '93' and '48'. Then the php file will return rank: '3' for score '57'. Because with your score we found 2 better guys, so you are number 3.

This is included in the .capx example.

What's Next?

This is a basic High Score system to learn the basics. Once you understand well what is happening behind the scenes, I suggest you to play around and make a few improvements.

Here is a list of a few things you can do:

  • Add an 'email' field/column to your 'scores' table.
  • Add a 'time' column to your 'scores' table to save when the score was submitted.
  • You can have scores separated by 'level' or by 'difficulty' (easy, medium, hard).
  • Prevent Cheating. Idea: Add a secret key when you send scores to [YOUR-SITE]/savescores.php?name=john&score=51&hash=[7hdndid2in2d...]. Then you can decypher this key inside 'savescores.php' and it should correspond with the submitted score. This way you guess if the score is real or fake. If the score/key is invalid, you don't save it to the DB. Idea 2: "record" player's input and save it (moves, rand seed, etc). Replay the game and check if the score is correct.
  • Only One (the best) score per person.
  • If you have 2 or more games on the same DB, you will need different table names. Like "flappybird-scores" and "stickninja-scores".
  • Inside your .capx you can make Leaderboard pages, like: page 1 [scores 1-10], page 2 [scores 11-20], etc.

Final Words

Any other thing you need to do, leave your questions. You can find great PHP and MySQL tutorials on https://www.w3schools.com/. Also try Google, there is a lot of info on PHP and scores, etc.

Hope you find this tutorial useful. Let your comments at the bottom!

Enjoy and thanks for reading!

.ZIP
.CAPX

highscore-example.capx

Download now 180.37 KB
  • 99 Comments

  • Order by
Want to leave a comment? Login or Register an account!
  • [FIXED] The scripts save/get should be updated according to the latest PHP formatting rules. After fiddling after a bit I found that in the save.php you should remove the "id" parts when inserting the score.

  • Great tutorial for a refresher. Ive just finished reading through the tutorial and will soon be implementing the PHP.

  • Thanks for this super clear tutorial! I like your idea about separating the scores by 'level' or by 'difficulty' (easy, medium, hard). Do you have a tutorial for this? Thanks alot!!!!

    • First, your scores table must have a new column. So you have: name, score AND level/difficulty.

      Then, when submitting score to savescores.php, you already sent name & score. Now you must also add the new variable: level/difficulty.

      Last, when getting scores with getscores.php, let's say you will read only scores for level 2 or difficulty easy.

      You can send a variable level/difficulty from the game to getscores.php. Edit the getscores.php PHP file to read from the Database only the scores with the requested level/difficulty.

  • hello, I have successfully created Hiscore with this tutorial. But I have a problem when I want to update the database or delete it.

    Can you please help how to update and delete the database?

  • Hi! Thanks a lot for this tutorial and for giving us the example projects and folders! This is really what users want!

    Could you please explain how could I improve/edit the design of the leaderboard tables? What would be the method? I there a way I can create the design on a software like Dreamweaver and have this php/mysql leaderboard implemented? Ou maybe even edit the design as a page/post on Wordpress?

    Lastly, would you be wiling to help me doing this for a work project of mine? Off course I'll be glad to pay something.

    All the best,

  • Load more comments (92 replies)