5/18/11

Game Maker: Array Tutorial

Game Maker is a development environment designed to let beginners quickly create their own simple games. Even though Game Maker can be used by those who have no programming experience by dragging and dropping action blocks, it also contains a powerful development language called GML. This contains all the features of any modern programming language, including the capability to use arrays of variables. Using arrays in Game Maker is very easy, and requires only a line or two of code to implement.
    • 1

      Start Game Maker and set up the game environment. Create a new room and an object, and place an instance of the object in the room.

    • 2

      Add a "Create" event for the object and place an "Execute a piece of code" action in it. Paste the following block of code into the window that opens when you double-click the action:

      {

      character_info[0,0] = "Zed";

      character_info[0,1] = 0;

      character_info[0,2] = 36;

      character_info[1,0] = "May";

      character_info[1,1] = 4;

      character_info[1,2] = 348;

      character_info[2,0] = "Consuella";

      character_info[2,1] = 4;

      character_info[2,2] = 224;

      }

      The above code creates a two-dimensional array, three variables across and three variables tall. Each of these nine locations are given a different value -- some strings and some integers.

    • 3

      Add a "Draw" event and place another "Execute a piece of code" action, this time inside of it. Paste the following lines into its code window:

      {

      for (i=0; i<=2; i+=1)

      {

      for (j=0; j<=2; j+=1)

      {

      draw_text(x+(60*i),y+(16*j),string(character_info[i,j]));

      }

      }

      }

      This prints the entire contents of the array to the screen, by way of two nested "for loops." The first loop runs three times, and during each iteration the inner loop runs three times, so that all nine values in the array are drawn.

    • 4

      Run your game to see the array at work. Arrays can be used in this way to store all kinds of data, whether scores, object positions, items in an inventory, or even lines in a multiple-choice conversation with a character in the game.

  • No comments: