Game Maker 8 Tutorial: Generate a Hexagonal Grid

James Cloud
Many popular war simulation strategy games use hexagonal maps, even Fraxis is reported to be using hexagonal maps in their upcoming sequel to Civilization, Civilization V. Hex maps add more tactical challenges than the traditional square or triangle tile maps used in some other games. This tutorial will show you how to generate hexagonal maps for your Game Maker 8 games, just follow the steps given below.

Step One - Preparation

Before getting into the making of a hexagonal map generation script, you will need to create a sprite and two simple objects.

The image for the sprite should be a single 32 by 32 hexagon. If you do not have your own hexagon sprite image you can use the one supplied below,

i222.photobucket.com/albums/dd180/Keydigit/hexagon.png

Feel free to use it freely in your commercial or non-commercial games.

Create a new sprite and use either your own image or the one provided by this tutorial, and call it spr_Hex.

Next, create an object called obj_Hex and set its sprite to the hexagon sprite you just made a minute ago called spr_Hex.

Once you have created obj_Hex, make another object and call it obj_Generator. This will be the object that will execute the map generator script you will make in the next step. Before you go there however, add this GML source code to the creation event of the new obj_Generator,

// Execute the Script to make a Grid
Generate_Grid(48, 48, 32, 32);

Save the changes and add obj_Generator to your room.

Step Two - Script Creation

Now comes the actual hexagonal map making script. Create a new script and call it Generate_Grid. Either type or copy and paste in the following script,

// Generate_Grid(width, height, hex_width, hex_height);
var width, height, hex_object;
width = argument0;
height = argument1;
hex_width = argument2;
hex_height = argument3;

// Get the total amount of tiles
tiles = width * height;

// Go through and create each tile
ay = 0;
ax = hex_width * -1;
zx = 0;
use_mod = false;
x_mod = hex_width / 2;
y_mod = x_mod / 2;

for (b = 1; b {

if(zx = width)
{
ax = 0;
zx = 1;
ay= ay + hex_height - y_mod;
use_mod = !use_mod;
}
else
{
ax = ax + hex_width;
zx+=1;
}

if(use_mod)
obj_temp = instance_create(ax + x_mod, ay, obj_Hex)
else
obj_temp = instance_create(ax, ay, obj_Hex)
}

Make sure to save the script. The comments inside the script give a brief explanation as to the operations performed in the script.

Conclusion

The newly created Generate_Grid script will create a hexagonal grid, using some basic math, with the supplied dimensions. It takes the width and the height of the desired grid, as well as the width and height of the hexagonal sprite that you are using for the obj_Hex object as parameters. Whenever you wish to create a hexagonal grid just call the Generate_Grid function and pass the desired parameters.

NOTE:
It is typical in the Game Maker community to ask for credit, however these scripts are provided absolutely free and require no credit what so ever.

Published by James Cloud

I like to program and do basically anything that has to do with technology and computers.  View profile

2 Comments

Post a Comment
  • tayete11/15/2010

    I modified it, the code should be:
    // Generate_Grid(width, height, hex_width, hex_height);
    var width, height, hex_object;
    width = argument0;
    height = argument1;
    hex_width = argument2;
    hex_height = argument3;

    // Get the total amount of tiles
    tiles = width * height;

    // Go through and create each tile
    ay = 0;
    ax = hex_width * -1;
    zx = 0;
    use_mod = false;
    x_mod = hex_width / 2;
    y_mod = x_mod / 2;

    for (b = 1; b<=height;b+=1){
    for (a=1;a<=width;a+=1){

    if(zx = width)
    {
    ax = 0;
    zx = 1;
    ay= ay + hex_height - y_mod;
    use_mod = !use_mod;
    }
    else
    {
    ax = ax + hex_width;
    zx+=1;
    }
    if(use_mod)
    obj_temp = instance_create(ax + x_mod, ay, obj_Hex)
    else
    obj_temp = instance_create(ax, ay, obj_Hex)
    }}

  • tayete11/13/2010

    Mmmm...I am very interested in this script, but it seems to have some lines missing.

    "for (b = 1; b {" makes no sense and the script gives an error

Displaying Comments

To comment, please sign in to your Yahoo! account, or sign up for a new account.