RPG Maker Forums

Plugin Name: Simple Ajax

Version: 1

Author: Amy Pond @ 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"

Code:
//=============================================================================
// Ajax.js
//=============================================================================/*:
* @plugindesc A simple AJAX interface.
* @author Amy Pond @ www.HBGames.org. 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; 
// 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;};

Credits/thanks

By Amy Pond of www.hbgames.org 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.

Latest Threads

Latest Posts

Latest Profile Posts

Day 9 of giveaways! 8 prizes today :D
He mad, but he cute :kaopride:

Our latest feature is an interview with... me?!

People4_2 (Capelet off and on) added!

Just beat the last of us 2 last night and starting jedi: fallen order right now, both use unreal engine & when I say i knew 80% of jedi's buttons right away because they were the same buttons as TLOU2 its ridiculous, even the same narrow hallway crawl and barely-made-it jump they do. Unreal Engine is just big budget RPG Maker the way they make games nearly identical at its core lol.

Forum statistics

Threads
106,040
Messages
1,018,469
Members
137,821
Latest member
Capterson
Top