Přeskočit na obsah

Game Maker Language

Z Wikipedie, otevřené encyklopedie

Šablona:Infobox Software Game maker language je programovací jazyk, který vytvořil Mark Overmars. K tomuto skriptovací programovacímu jazyku je potřeba vývojové prostředí Game Maker. Toto IDE se stará i překlad do spustitelného souboru. Má syntaxi podobnou C s možností použití i syntaxe Pascalu.

GML automatically allocates memory for variables on demand, and uses a form of dynamic typing so that assignments with different types of values are possible.

For example, while in some dynamically typed languages (PHP used as an example) this would work:

$intNumber = 1;
$stringNumber = "one";

echo $intNumber . $stringNumber;

In Game Maker, the equivalent code:

intNumber = 1;
stringNumber = "one";

show_message(intNumber + stringNumber);

will output an error.

In addition, there is no command for releasing a variable to free memory. When an instance is destroyed or a script finishes processing, however, any variables local to that instance or script are released. Hence, to conserve memory, it is advisable that one use variables local to either instances or scripts to store information rather than global variables.

For storing and manipulating larger amounts of data more efficiently, Game Maker has some built in data structures, such as stacks, queues, lists, maps, priority queues and grids. These structures are created, modified, and destroyed through built-in functions. There are also functions for sorting these structures, respective to each structure type. This can be particularly beneficial for speed optimization since the pre-compiled functions avoid the need to cycle through many loops of interpreted code.

Instances and resources

Game Maker does not support pointers to reference locations in memory. Thus, each resource and instance in Game Maker has a unique ID number, which is used to reference that particular resource or instance. This ID number can be used by scripts and functions to reference a particular instance or resource. Upon creation of a resource in GM the name of the resource is defined as a constant which references that resource (for objects the first instance is referenced). The ID of a particular instance of an object can be found using the variable "id".

When creating resources or instances at runtime, its unique ID is returned and may then be passed to other variables and functions. -->

Příklady kódu

Následují příklady kódu.

Úsek kódu Hello world zobrazující text ve vyskakovacím okně:

show_message("Hello World!");

Stejný příklad jako minulý, avšak text vypisuje do herního okna na počátek souřadnic. Většinou je nutné tento kód psát do draw eventu.

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

Komentovaná ukázka z kódu hry napsaná v GML.

// Toto je komentar.
/* Toto je blokovy komentar. */

/* Deklarace lokalnich promennych, ktere budou uvolneny na konci skriptu. 
   Vsimnete si, ze deklarace neobsahuje typy promennych */
var xx, yy, nn;

// Podminka. Muze byt zkracena na "if (can_shoot)".
if (can_shoot = true) // "=" a "==" muze oboji byt pouzito v podminkach jako zapis porovnani.
{  // Zacatek bloku kodu. Je mozno taky pouzit "begin" jako v Pascalu.

   /* Nastaveni promenne na false. Muze byt taky napsano takto: "can_shoot = 0;",
      protoze GML nerozlisuje mezi typy boolean a integer. */
   can_shoot = false;

   /* Zde se nastavi nulty alarm na pet. Promenna alarm bude automaticky snizovana.
      Kdyz bude dosazena nula bude vyvolan alarm0 event. */
   alarm[0] = 5;

   /* Zde lokalni promenna xx definovana jako cele cislo (integer)
      a funkce lengthdir_x je pouzita. */
   xx = x + lengthdir_x(14, direction);
   yy = y + lengthdir_y(14, direction);

   // Toto vytvori obj_bullet a nasledne vrati jeji instance id do nn.
   nn = instance_create(xx, yy, obj_bullet);

   /* Klicové slovo with zpristupnuje clenske promenne objektu primo,
      bez nutnosti zapisovat vyrazy jako nn.speed nebo nn.direction. */
   with (nn)
   {
      speed = obj_tank.speed + 3;
      direction = obj_tank.direction;
   }
}

GML podporuje vice typu syntaxe. Jako příklad může být přepsaní předchozího příkladu v syntaxi podobné Pascalu:

var xx, yy, nn;
if can_shoot = true then begin
   can_shoot := false
   alarm[0] := 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

Toto je příklad jednoduchého pohybu ovládaného klávesnicí. Funkce motion_set má dva parametry: směr ve stupních a rychost v pixelech za krok programu. Její volání nastaví vestavěné lokání proměnné "speed" and "direction" , které Game Maker používá automatickou změnu pozice instance. Tato změna nastává v každém kroku. Pozice instance může být změněná i přímo pomocí nastavení vestavěných proměnných "x" a "y".

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);

Zde je uvedena komplexnější alternativa volání motion_set(180, 4), která kontroluje zda je místo ve směru posunu.

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;

Ukázka volání OpenGL procedury.

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

V tomto článku byl použit překlad textu z článku Game Maker Language na anglické Wikipedii.