Game Maker Language - Code Examples

Code Examples

Here is a simple piece of code that would display "Hello World!" in a popup message box.

show_message("Hello World!");

Another example that would display the same text in the game window instead. Note that by default, Game Maker redraws the background continuously, so using the default setting, this code would need to be attached to the draw event for drawing to work properly.

draw_text(0, 0, "Hello World!");

Here is a piece of code from a game using GML:

// This is a comment /* this is a C-Style comment. */ /* temporary variable declaration. A temporary variable will be released at the end of a script. Note that this doesn't declare it to be of a specific type! */ var xx, yy, nn; // A conditional. It can also be shortened to "if (can_shoot)". if (can_shoot = true) // "=" and "==" can be used interchangeably in conditionals { // This begins a block of code. You can also use "begin" as with Pascal. /* Here we are setting the variable to false. This could also be written as "can_shoot = 0;" since Game Maker doesn't distinguish between integer and boolean types. */ can_shoot = false; /* Here you are setting the 0th alarm to five steps. The alarm variable will automatically count down to 0, and when it hits 0, the alarm0 event will be triggered. */ alarm = 5; /* Here the temporary variable xx is defined implicitly as an integer, and the lengthdir_x function is used. */ xx = x + lengthdir_x(14, direction); yy = y + lengthdir_y(14, direction); //This function creates an obj_bullet and then returns its instance id to nn. nn = instance_create(xx, yy, obj_bullet); /* The with statement allows you to access the fields of an object directly, without having to write statements like nn.speed or nn.direction. */ with (nn) { speed = obj_tank.speed + 3; direction = obj_tank.direction; } }

GML supports many variations in syntax. As a demonstration, the previous example could also be written like this:

var xx, yy, nn; if can_shoot = true then begin can_shoot := false alarm := 5 xx := x + lengthdir_x(14, direction) yy := y + lengthdir_y(14, direction) nn := instance_create(xx, yy, obj_bullet) with nn begin speed := obj_tank.speed + 3 direction := obj_tank.direction end end

Here is an example of basic keyboard-controller movement. The motion_set function takes two arguments: direction (degrees) and speed (pixels per step). Calling this function will assign an instance with a "speed" and "direction" (both built-in local variables), which Game Maker uses to update the instance's position automatically each step (an instance's position can also be modified directly using the built-in local "x" and "y" variables).

if (keyboard_check(vk_left)) motion_set(180,4); if (keyboard_check(vk_up)) motion_set(90,4); if (keyboard_check(vk_right)) motion_set(0,4); if (keyboard_check(vk_down)) motion_set(270,4); if (keyboard_check(vk_nokey)) motion_set(0,0);

Here is an example of a more complex script from a platform game. Using this, the player can walk over hills and bumpy terrain.

if (!place_free(x-4,y)) { if (place_free(x-4,y-4)) { x-=4; y-=4; } else if (place_free(x-3,y-5)) { x-=3; y-=5; } else if (place_free(x-2,y-6)) { x-=2; y-=6; } } else x-=4;

Here is a piece of code that draws a simple 3D floor (the function d3d_start must be called beforehand).

d3d_draw_floor(0,0,0,500,600,1,background_get_texture(background1),.01,.01);

Read more about this topic:  Game Maker Language

Famous quotes containing the words code and/or examples:

    Wise Draco comes, deep in the midnight roll
    Of black artillery; he comes, though late;
    In code corroborating Calvin’s creed
    And cynic tyrannies of honest kings;
    He comes, nor parlies; and the Town, redeemed,
    Gives thanks devout; nor, being thankful, heeds
    The grimy slur on the Republic’s faith implied,
    Which holds that Man is naturally good,
    And—more—is Nature’s Roman, never to be
    scourged.
    Herman Melville (1819–1891)

    No rules exist, and examples are simply life-savers answering the appeals of rules making vain attempts to exist.
    André Breton (1896–1966)