Need help sending highscores to a webserver with httpObject
by Conor O Kane · in Torque Game Builder · 03/03/2008 (4:19 am) · 9 replies
I want to do an online scoreboard.
So far I have the MySQL database set up, and I have a php file ready to show scores at
http://cokane.com/db_test/displayscores.php which takes input in the form:
This is working fine when I submit scores from a browser, however when I submit scores from within TGB I get an error which suggests that the submitscore.php is not getting the right variables.
Here's my torquescript:
When I type "submitscore(test, 300)" into the console it echoes this:
The 'No score was submitted' is the response from the submitscore.php file, so it's getting to the page ok, but it's not sending the variables correctly. I don't know what the ^'s after the error are about. Does anyone know what I'm doing wrong?
So far I have the MySQL database set up, and I have a php file ready to show scores at
http://cokane.com/db_test/displayscores.php which takes input in the form:
http://cokane.com/db_test/submitscore.php?name=TGBtest&score=100
This is working fine when I submit scores from a browser, however when I submit scores from within TGB I get an error which suggests that the submitscore.php is not getting the right variables.
Here's my torquescript:
function submitScore(%name, %score)
{
%server = "cokane.com:80";
%path = "/db_test/";
%postName = "name=" @ %name;
%postScore = "&score=" @ %score;
%post = %postName @ %postScore;
%script = %path @ "submitscore.php?";
%obj = new HTTPObject(testObject){};
echo("Submitting score to " @ %server @ %script @ %post);
%obj.post(%server, %script, null, %post);
}
function testObject::onLine(%this, %line)
{
echo(%line);
}When I type "submitscore(test, 300)" into the console it echoes this:
Quote:Submitting score to cokane.com:80/db_test/submitscore.php?name=test&score=300
No score was submitted ^^
^^
^^ ^
The 'No score was submitted' is the response from the submitscore.php file, so it's getting to the page ok, but it's not sending the variables correctly. I don't know what the ^'s after the error are about. Does anyone know what I'm doing wrong?
#2
www.garagegames.com/mg/forums/result.thread.php?qt=19441
... but I can't read it because I have a TGB indie license.
03/03/2008 (5:28 am)
Of course the answer is probably in this thread "Using httpobject to post information to php ."www.garagegames.com/mg/forums/result.thread.php?qt=19441
... but I can't read it because I have a TGB indie license.
#3
So you might want to create a dummy php page and just echo back what you get in the $_POST global. That should show you what exactly your receiving from TGB so you can adjust your script(php or torque) accordingly.
Sorry I don't have a better answer!
03/03/2008 (8:37 am)
I have never used the httpObject in torque before, but it seems it does indeed POST the data to the php page. So you might want to create a dummy php page and just echo back what you get in the $_POST global. That should show you what exactly your receiving from TGB so you can adjust your script(php or torque) accordingly.
Sorry I don't have a better answer!
#4
Whether I use $_GET or $_POST doesn't affect what happens when I send data from TGB with a httpObject, either way if I do %obj.post(%server, %script, null, %post);
where %post is for example "name=tgb&score=100" and then echo those back I get:
Name: , Score: ^^
^^
^^ ^
and I don't know what the ^'s are - are those newlines in TGB?
03/03/2008 (5:30 pm)
Thanks for the suggestion Eric. I'm new to php, so I don't know the difference between $_GET and $_POST, however if I use $_POST to retrieve parameters, it no longer reads them from the browser url correctly.Whether I use $_GET or $_POST doesn't affect what happens when I send data from TGB with a httpObject, either way if I do %obj.post(%server, %script, null, %post);
where %post is for example "name=tgb&score=100" and then echo those back I get:
Name: , Score: ^^
^^
^^ ^
and I don't know what the ^'s are - are those newlines in TGB?
#5
I abandoned the HTTPObject entirely and went with the TCPObject scripts from here:
http://www.garagegames.com/blogs/32/10202
Thanks to Harold for writing that, and to Deden on the IRC channel for pointing me in the right direction. You can expect online scoreboards in all my games from now on!
03/04/2008 (2:45 am)
I got it to work!I abandoned the HTTPObject entirely and went with the TCPObject scripts from here:
http://www.garagegames.com/blogs/32/10202
Thanks to Harold for writing that, and to Deden on the IRC channel for pointing me in the right direction. You can expect online scoreboards in all my games from now on!
#7
I'm guessing at a minimum I'd change "www.domain.com" to my own website's URL, but the rest is mostly greek to me... I don't know if I'd have to change any of it, or what. (Aside from what appears to be the path to a PHP script of some sort...)
Speaking of which, would you mind sharing your PHP script, for those of us (me) who know nothing about PHP? And any other tips you may have about the whole process?
03/07/2008 (9:56 am)
Hey Conor, this is great news for me, as I'm looking to do the same thing right now too. As I am also new to PHP, SQL, etc. could you post any pertinent into you think might be helpful to others trying to use those scripts? For instance, there is the line from the TCPObject Torquescript you linked to:%httpCmd="POST /gg/upload.php HTTP/1.1\nHost: www.domain.com:80\nUser-Agent: Torque/1.0 \nAccept: */*\nContent-Length: "@ strlen(%data) @"\nContent-Type: application/x-www-form-urlencoded; charset=UTF-8\n\n" @ %data;
I'm guessing at a minimum I'd change "www.domain.com" to my own website's URL, but the rest is mostly greek to me... I don't know if I'd have to change any of it, or what. (Aside from what appears to be the path to a PHP script of some sort...)
Speaking of which, would you mind sharing your PHP script, for those of us (me) who know nothing about PHP? And any other tips you may have about the whole process?
#8
03/07/2008 (11:39 am)
Er, nevermind, just saw your post to the other page, where you added more details, and posted some code too.
#9
In the php file I have
$name = $_POST['name'];
$score = $_POST['score'];
to catch the name and score variables sent, and I then stick these in a database. I found the tutorial on how to use php to store scores in a database here: www.clickteam.com/eng/resources/online_score/onlinescoreboards.html
The only thing you need to change is the www.domain.com to your domain, and the db_test/submitscore.php to the path to your php file.
03/07/2008 (5:58 pm)
Here's what I changed:function TCPSubmitScore(%name, %score)
{
%obj = new TCPObject(TCPScoreSender);
%obj.name = %name;
%obj.score = %score;
%obj.connect("www.domain.com:80");
}
function TCPScoreSender::onConnected(%this)
{
// add the correct tags for the php to read the name and score correctly
%data = "name=" @ %this.name @ "&score=" @ %this.score;
%data = URLEncode(%data);
%httpCmd="POST /db_test/submitscore.php HTTP/1.1\nHost: www.domain.com:80\nUser-Agent: Torque/1.0 \nAccept: */*\nContent-Length: "@ strlen(%data) @"\nContent-Type: application/x-www-form-urlencoded; charset=UTF-8\n\n" @ %data;
echo(%httpCmd);
%this.send(%httpCmd @ " \r\n");
}In the php file I have
$name = $_POST['name'];
$score = $_POST['score'];
to catch the name and score variables sent, and I then stick these in a database. I found the tutorial on how to use php to store scores in a database here: www.clickteam.com/eng/resources/online_score/onlinescoreboards.html
The only thing you need to change is the www.domain.com to your domain, and the db_test/submitscore.php to the path to your php file.
Torque Owner Conor O Kane