Scores on website

Get help using Construct 2

Post » Tue Sep 27, 2011 6:53 pm

Is there any way to save scores on my website?
B
14
S
4
G
2
Posts: 42
Reputation: 2,046

Post » Tue Sep 27, 2011 8:17 pm

Yes there is - I had a similar problem a couple hours ago.

Check out the ajax tutorial here:

http://www.scirra.com/tutorials/61/ajax-example-with-construct-2/page-1

Say you wanted to request savescores.php - you would need two pass two values in - one being the username for identification purposes, and the second being their score.

The PHP would look something like:


[code]

<?php

/*
     You will need to create the database, then create the "scores" table by running the SQL query below.
     
     CREATE TABLE `scores` (
     `id` INT NOT NULL AUTO_INCREMENT ,
     `name` VARCHAR( 20 ) NOT NULL ,
     `score` INT NOT NULL ,
     PRIMARY KEY ( `id` )
     ) ENGINE = InnoDB;


*/

$db = "your_database";//Your database name
$dbu = "yourdb_user";//Your database username
$dbp = "yourpassword";//Your database users' password
$host = "localhost";//MySQL server - usually localhost

$dblink = mysql_connect($host,$dbu,$dbp);
$seldb = mysql_select_db($db);

if(isset($_GET['name']) && isset($_GET['score'])){

     //Lightly sanitize the GET's to prevent SQL injections and possible XSS attacks
     $name = strip_tags(mysql_real_escape_string($_GET['name']));
     $score = strip_tags(mysql_real_escape_string($_GET['score']));
     $sql = mysql_query("INSERT INTO `$db`.`scores` (`id`,`name`,`score`) VALUES ('','$name','$score');");
     
     if($sql){
     
          //The query returned true - now do whatever you like here.
          echo 'Your score was saved. Congrats!';
          
     }else{
     
          //The query returned false - you might want to put some sort of error reporting here. Even logging the error to a text file is fine.
          echo 'There was a problem saving your score. Please try again later.';
          
     }
     
}else{
     echo 'Your name or score wasnt passed in the request. Make sure you add ?name=NAME_HERE&score=1337 to the tags.';
}

mysql_close($dblink);//Close off the MySQL connection to save resources.
?>

[/code]


EDIT: To retrieve the scores, just run mysql_fetch_array (or object, depending on your preference) and loop through the returned results. It's up to you for how you want them displayed. I suggest doing an ORDER BY `scores`.`score` DESC LIMIT 10 query so you can do a "Top 10" leaderboard.Pilfer2011-09-27 20:24:57
B
2
G
1
Posts: 4
Reputation: 458

Post » Tue Sep 27, 2011 10:01 pm

I have a simple scoring system in the test game I created along with the tutorial. I'm going to notate how to add a scoring system that will expand the tutorial a bit. Since the physics stuff came out, I'd like to update the game too.

The reality is that it's mostly PHP that does the bulk of the work for you.
B
18
S
8
G
7
Posts: 120
Reputation: 5,630

Post » Fri Oct 21, 2011 2:52 am

How can the PHP be created from the game?
B
12
S
2
G
2
Posts: 28
Reputation: 1,695

Post » Fri Oct 21, 2011 4:57 am

[QUOTE=Daniel] How can the PHP be created from the game?
[/QUOTE]

Daniel,

The php page is created on the server. It is already made.

@Pilfer, thanks for posting that code snippet.
B
18
S
8
G
7
Posts: 120
Reputation: 5,630

Post » Fri Oct 21, 2011 8:12 am

Hi Ghengis

I am a complete novice in programing. but have be strugling with
a highscore/top10

Could you PLEASE tell how its done, or place a sample xcap

B
22
S
12
G
10
Posts: 322
Reputation: 7,353

Post » Fri Oct 21, 2011 6:16 pm

[QUOTE=flemmig] Hi Ghengis

I am a complete novice in programing. but have be strugling with
a highscore/top10

Could you PLEASE tell how its done, or place a sample xcap

[/QUOTE]

I've been really busy with my day job but have started cobbling together an example. I know there was another member who provided an example as well. If I get a couple of hours in the next week I will post a tutorial.
B
18
S
8
G
7
Posts: 120
Reputation: 5,630

Post » Sat Oct 22, 2011 3:08 pm

1000 thanks .... Will be so glad
Have a Nice weekend
B
22
S
12
G
10
Posts: 322
Reputation: 7,353


Return to How do I....?

Who is online

Users browsing this forum: Lof and 4 guests