Loading External Resources Online on Game Maker Studio

Summary
This tutorial shows you how you can load external resources from web to your game.

GM Version : Game Maker Studio 1.4 & Game Maker Studio 2
Target Platform : All
Download The Example

Why Loading Files From External Sources ?

  1. Decrease the initial filesize of your game, most people don’t like installing large games(unless they’re AAA:Big games)
  2. You can download further levels of your game while player hangs around in initial levels. This would bring the comfort to the player that they do not have to wait for some download files.(EA Game’s Origin actually do this; from what I observed from Battlefield)
  3. Silent update system, where players can receive fresh updates without updating the main client.
  4. Create a DLC based system where the other content can be downloaded after purchasing/unlocking the DLC package.

This tutorial is based on http requests. Game Maker Studio offers us one useful function http_get_file.

Downloading Files

I create one object and name it as downloader_object and continue with the following code.

 CREATE EVENT

This will simply request a file from server. Be noticed that you can use free file servers(even Dropbox and Google Drive), or your FTP server. Now we are heading to Async Events for handling the data.

 ASYNC – HTTP EVENT (Please check the explanation below as I detailed every part of this code)

Explanation

After firing up a http_get_file request, we receive a data contained in ds_map(async_load,..) in Async – Http Event every time we load a piece of data from that request. This data includes

  • ds_map_find_value(async_load, “id”) = ID of the file we requested. We use this ID to confirm that we are checking the status and getting the data of what we had requested through http_get_file
  • ds_map_find_value(async_load, “status”) = The status of the file.
    A value lower than 0 = ERROR. File cannot be downloaded.
    0 = The file is being downloaded at the moment.
    1 = The file is downloaded successfully!
  • ds_map_find_value(async_load, “result”) = This is the data we received. In http_get_file request, this is the path of the file we downloaded. We can then use, for example, zip_unzip function to extract the data inside of a zip file; later to be used.
  • ds_map_find_value(async_load, “contentLength“) = This is the file size that server tells us. Some servers may not give this information, but we can use this data to create a “loading bar”.
  • ds_map_find_value(async_load, “sizeDownloaded“) = This is the size that we downloaded, in bytes. Combining this information with contentLength can be used for that loading bar screen.

In the code above, I’ve made a simple example for file downloading and I indicated the percentage of the file I am downloading. Later on, I used zip_unzip function to extract the data from the zip file I downloaded. I will load this data after then.

Loading The Data From The Unzipped File

From this point, it is easy to load the data. Imagine that I put a picture and an .ini file into my zip before, let’s call them hello.png and marty.ini, and they are extracted(In the code above, we extracted them to new_content folder. Let’s load them.

 Keyboard Press Event – ENTER button (For Demonstration)

Final Notes

  • For a better view, you can use the percentage for “Loading Bar” and show some cool GUI graphics to players.
  • Be noticed that sprite_add creates a new Texture Page, means that it takes up more space than adding sprite from GMS IDE. In the next topic, I will detail this and give you a cool feature to overcome this.

Post Author: Marty

I think I'm a game developer, I think... :(

Leave a Reply

Your email address will not be published.