Before dealing with the asynchronous functions (referred to from now on as async functions) in GameMaker: Studio it may be that you are wondering what the word "asynchronous" actually means! Well, the actual dictionary definition of asynchronous is:
"pertaining to a transmission technique that does not require a common clock between the communicating devices; timing signals are derived from special characters in the data stream itself."
In the context of GameMaker: Studio, an async function is one that sends out to a web server, or asks for user input, or even streams data, while letting GameMaker: Studio continue to run without
blocking. When the information callback is received a special Async Event is fired which, in turn, allows any instance with an Async Event assigned to it to execute further functions (although this does not
have to be the case). So, basically, it's a way to communicate with some external resource (server, user or device) without the game or project blocking while waiting for a reply.
Why is this important? Well, since it allows GameMaker: Studio to continue functioning while sending or receiving data, and this means that you can do many things all without blocking the game loop like -
- stream data from the device into your game without the player waiting for things to load
- have callback events to do things only when the correct information is received
- communicate and interchange data with a web server
This all makes everything function in a much more fluid and unobtrusive way. It should be noted that the Async Events will be triggered for all instances that have them, so you can use an async function in one
instance, and have the Asynchronous Event that deals with the reply in another one, or even various other ones. You can go here for more information on how the Asynchronous event works -
Advanced Use: Asynchronous Events.
Below you can find a list of the Async http functions available in GameMaker: Studio:
Please note that the above http_ functions may not function as expected due to cross domain security issues. This means that requests to your server or attempts to
load resources from across domains are blocked and may appear to return blank results or 404 errors. One of the ways you can get around this is to have some server
side PHP which allows certain domains to access your server (this is also a way to protect your resources and block servers that are not included in the PHP allow list).
The following is an example of the code you can use for this:
$http_origin = $_SERVER['HTTP_ORIGIN'];
if ($http_origin == "http://127.0.0.1:51268")
{
header('Access-Control-Allow-Origin: *');
}
Previous versions of GameMaker had pop-up dialogues that blocked the runner while waiting for a response from the user. However most modern devices do not like this and will interpret
the runner stopping as an error and close the app. To prevent this GameMaker: Studio has async equivalents of these functions that fulfill the same purpose as previous versions, but do not
block the game runner at any time, meaning that when called the game will run as normal in the background while the user responds to the shown dialogue. Once the user has responded, a
Dialogs Asynchronous Event will be called so that you can deal with the results.
Below is a list of all the available asynchronous dialogue functions: