Hi guys! It's come to our attention that some of you are unaware of certain changes that are being made to how GM functions with the introduction of GameMaker:Studio, specifically relating to the use of functions like show_message(), keyboard_wait() and many others which stop the game loop while waiting to be resolved. These are no longer going to be supported as mobile devices do NOT like them and GameMaker:Studio will instead be dealing with things in an asynchronous way. So what does this mean? Well, 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."
Why is this important? Well, since it allows GameMaker:Studio to continue functioning while sending or recieiving 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
The Asynchronous Event
An asynchronous event is one that is fired off when GameMaker:Studio receives a "call back" from some external source, which can be from the web or from the device running your game. Basically, you tell GameMaker:Studio to do something, like load an image, and it'll start to do this but continue doing whatever it else it has to do meanwhile its working. Then, when the request has been completed, a call-back will be sent to GameMaker:Studio and any Asynchronous Events defined for that type of call back will be fired. Please note that the Asynchronous Events are fired for all instances that have them, much like the key events, so you can do an http_get call in one instance, yet have the Asynchronous HTTP event in another. There are four types of sub-events associated with the Asynchronous event, and they are all explained below :
Image Loaded and Sound Loaded Events
These two events are triggered when you load an image or a sound into GameMaker:Studio, as long as you have used a valid URL with the applicable load file function. For example say I want to load a background image, and only change the current background to the new one when it has loaded. Well I would have something like this in a create event or an alarm event :
back = background_add("http://www.angusgame...ackground1.png", 0, 0);
This will now start to load the image into the device or the browser, but it will not block GameMaker:studio while it waits for the file to be loaded. Instead GameMaker:Studio will keep running as normal until the image is loaded and the call back triggers the Image Loaded event, where a ds_map (more commonly known as a "dictionary") is created and stored in the special variable async_load. The map contains the following information :
- URL : The complete URL you requested.
- id : The ID of the resource that you have loaded. This will be the same as the variable that you have assigned the resource to.
- status : Returns a value of less than 0 for an error.
HTTP
The HTTP Event is one that is triggered by the call back from one of the http_ functions, like http_post_string. It actually generates a ds_map (more commonly known as a "dictionary") that is exclusive to this event and is stored in the special variable async_load. This ds_map has the following structure :
- URL : The complete URL you requested.
- result : The data received (string only).
- http_status : The raw http status code (if available). This returns the standard web status codes for most browsers, eg: 304 for "Not Modified" or 204 for "No Content", etc...
- id : The ID which was returned from the command. If you fire off a series of http_ requests then you need to know which one you are getting the reply to, and so you would use this value to compare to the value you stored when you originally sent the request to find the right one.
- status : Returns a value of less than 0 for an error.
User Interactions
Like the above events, the User Interaction event is only triggered when it gets a call back from one of the special asynchronous user functions, like get_login_async(). These events are the ones that ask for some type of user input, which can be a name, login details, a number or a colour etc... As most devices do not like sitting in a loop waiting for a reply, they have to be asynchronous and GameMaker:Studio will continue to run in the background while these functions are running until they get the required user input and trigger this event. Again, a ds_map is returned with the id held in the special variable async_load. The values held in this map will depend on the function used, and you should consult the individual entries for each function in this manual for more details.
Note : The variable async_load is only valid in this event, as the ds_map that is points to is created at the start of the event, then deleted again at the end, with this variable being reset to a value of -1.
Asynchronous Functions
http_get()
With this function, you connect to the specified URL in order to retrieve information. As this is an asynchronous function, GameMaker:Studio will not block while waiting for a reply, but will keep on running, whether it gets callback information or not. If it does get a callback, this information will be in the form of a string and will trigger an Async Event in any instance that has one defined in its object properties. You should also note that HTTP request parameters (the bits sometimes "tacked on" to the end of a URL when you submit a form on a web page) are perfectly acceptable when using this function, for example :
http_get("http://www.YoYoGames...ogon?username=" + name);
will pass the string held in the variable "name" to the server as well a retrieve the data from that URL (triggering all Async Events). So, essentially, any time a simple, short piece of data needs to be passed from the client to the server, this would a reasonable choice of function to use.http_post_string()
In computing, a post request is used when the client needs to send data to the server as part of the retrieval request, such as when uploading a file or submitting a completed form, and the same is true of this function in GameMaker:Studio. In contrast to the http_get() request method where only a URL is sent to the server, http_post_string() also includes a string that is sent to the server and may result in the creation
of a new resource or the update of an existing resource or both. Here is a quick example of how it would be used :
var str;
str = ds_grid_write(game_grid);
post[0]http_post_string("http://www.YoYoGames.../game?game_id=" + string(global.game_id), str);
get_login_async()
This function opens a window that asks the user to input a username and password. These arguments can be set as an empty string or you can store previously entered values to use if you wish. This is an asynchronous function, and as such GameMaker:Studio does not block the device it is being run on while waiting for ansser, but rather keeps on runniing events as normal. Once the user has input the details and pressed the "Okay" button, an asynchronous User Interaction event is triggered which, for the duration of that event only, will have a ds_map stored in the variable async_load. This map will contain the two keys "username" and "password", with the corresponding user input stored in each.











