Static Files and Sprites

Static Files and Sprites

Most websites have a need to serve a variety of static content, not just dynamically-generated pages. To help with this, HTGM has a couple ways serve up static content: A static file server, and a sprite server.

File Server

HTGM can create a dynamic path handler that will serve files from a folder in the datafiles of the GameMaker project. This allows you to include many static files in the project, without having to load them into the game itself. This is useful for things like image files, javascript libraries, or even whole HTML websites.

// Create a server on port 5000
global.server = new HttpServer(5000);
		
// Add a static file server
global.server.add_file_server("demos/file-server/*", "static/demos");
		
// This must run after entering the first room
global.server.start();

In the above example, a dynamic path demos/file-server/* is used, and mapped to a folder called static/demos inside the datafiles of the Gamemaker project. This means if a client accesses a path such as demos/file-server/cat.png, this is mapped to the file static/demos/cat.png in the datafiles folder of the project. HTGM will guess the MIME-type of the file based on its file extension. You can see the demo here.

Sprite Server

HTGM can serve any sprite that is included in the GameMaker project as a PNG. This allows you to make sprite assets in the project available to the webserver.

// Create a server on port 5000
global.server = new HttpServer(5000);
		
// Add a sprite server
global.server.add_sprite_server("demos/sprite-server/{sprite}.png", "sprite");
		
// This must run after entering the first room
global.server.start();

In the above example, a dynamic path demos/sprite-server/{sprite}.png" is used, and the sprite path variable is used to match sprite asset names in the project. This means if a client accesses a path such as demos/sprite-server/sLogo.png, this is mapped to the asset sLogo in the project. You can see the demo here.