Plugin Name: Simple Ajax
Version: 1
Author: Amy Pond @ http://www.hbgames.org
Introduction
As RPG Maker MV games use JavaScript, one simple way to talk to a server is via the web, using Ajax. Technically here we aren't using the x part unless we format the responded text using XML, but the principle is the same.
This plugin is one simple function that returns a text variable from a web server using some entered variables.
How to Use
Create a PHP file on your web server which returns a text response (using echo() for example). Use GET variables to generate the response based on data sent to it.
There is an example PHP file in the Help part of the plugin.
Install the plugin, and type in the location of your PHP file, for example: http://www.rpgmakerweb.com/files/ajax.php (not a real file).
As an example of its usage:
var mytext = ajaxRequest("variable=1&othervariable=2");
On the server we check for $_GET['variable'] and $_GET['othervariable'].
We clean the variables before using (this is very important - please read up on cleaning user sent data in PHP).
Then we do something with these variables and send back some data. Say we echo("hello"), then:
var mytext = ajaxRequest("variable=1&othervariable=2"); // mytext = "hello"
Credits/thanks
By Amy and originally created for the game Afar.
Thanks to w3schools.
Terms of Use
Free for use in any game with no credit necessary. If you like you can provide a link to HBGames.org or this thread but this is not necessary. Available for commercial games for free.
Please note that this script used incorrectly creates a vulnerable web server. If you do not clean your GET variables then you ARE open to hacking quite easily. No liability is accepted for any damage caused by use of this function. Use is at your own risk and I urge you to look into PHP security, in particular, cleaning data sent from a user. For integers this can be as simple as turning them into integer variables by (int)var, though for text you will need prewritten functions, or to make your own using preg_replace.
Version: 1
Author: Amy Pond @ http://www.hbgames.org
Introduction
As RPG Maker MV games use JavaScript, one simple way to talk to a server is via the web, using Ajax. Technically here we aren't using the x part unless we format the responded text using XML, but the principle is the same.
This plugin is one simple function that returns a text variable from a web server using some entered variables.
How to Use
Create a PHP file on your web server which returns a text response (using echo() for example). Use GET variables to generate the response based on data sent to it.
There is an example PHP file in the Help part of the plugin.
Install the plugin, and type in the location of your PHP file, for example: http://www.rpgmakerweb.com/files/ajax.php (not a real file).
As an example of its usage:
var mytext = ajaxRequest("variable=1&othervariable=2");
On the server we check for $_GET['variable'] and $_GET['othervariable'].
We clean the variables before using (this is very important - please read up on cleaning user sent data in PHP).
Then we do something with these variables and send back some data. Say we echo("hello"), then:
var mytext = ajaxRequest("variable=1&othervariable=2"); // mytext = "hello"
JavaScript:
//=============================================================================
// Ajax.js
//=============================================================================
/*:
* @plugindesc A simple AJAX interface.
* @author Amy Pond @ [url=http://www.HBGames.org]http://www.HBGames.org[/url]. Created for Afar - the MMORPG
*
* @param URL
* @desc The URL to your actions php file.
* @default N/A
*
* @help To use this plugin you need a web server that can run PHP.
* On this server, host a PHP file.
* This file should check for GET variables, clean them (IMPORTANT),
* and return back a message using echo(). The function is called
* by ajaxRequest(parameters) where parameters takes the form:
*
* var1=data&var2=data&var3=data
*
* For example, I may wish to request the result of the equation 2+3.
* In game I call the function: ajaxRequest(action=add&number1=2&number2=3);
*
* Server side, using $_GET and cleaning it to an integer for security,
* I take these variables, add them together, and return the result:
*
* <?php
* //action.php
* switch($_GET['action'])
* {
* case 'add':
* number1 = (int)$_GET['number1'];
* number2 = (int)$_GET['number2'];
* result = number1 + number2;
* echo result;
* break;
* case 'login':
* // code goes here
* break;
* }
* ?>
*
*/
ajaxRequest = function(args) {
var retrievedData = "";
var parameters = PluginManager.parameters('Ajax');
var get_params = args[0];
// create a new Ajax request
var xhttp = new XMLHttpRequest();
// This function is performed when a message is returned from the Ajax request
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
// set var retrievedData to the returned text
retrievedData = xhttp.responseText;
}
}
// open the xhttp connection and send it
// this function is sync not async, such that the game will wait for the
// response. Change false to true and the code will run while the game
// does other things, so you don't have to pause for non-important things
// where the player doesn't need a response to continue, such as merely
// updating the online players list.
xhttp.open("GET", parameters['URL'] + "?" + get_params, false);
xhttp.send();
// return the retrieved text
return retrievedData;
};
By Amy and originally created for the game Afar.
Thanks to w3schools.
Terms of Use
Free for use in any game with no credit necessary. If you like you can provide a link to HBGames.org or this thread but this is not necessary. Available for commercial games for free.
Please note that this script used incorrectly creates a vulnerable web server. If you do not clean your GET variables then you ARE open to hacking quite easily. No liability is accepted for any damage caused by use of this function. Use is at your own risk and I urge you to look into PHP security, in particular, cleaning data sent from a user. For integers this can be as simple as turning them into integer variables by (int)var, though for text you will need prewritten functions, or to make your own using preg_replace.