The Async Events

The Async Event

An Asynchronous Event is one that is fired when GameMaker receives a callback from some external source, which can be from the web or from the device running your game.

Basically, you tell GameMaker to do something - like load an image - and it will start to do this. As an example, let's say you want to add a sprite from an external file. This can be done synchronously or asynchronously. To do this synchronously, you use the function sprite_add

var _new_big_sprite = sprite_add("my_new_sprite_image.png", 1, false, true, 0, 0);

// A lot more code below this line that all needs to be executed on time to keep a steady FPS

This function has a bit of a disadvantage, however, as it fully loads the sprite immediately. The next line of code will only be executed after that's finished! This may barely be noticeable if you're just adding a single small sprite, but might freeze your game when loading one big sprite or many small ones.

GameMaker also has built-in functions that work asynchronously instead. Using these functions GameMaker only starts doing what you asked, but doesn't stop executing code until this has finished. It continues to process your async request "in the background" and continues to run the rest of your game's code normally. When the request completes, a callback will be sent to GameMaker and any Asynchronous Events defined for that type of callback will be fired.

In the example of sprite_add, you can use its asynchronous counterpart sprite_add_ext to just start loading the sprite and trigger an async event afterwards: 

var _new_big_sprite = sprite_add_ext("my_new_sprite_image.png", 1, 0, 0, true);

// A lot more code below this line that will be executed almost instantly after the call to sprite_add_ext

Async Image Loaded Event

/// "The sprite you requested has finished loading"

There are other situations in which you don't want the game to wait until something that might take an amount of time to complete has finished, such as making HTTP requests to a web server.

You don't always have to call a function yourself first in order to receive a callback in the form of an async event, for example the async System event will be triggered when a gamepad is connected or disconnected. For this you don't have to call a function first.

Please note that the Asynchronous Events are fired for all instances that have them, much like the keyboard events, so you can - for example - do an http_get call in one instance, yet have the Asynchronous HTTP Event in another to deal with the callback.

NOTE You should be aware that due to XSS protection in browsers, requests and attempts to load resources from across domains are blocked and may appear to return blank results when using any of the following events.

There are various types of events associated with the Asynchronous event category, and they are all explained in the sections below:

NOTE The variable async_load which is mentioned in the following sections is only valid in these events, as the DS map that it 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 at all other times.

 

It may be that if you are making extensions for different platforms you would like to target one or more of these asynchronous events for callbacks from your extension functions. To that end, you can find a table below that outlines each of the internal event name constants and their corresponding ID value.

NOTE These event constants are only for use with extensions!

 

Async Event Constants
Constant Value
EVENT_OTHER_WEB_IMAGE_LOAD  60
EVENT_OTHER_WEB_SOUND_LOAD  61
EVENT_OTHER_WEB_ASYNC  62
EVENT_OTHER_DIALOG_ASYNC  63
EVENT_OTHER_WEB_IAP  66
EVENT_OTHER_WEB_CLOUD  67
EVENT_OTHER_WEB_NETWORKING  68
EVENT_OTHER_WEB_STEAM  69
EVENT_OTHER_SOCIAL  70
EVENT_OTHER_PUSH_NOTIFICATION  71
EVENT_OTHER_ASYNC_SAVE_LOAD  72
EVENT_OTHER_AUDIO_RECORDING  73
EVENT_OTHER_AUDIO_PLAYBACK  74
EVENT_OTHER_SYSTEM_EVENT  75
EVENT_OTHER_MESSAGE_EVENT  76