Saving Game Attributes to Server

WarrenTierney

  • Posts: 13
I am a complete noob and only started using servers, php, mysql and Stencyl last week. But anyone who reads these posts knows I have been trying to save game attributes to a server. I found a way. This is not to suggest it is the best way or the only way, or that people did not know. I am just putting it out there so some poor other person does not have to spend 5 days coming up with a way to do this.


This is just a brief outline of how to save Game Attributes to an online server.  In order to achieve this you need 3 communication pathways.

1. The POST function available in Stencyl.
2. A php file on your server to receive the POST (with the GET or REQUEST function; https://www.youtube.com/watch?v=IQmpTxKh9k8).

Example of code for a POST:
[/b]
<?php

   include('connect-mysql.php');
   
   $fname = $_REQUEST['fname'];       (Notice how there is no submit button here, that it is easier to pass through for Stencyl)
   $sqlinsert = "INSERT INTO People (firstname) VALUES ('$fname')";
      
   if (!mysqli_query($dbcon, $sqlinsert)) {
      die('error inserting new record');
   }
   
   $newrecord = "1 record added to the database";
      
?>

<html>   
<head>
<title>Hello World</title>
</head>
<body>

<h1>Insert Data into DB</h1>
<form method="REQUEST" action="Prosses.php">(Processes.php is usually called Index.php but just the php file receive the post)
<fieldset>
   <legend>Score</legend>
   <label>First Name: <input type="text" name="fname" /></label>
</fieldset>


<input type="submit" value="add new person" />
</form>

<?php
echo $newrecord
?>
 
</body>
</html>

Or you can use GET
[/b]

<?php

include('connect-mysql.php');

   $fname = $_REQUEST[fname];
    $sqlinsert = "INSERT INTO People (firstname) VALUES ('$fname')";
   
if (!mysqli_query($dbcon, $sqlinsert)) {
      die('error inserting new record');
   }
   
   $newrecord = "1 record added to the database";
   
echo $fname
      
?>

PHP to MySql database
[/b]
3. Then the php should be set up to relay the data back to a mySql server (https://www.youtube.com/watch?v=duMcPNbLGRI). 

<?php

DEFINE ('DB_USER', 'YOURUSERNAME');
DEFINE ('DB_PSWD', 'YOURPASSWORD');
DEFINE ('DB_HOST', 'YOURHOST');
DEFINE ('DB_NAME', 'Score');

$dbcon = mysqli_connect (DB_HOST, DB_USER, DB_PSWD, DB_NAME);


if (!$dbcon) {
   die('Fuck you fucker');
    }

echo 'This is So annoying';

?>

Once you are in a position where you know that the php is connected to the server i.e., you can post or include information at the end of the .php url like ?fname=Warren and this information is sent to MySql and stored on the database, you are ready.

Final Part

Note in this demonstration I set the post to receive from the field name: fname and the table area in MySql was People, just like the Youtube clip.

Load the game to the server.

You need the subsequent code:

This is what Stencyl recommends



Now the code is sent in exactly the same way as the recommended method is suggested by Stencyl, it is just programmed in a way that makes the raw code the game attribute readable by the php file and transferable to MySql.

Hope this helps someone!

captaincomic

  • *
  • Posts: 6108
Thanks for sharing this. This gets asked regularly, so I'm sure this will help somebody.

('Suggestions' is for suggesting new features - moving to 'Shared Resources' the new 'Guides & How-to's')

« Last Edit: December 21, 2014, 11:00:04 am by captaincomic »

WarrenTierney

  • Posts: 13
Thanks for the help man! Stencyl is great!

FranAlt

  • Posts: 169
Can you maybe do a tutorial on this or further expand the explanation? This will help me a lot!  :P
Swipe - FREE          Cloud Run - FREE              
                                  
         iOS                                Android
    Android

wolf2013

  • Posts: 70
I am a complete noob and only started using servers, php, mysql and Stencyl last week. But anyone who reads these posts knows I have been trying to save game attributes to a server. I found a way. This is not to suggest it is the best way or the only way, or that people did not know. I am just putting it out there so some poor other person does not have to spend 5 days coming up with a way to do this.


This is just a brief outline of how to save Game Attributes to an online server.  In order to achieve this you need 3 communication pathways.

1. The POST function available in Stencyl.
2. A php file on your server to receive the POST (with the GET or REQUEST function; https://www.youtube.com/watch?v=IQmpTxKh9k8).

Example of code for a POST:
[/b]
<?php

   include('connect-mysql.php');
   
   $fname = $_REQUEST['fname'];       (Notice how there is no submit button here, that it is easier to pass through for Stencyl)
   $sqlinsert = "INSERT INTO People (firstname) VALUES ('$fname')";
      
   if (!mysqli_query($dbcon, $sqlinsert)) {
      die('error inserting new record');
   }
   
   $newrecord = "1 record added to the database";
      
?>

<html>   
<head>
<title>Hello World</title>
</head>
<body>

<h1>Insert Data into DB</h1>
<form method="REQUEST" action="Prosses.php">(Processes.php is usually called Index.php but just the php file receive the post)
<fieldset>
   <legend>Score</legend>
   <label>First Name: <input type="text" name="fname" /></label>
</fieldset>


<input type="submit" value="add new person" />
</form>

<?php
echo $newrecord
?>
 
</body>
</html>

Or you can use GET
[/b]

<?php

include('connect-mysql.php');

   $fname = $_REQUEST[fname];
    $sqlinsert = "INSERT INTO People (firstname) VALUES ('$fname')";
   
if (!mysqli_query($dbcon, $sqlinsert)) {
      die('error inserting new record');
   }
   
   $newrecord = "1 record added to the database";
   
echo $fname
      
?>

PHP to MySql database
[/b]
3. Then the php should be set up to relay the data back to a mySql server (https://www.youtube.com/watch?v=duMcPNbLGRI). 

<?php

DEFINE ('DB_USER', 'YOURUSERNAME');
DEFINE ('DB_PSWD', 'YOURPASSWORD');
DEFINE ('DB_HOST', 'YOURHOST');
DEFINE ('DB_NAME', 'Score');

$dbcon = mysqli_connect (DB_HOST, DB_USER, DB_PSWD, DB_NAME);


if (!$dbcon) {
   die('Fuck you fucker');
    }

echo 'This is So annoying';

?>

Once you are in a position where you know that the php is connected to the server i.e., you can post or include information at the end of the .php url like ?fname=Warren and this information is sent to MySql and stored on the database, you are ready.

Final Part

Note in this demonstration I set the post to receive from the field name: fname and the table area in MySql was People, just like the Youtube clip.

Load the game to the server.

You need the subsequent code:

This is what Stencyl recommends



Now the code is sent in exactly the same way as the recommended method is suggested by Stencyl, it is just programmed in a way that makes the raw code the game attribute readable by the php file and transferable to MySql.

Hope this helps someone!




hej,
this does not work if it's a localhost server ?

WarrenTierney

  • Posts: 13
That I do not know, I was using online server.  I imagine for a local host if your using PHP I would follow those guidelines.

stefan

  • *
  • Posts: 2263
Somewhere in the end of this week I might check if this is also possible using dropbox. I have seen websites build from scratch with dropbox as its host, so this should be able as well I guess...