How to Add Code to a GameMaker: Studio Project

By Michael Rohde

GameMaker: Studio helps developers create videogames for just about any platform. You create Objects through an Object Properties window. Within that window, you can add Events and Actions to define the Object. To open a code window, you add an Execute Code Action to an Event, such as a Keypress Event, where the player presses S to save the game.

Here’s an example procedure on how to add code to a GameMaker: Studio project. If you haven’t already, download a free, limited version of GameMaker: Studio on the YoYo Games manufacturer’s website to try this yourself.

  1. With a project open, create a new Object by choosing ResourcesNew Object from the main menu.

    The Object Properties window appears.

    image0.jpg

  2. Click the Add Event button.

    The Event menu window appears.

  3. From the Event menu window, choose Letters.

    A submenu appears.

  4. From the submenu, choose S.

    The Keypress Event for the letter S appears in the Events section of the Object Properties window.

  5. Drag and drop an Execute Code Action from the Control tab to the Actions section of the Object Properties window.

    A code window appears.

  6. In the code window, type the following:

    image1.jpg

    ///save game 
    var file, str;
    //Open file for writing //two slashes are used for normal comments
    file = file_text_open_write("level.txt");
    //Create player string
    str = string(obj_Player.x) + "|" + string(obj_Player.y) + "|";
  7. Click the green check mark.

    The code window is saved and closed.

You can name the Execute Code Action by using three slash marks as shown in the first line of this code (whereas a normal commented line uses two slash marks as shown in the third line of the code). Note that if you use ///save game, when you close the code window, in the Actions section, Execute a piece of code will now read save game. This can be useful when you have several blocks of code in the Actions window.