Legacy GM How to send variable over from GM to online Javascript file with 'http_post_string'?

jobjorgos

Member
I need instance variable online_players to be send over from GameMaker to players.js on my website
Too bad with the code below there has not anything been updated yet in the players.js file.



Alarm[0] Event:
GML:
http_post_string("http://example.com/***/***/players.js", "online_players="+string(online_player));
//I'm confident that the path is correct, but I guess I should not show the full path to public here because then anybody can adjust players.js on my website?

players.js
JavaScript:
online_players = 0;

//updates every 1 second
var myVar = setInterval(myTimer, 1000);
function myTimer() {
    //realmstatus
    document.getElementById("realmstatus").innerHTML = online_players + " Players are now online in the world"
}

And afterall, are these 'permission settings' of players.js alright?
permission.png
 
Last edited:

chamaeleon

Member
Am I atleast right using http_post_string you think? It's just 1 simple variable I need to be send to my website
Using http_post_string() you are able to send information to your web server, yes (but more than likely it will be a PHP page accepting the data, not javascript, as I assume you're not running a nodejs server). But that in itself won't mean anything for clients without them making requests for new or updated information using other calls.
 

jobjorgos

Member
it will be a PHP page accepting the data
aha, I just did so. I have this now:

players.php
PHP:
<?php
    $online_players = $_POST['online_players'];
?>
It has to be something like this when I search on similar topics. But like the php code is now it is not retrieving anything yet.

I know I might should better go to a PHP forum, but since it's just 1 line of code I think it would be nice for me and others who reads this that someone can demonstrate an example piece of PHP code that retrieves http_post_string
 

FrostyCat

Member
Here's what the most basic form would look like (without any authentication, authorization or validation):
PHP:
<?php
$onlinePlayers = $_POST['online_players'];
$f = fopen('players.js', 'w');
fwrite($f, "online_players = $onlinePlayers;");
fclose($f);
If you're serious about this, I strongly recommend that you learn how to use databases (e.g. MySQL or Mongo) and adopt a PHP framework (e.g. Laravel, Zend, etc.) instead of improvising with raw PHP.
 

jobjorgos

Member
<?php $onlinePlayers = $_POST['online_players']; $f = fopen('players.js', 'w'); fwrite($f, "online_players = $onlinePlayers;"); fclose($f);
Oh that script works very well and does exact what I needed to get the variable from the .PHP to .JS file! So that is very nice


However, sadly I can still not receive any information from GameMaker to my PHP file, and I'm really getting annoyed since I'm trying like a die-hard for 10 hours in a row already now...

Do I really have to use $_POST to receive the string from http_post_string( )?
Because what I know of how $_POST works in HTML forms, u must first give a name attribute (see HTML example below) before $_POST["email"] can receive that information.
And since http_post_string() uses no name attribute, what do I have to fill in between the brackets in $_POST[ ] on my website? Because 'online_players' what I have now makes no sense at all and reffers to nothing.

HTML:
<form action="welcome.php" method="post">

    Name: <input type="text" name="name"><br>

    E-mail: <input type="text" name="email"><br>

    <input type="submit">

</form>

To make sure it is not a problem of my php code I tested it by putting '47' and it works perfect:
PHP:
$onlinePlayers = '47';   //$_POST['online_players'];
Also if I directly visit the url I placed in http_post_string in my browser, it opens players.php on a blank page.
So the path of the url is correct too.


So it is clear that $_POST['online_players']; does nothing/receives nothing





If you're serious about this, I strongly recommend that you learn how to use databases (e.g. MySQL or Mongo) and adopt a PHP framework (e.g. Laravel, Zend, etc.) instead of improvising with raw PHP.
True, I already have an online extension from the marketplace which works with a database with all works fine already.
What I'm doing now with this raw PHP sending stuff is just 1 small exception what I only want for this single variable to be send to my own website.
 
Last edited:

Coded Games

Member
If you want to use JavaScript maybe look into a Function-as-a-Service platform like AWS Lambda. This is what my game currently uses for online high scores and logging.
 

FrostyCat

Member
Of course $_POST is where the variable ends up. In HTML5, the name attribute specifies the left side of the equals sign. You're just replicating that in GML when you do this:
GML:
http_post_string("http://example.com/updateOnlinePlayers.php", "online_players="+string(online_player));
The submitted value will end up in $_POST['online_players'], assuming that the request is done properly (i.e. not cross-domain on HTML5, not plaintext HTTP from Android, etc).

Also, visiting the URL via a browser's address bar sends a GET request, not the POST that PHP code using $_POST would refer to. This is why you need to use a request tester like Postman or cURL (gURL if you prefer a GUI) when developing HTTP-based APIs.
 

jobjorgos

Member
adopt a PHP framework (e.g. Laravel, Zend, etc.) instead of improvising with raw PHP.
look into a Function-as-a-Service platform like AWS Lambda
you need to use a request tester like Postman or cURL (gURL if you prefer a GUI) when developing HTTP-based APIs.
Why do I need all these frameworks, services, etc. etc.? I acctually just want the most simple way to send online_players from gamemaker to players.php. Are they neccesary to write something in a .php file? Then I better pay someone with proffesional knowledge in networking doing it for me I guess.

The submitted value will end up in $_POST['online_players']
So $onlinePlayers = $_POST['online_players']; would define $onlinePlayers with the same value as it would here $onlinePlayers = $_POST['blablabla'];?

And should I not just use $_POST since brackets [ ] in PHP are acctually used for arrays? http_post_string sends a single string and not an array.
 
Last edited:

Coded Games

Member
Networking is hard. Here is how I would implement your backend as an AWS Lambda function in Python (because I like python) in the absolute simplest way possible. No database, no files, just python.

Python:
#
# AWS Lambda Functions Default Function
#
players = list()

def lambda_handler(event, context):

    if (event.playerID not in players):
        players.append(event.playerID)

    return {
        "players": len(players)
    }
So what you would do is create an API Gateway which would give you a URL to call http_post with in GameMaker. Then you would send json like this:

JSON:
{
    "playerID": "GJFIDGJI"
}
This can also be sent as a string with http_post_string.

PlayerID would have to be generated when your game is ran, and they must be unique per player. Serverless (FaaS) platforms destroy infrastructure every 10-15 minutes so you would have to continually call this micro service every few minutes to keep the player count up to date. The unintentional perk of this is that player count would decrease as people stop playing and serverless platforms can scale from 0 people playing to thousands.
 

jobjorgos

Member
Thanks alot for all the help! I tried doing it the way you suggested Coded Games by using Python, but too bad I simply have not enough knowledge doing that.

Since I simply don't have the skills setting up the online connection from GM to my website by myself, I came up on the following solution by getting this extension from the marketplace:

With this extension I can easily send data from my game to my websites MySQL Database, and once online_players succesfully get retrieved by my database it will be really easy to get that data on my website!
 

Mert

Member
This is kinda off-topic, but I'm working on a video tutorial series about writing basic server system for Game Maker games(With Golang). I'll share once the videos are done.
 

jobjorgos

Member
This is kinda off-topic, but I'm working on a video tutorial series about writing basic server system for Game Maker games(With Golang). I'll share once the videos are done.
Oh that is interesting, I really know so less about networking. I don't even know what a gateway or port is so... alot to learn
 

jobjorgos

Member
Seems that the whole networking thing is still in the state silimar to making games with just 0's and 1's instead of a tool such as GameMaker. I'm pretty sure that within the next 10 years the internet has envolved so everyone can just easily use networking online database functions in general or for GameMaker or Unity.

Right now I'm concluding that still alot of people have to keep reinviting the wheel, and that many things are still not optimal (like sending over data from GM to SQL Database for example, or adding online synch functions to Unity or whatever).
I guess within 10 years it is just a matter of pressing 1 button, filling the hostname/password of a SQL database one time and the connection is done!
 

chamaeleon

Member
Seems that the whole networking thing is still in the state silimar to making games with just 0's and 1's instead of a tool such as GameMaker. I'm pretty sure that within the next 10 years the internet has envolved so everyone can just easily use networking online database functions in general or for GameMaker or Unity.

Right now I'm concluding that still alot of people have to keep reinviting the wheel, and that many things are still not optimal (like sending over data from GM to SQL Database for example, or adding online synch functions to Unity or whatever).
I guess within 10 years it is just a matter of pressing 1 button, filling the hostname/password of a SQL database one time and the connection is done!
No client program (like a game created in GMS, or any other program you can think of implemented in any other language) wanting to work against a database over the internet should ever contact a database directly. You write servers to handle this, in every case, lest you bring down misery upon yourself as soon as someone finds out you're doing it by inspecting your program or the network communication. There are reasons databases in any sane environment is safely hidden behind a server/service, security being high on that list. Client (your game) to server communication should be an abstraction of what you want to communicate (commonly represented using JSON), not a direct representation of the SQL needed to perform the request.
 

jobjorgos

Member
In my previous message I'm not referring to direct commands from a program such as GMS to an internet database like SQL, but I'm reffering to the total picture of online communication between programs.
I know there is a bridge needed and it can not be done directly because they are different programs/languages.
but I guess that you can agree that how network functions/connections has to be used now in game eniges tools such as GameMaker is pretty painful.


Some things on the internet where 20 years ago super hard to do and very uneffecient. You had no GameMaker or Unity and had to make games on a very rought and uneffiecient way in a low level of scripting (just a slight better than just programming with only 0's and 1's).
I compare that with how online functions work now in GameMaker that has to connect with e.g. an online database.

Here you can only read the GameMakers part, but the part of the other side such as requesting those data from php or a database or something is something mysterious and can only be found for those who are experts in networking.

What we need now (and to where we probly will envolve to anyway within 10 years) is that there comes some company with a program that just works automatilly between GameMaker/Unity/Unreal Enige etc. and mySQL for example so not every single game developer has to reinvent the wheel again or just abort their ideas of online functions in their game.


I don't need to learn C++ to make games, so I hope there will also come a time I don't need to learn about networking to use online functions.
 
Top