GameMaker Studio 2


创建人: dougen

190 信息 1072 成员
游戏开发工具 GameMaker Studio 2 的讨论小组


Linpean 2017-07-06



  1. GML语言概述内容:
  • 1.1.基础编程结构(BASIC CODE STRUCTURE)
  • 1.2.函数FUNCTIONS
  • 1.3.脚本SCRIPTS
  • 1.8.赋值/求值顺序(EVALUATION ORDER)
  • 1.9.数据类型(DATA TYPES)
  • 1.10.数组(ARRAYS)
  • 1.11.赋值(ASSIGNMENTS)
  • 1.12.运算(EXPRESSIONS)
  • 1.13.访问器(ACCESSORS)
  • 1.14.语言特性(LANGUAGE FEATURES)




Define: A function has an input and an output, and the output is related somehow to the input.

<function>(<arg0>, <arg1> ,... <arg15>);

two types of functions:

  • 大量的Gamemaker内置函数
  • 任何在Gamemaker用户自建的脚本


  1. instance_destroy();
  2. // Destroy the calling instance - this requires no arguments and returns nothing
  3. dist = point_distance(x, y, mouse_x, mouse_y;
  4. // Get the distance from the current instance position to the mouse position - takes four arguments and returns a real


  1. instance_nearest(x, y, obj).speed = 0;

The return value for the expression in that code example is a real number (the unique ID value for the nearest instance) and so it must be enclosed in brackets to be used in this way and properly address the instance required (see Addressing Variables In Other Instances for more information). [以便以这种方式使用,并正确地处理所需的实例(参见其他实例中的寻址变量以获得更多信息)]The above code would be correctly written as:

  1. (instance_nearest(x, y, obj)).speed = 0;
  2. //or
  3. var inst = instance_nearest(x, y, obj);
  4. inst.speed = 0;


Scripts are essentially【实质上】 functions that you write yourself as short snippets of code that can resolve[解析] expressions, return values or anything else that the language permits. Basically if you have a block of code that you use in more than one place or object, then it's probably better off in a script, as it means that you can change it once when required and the change will be "picked up" by every object that has a call to the script. They can also be very handy from an organisational point of view, even if the code they hold is called only once by a single object, since it permits you to break large code blocks into more easily managed "chunks" at logical points.



脚本参数是在脚本被调用时,需要传入的值,它在脚本以变量的形式储存,在脚本调用时,you must ensure that the script uses all the supplied arguments.,如果传入的参数不够,将会报错。

argument[0 ... max_num]


但是在脚本被调用时,不用混合使用两种类型的参数,argument[0,1,argument[2,3]]是错误的,要是使用argument0,argument1 ...,要么使用一个数组argument[0 ... max_num]

return <expression>


  1. {
  2.   if !is_real(argument0)
  3.     {
  4.       return 0;
  5.     }
  6.   else
  7.    {
  8.       return (argument0 * argument0);
  9.    }
  10. }





  • 用双斜杠进行单行注释
  1. //initialize variables
  2. sr = 10;
  3. hp = 100;
  • 用/* */进行多行注释,

  1. /*
  2. usage:
  3. diff = angle_difference(angle1,angle2);
  4. angle1 first direction in degrees, real
  5. angle2 second direction in degrees, real
  6. returns: difference of the given angles in degrees, -180 to 180
  8. */
  9. {
  10. return ((((argument0 - argument1) mod 360) + 540) mod 360) - 180;
  11. }
  • 如果想要在调用脚本时,得到内置函数一样的提示,需要以JsonDoc风格的注释在脚本开头注明。

  1. /// @description Compare an instance object index with that of another.
  2. /// @param {real} instance_id The unique instance ID value of the instance to check.
  3. /// @param {real} object_index The object index to be checked against.
  4. if argument0.object_index == argument1
  5.  {
  6.     return true;
  7. }else return false;


These keywords are used primarily to identify instances and each one is explained in the text below. Note that all keywords are represented by a negative integer value internally, so care must be taken when assigning values to variables, as you may get unexpected results later as GameMaker Studio 2 interprets the value you have used as something else. You should also note that using the values instead of the keywords in your code is not at all recommended and could cause issues later on.


Self(-1): The instance which is executing the current block of code.指代当前正在执行的代码所归属的实例
Other(-2): The other instance involved in a collision event, or the other instance from a with function.使用with函数返回,或者碰撞函数返回的实例。


  1. var nnn;
  2. nnn = instance_create(x, y, obj_Bullet);
  3. nnn.damage = 5 + irandom(5);
  4. nnn.speed = 8;
  5. nnn.direction = point_direction(x, y, obj_Player.x, obj_Player.y);

See how we[看看我们如何] set its variables using the point method outlined[后置过去分词定语:上文概述过的] in the section on Addressing Variables[看看我们在Addressing Variable部分讲过的指针方法来给变量赋值]? This will give the bullet objects a different damage value[这会给子弹对象赋值不同的伤害数值]. But what about the player object? How will it detect the damage that it has to take[如何侦测到它自己的受到伤害]? By using other in the collision event[那就是在碰撞事件中使用other变量]:

  1. hp -= other.damage;
  2. if hp <= 0 instance_destroy();

The above code will deduct the amount stored in the other instance in the collisions "damage" variable from the player "hp" variable, then it will check to see if the "hp" is lower than or equal to 0. If it is then it will destroy the player object. Please note that other used in this way only works in the collision event and that the other instance must have the variable being checked or else an error will be thrown. However you can assign values to variables, or even create new ones, using other in the collision event too, like this:

  1. other.mana += 10; //add ten to the other instance "mana" variable
  2. other.hit = true; //set the other instance variable "hit" to true, creating it if the variable doesn't already exist

all(-3): All instances currently active in the room.指代所有在场景中(room)中活动着的实例

This keyword is used to tell GameMaker Studio 2 that a function is to be applied, or to check, all active instances within a room (deactivated instances will not be checked or accessed). You cannot use all to access or set variables in other instances using the point method (see here), but you can use it when calling with(), for example:
此关键字用于在应用某个功能或检查时,告诉GameMaker Studio 2房间中的所有活动实例(禁用的实例将不被检查或访问)。 您不能使用all来访问或使用指针方法在其他实例中设置变量(请参阅此处),但是可以在调用with()时使用它,例如:

  1. with (all)
  2. {
  3.  speed = 0;
  4.  }

The above code will set the speed of all instances in the room to 0. You can also use all within functions to target or check all instances in the room for example:

  1. inst = instance_position(mouse_x, mouse_y, all);
  2. //Check a point for any active instance in the room 访问房间中任何活动实例的位置
  3. if collision_line(x, y, mouse_x, mouse_y, all, false, true)
  4. {
  5. }
  6. //Check all instances for a collision along a line
  7.  访问沿一条线的碰撞上的所有实例
  8. mp_grid_add_instances(grid, all, false);
  9. //Add all instances in the room into a motion planning grid 将房间中的所有实例添加到动态的规划网格

all is a very useful keyword and can be used in numerous situations within your code and actions, often cutting down on the amount of code you need to write to achieve a desired effect.[以达预期的效果]

noone: No instance at all.没有任何实例

It may seem odd, but many times while programming your games will you find the need to check if there are no instances found at a location, or in a collision etc... In those cases you would use this keyword to check for nothing, something like this:

  1. if instance_nearest(x, y, obj_enemy) != noone
  2. {
  3.  //do something as there is an enemy instance near
  4. }

In this example, the function instance_nearest() will return either noone or the unique ID of the nearest found instance. Basically, any time that you need to check for an instance, you can expect to get either noone or a unique instance ID returned

在这个例子中,函数instance_nearest()将返回没有实例或找到的最近实例的唯一ID。 基本上,任何时候您来确认实例是否存在,您都可以预计获得结果:要么返回不存在“noone”或返回找到唯一的实例ID。


顺子 2017-07-07



加入 indienova

  • 建立个人/工作室档案
  • 建立开发中的游戏档案
  • 关注个人/工作室动态
  • 寻找合作伙伴共同开发
  • 寻求线上发行
  • 更多服务……