buffer_save_async

This function saves part of the contents of a buffer to a file asynchronously, ready to be read back into memory using any of the buffer_load_* functions.

The "offset" defines the start position within the buffer for saving (in bytes), and the "size" is the size of the buffer area to be saved from that offset onwards (also in bytes). This function works asynchronously, and so the game will continue running while being saved, and all files saved using this function will be placed in a "default" folder. This folder does not need to be included in the filename as it is added automatically by GameMaker. For example the filename path "Data\Player_Save.sav" would actually be saved to "default\Data\Player_Save.sav". However, if you then load the file using the function buffer_load_async, you do not need to supply the "default" part of the path either (but any other file function will require it, except on consoles Xbox One, PS4 and Nintendo Switch).

NOTE On HTML5 "default/" is not added automatically and you may have to account for this in your code.

The function will return a unique ID value and trigger an Asynchronous Save/Load Event where you can use the returned ID to check the async_load's ID value, as shown in the extended example below. The async_load map in the event will have the following two key/value pairs:

Note that you can save out multiple buffers in one by calling this function multiple times between calls to buffer_async_group_begin and buffer_async_group_end (see those functions for further information on this).

NOTE On HTML5 the returned results are saved as base64 encoded strings when saved to a file.

 

Syntax:

buffer_save_async(buffer, filename, offset, size);

ArgumentTypeDescription
bufferBufferThe buffer to save.
filenameStringThe name of the file to save as.
offsetRealThe offset within the buffer to save from (in bytes).
sizeRealThe size of the buffer area to save (in bytes).

 

Returns:

Async Request ID

 

Example:

The buffer_save_async function can be called from any event, and since it is asynchronous the callback can be almost instantaneous or could take several seconds. Calling the function is simple and would look something like this:

saveid = buffer_save_async(buff, "Player_Save.sav", 0, 16384);

The above code saves the contents of the buffer buff to the given save file, storing the ID of the function call in a variable saveid. When the save is complete, the asynchronous Save / Load event will be triggered and you can parse the async_load map for the correct ID of the function, like this:

if (ds_map_find_value(async_load, "id") == saveid)
{
    if (ds_map_find_value(async_load, "status") == false)
    {
        show_debug_message("Save failed!");
    }
}

The above code will first check the ID of the DS map that has been created, then check the status of the callback, posting a debug message if there have been any issues.