GameMaker: Windows-specific functions for Studio

While working on one of recent projects, I've stumbled upon few common issues that many meet - even if you are only targeting Windows in GameMaker: Studio, you cannot access files outside the game's AppData directory (not even in program directory). Neither you can order system to open a file, meaning no external "readmes" to be easily hooked up with game, nor portable configuration files, and some other limitations.
So I took an evening and made a simplistic DLL library to bring equivalents to some functions (sleep, execute_shell, non-sandboxed I/O) back for Windows target. Function list is as following:

delay

delay(time_in_ms)

Identical to pre-Studio sleep(ms) routine. Pauses game execution for given number of milliseconds. For example,

delay(2500)

Will pause game execution for 2.5 seconds.

shell_do

shell_do(action, filepath)

Orders system to do specified action to given file. Common ones are "open", "edit", "print", and availability may vary depending on file type and system configuration.

shell_execute

shell_execute(path, args)

Identical to execute_shell. You can also use this to open programs with arguments.

shell_execute_at

shell_execute_at(file, args, directory)

Similar to shell_execute, but you also get to specify working directory for program (or file) ran. This can be useful, if you want to use external tools without specifying absolute paths or having to store them in root of game folder.

file_text_get

file_text_get(path)

Retrieves contents of text file located at given absolute path (no sandboxing) as string. If file does not exist, or an error occurs while retrieving the contents, an empty string is returned.

file_text_put

file_text_put(path, contents)

Replaces contents of file at given path (no sandboxing) with ones provided. If file does not exist, it will be created. Returns, whether the operation was successful.

get_color_win

get_color_win(default_color)

A more or less precise replica of get_color function from older versions of GameMaker.

file_copy_win

file_copy_win(path, newpath)

A non-sandboxed function for copying files. Note: Uses absolute paths!

In addition, the example attached demonstrates usage of most of these functions.

Downloads

Example + extension DLL source code

Revisions

  • January 23, 2016:
    Fixed the issue with file handles not being closed automatically.
    The example is now distributed as ZIP to circumvent problems with importing.
    Source code is now included in the download and is available separately.
    Removed shell_open due to inconsistent behavior on some systems - use shell_do("open", path) instead. Added get_color_win, window_set_caption_ext, file_copy_win.
  • February 12, 2016:
    The DLL no longer requires Visual C++ Runtime to function.
    Should help if it previously didn't work for you.

Have fun!

Related posts:

69 thoughts on “GameMaker: Windows-specific functions for Studio

  1. Hello, can you tell me why gm generates an error when working with more than 4 arguments in a dll. Do you have to get around this situation. Because I’m calling an Api and the function needs to work with 5 mixed arguments. 2 string arguments, 3 real arguments.

    • If you can, switch to an Early Access version of GameMaker: Studio. It has that fixed already.

      Otherwise you’ll need to either split the call into multiple <4arg ones for now, or pass the arguments using a buffer.

      • When I update it generates conflicts with variables and arguments of some Dlls.
        But in version 1.3 that was created the engine runs perfectly.
        The dll is third, I can not modify.
        Where can I find an example with GM that makes one of two options?
        Divide in 4 pars or pass the arguments using a buffer.

        • Password and LoginID must be of type ty_string and rest of the arguments in ty_real
          return (external_call(NKLogin,Password,16,0,0,LoginID));

  2. I updated to the latest version GMS 1.4 and the one that you indicated to me to Early Access and error persists

    Error: DLL functions with more than 4 arguments can not have string arguments arguments

    • That’s unfortunate then, one of people working at YoYoGames said on forums in March that it should be fixed in EA.

      If you do not have source code for the DLL, you can’t modify it to work with multiple functions or buffers.

      You could download a 1.3 version again from the list, but that would have it’s own problems.

      • In this case, I am barred.
        I already downloaded the last version of 2016.
        The error persists, it does not allow more than 4 arguments with string.

  3. Just stumbled across on my search for a shell_execute() command in GMS.

    Works great! Thank you so much!

Leave a Reply

Your email address will not be published. Required fields are marked *