本站文本內容除另有聲明外,均在知識共享 署名-非商業性使用-相同方式共享 3.0 協議下提供。(詳情…本站文本內容除另有聲明外,均在知識共享 署名-非商業性使用-相同方式共享 3.0 協議下提供。(詳情…中文Minecraft Wiki是完全公開的。歡迎持續參與翻譯工作中文Minecraft Wiki是完全公開的。歡迎持續參與翻譯工作Minecraft中文Wiki微博正在更新!或許有興趣去看看Minecraft中文Wiki微博正在更新!或許有興趣去看看翻譯或創建頁面之前,不妨看看譯名標準化Wiki條例頁面。翻譯或創建頁面之前,不妨看看譯名標準化Wiki條例頁面。想與其他用戶進行編輯上的溝通?社區專頁正是為此創建的。想與其他用戶進行編輯上的溝通?社區專頁正是為此創建的。需要管理員的協助?在管理員告示板留言也許可以幫到您。需要管理員的協助?在管理員告示板留言也許可以幫到您。Minecraft發布即將迎來十周年!快到官方主題專頁和我們一起慶祝吧!Minecraft發布即將迎來十周年!快到官方主題專頁和我們一起慶祝吧!Minecraft遊戲本體開始促銷了,快前往官網購買吧!Minecraft遊戲本體開始促銷了,快前往官網購買吧!

基岩版beta MoLang文档

来自Minecraft Wiki
跳转至: 导航搜索
Mclogo.svg


Ic translate.svg
此页面的(部分)内容需要被翻译。
你可以帮助我们来翻译此页面

版本:1.10.0.3[编辑 | 编辑源代码]

这是 Minecraft 发布的的一个测试版的文档。这个版本中的新特性、组件和功能都不是最终版本,并且可能在最终版本发布之前有所更改而不做任何提醒。 如果你的附加包在测试结束之后出现运行错误,请务必重新核对本文档。因为在这个测试版中所写的资源包和行为包都不确保能够在最终版本中运行。

为什么会有Molang?[编辑 | 编辑源代码]

Molang是一种基于表达式的简单语言,它被设计用于在运行时快速计算值。它的重点仅仅是在JavaScript不具备大规模性能的高性能系统中实现类似脚本的功能。 我们需要这些低级系统中的脚本功能,以支持最终用户修改功能,自定义实体,渲染和动画。



语法结构[编辑 | 编辑源代码]

语言结构主要基于简单的C风格语法,侧重于处理数学表达式。一般情况下,脚本由一个表达式组成,或者可以由需要中间值的几个表达式组成,有助于减少计算时间。

在所有情况下,脚本中最后一个表达式的值都提供了脚本的值。 在多表达式脚本中,除最后一个表达式之外的所有表达式都必须为变量赋值。 最后一个表达式也可以赋值给变量,但不需要,因为它的值被假定用作返回值。

[编辑 | 编辑源代码]

- 值的类型是具体指定的,否则为数值。
- 所有数值都是浮点数。
- 布尔值(如actor标志)分别转换为0.0或1.0的浮点值(值为false或true)。
- 对于布尔值,等于0.0的浮点值为false,不等于0.0的值为true。
- 对于数组索引,浮点数是C风格的强制转换为int,负值的浮点数被限制为零,大值的浮点数被数组大小所包围。
- 其他受支持的类型是纹理、材质和几何图形(如渲染控制器)。
- 发生错误通常返回值0.0。

变量[编辑 | 编辑源代码]

变量可归属于以下的域:

参数
范围 例子
temp 当前表达式 temp.foo = math.sin(query.anim_time); return temp.foo * temp.foo;
variable 可读写变量,通常与当前实体有关 variable.my_saved_var = variable.my_saved_var + 1;
query 只读变量,通常与当前实体有关 query.is_baby
geometry 当前渲染控制器 "geometry": "array.geos[query.is_sheared]"
material 当前渲染控制器 "materials": [ { "*": "material.default" }, { "leg*": "material.legs" } ]
texture 当前渲染控制器 "textures": ["array.skins[query.is_saddled]"]

关键字[编辑 | 编辑源代码]

下面列举出来的标识为将来使用保留

参数
关键字 描述
`float` `数字常量`
`( )` `Parentheses for expression term evaluation control`
` `Brackets for array access`
`query.function_name` `Access to an entity's properties`
`math.function_name` `Various math functions (see below)`
`temp.variable_name` `Store an intermediate value for the duration of the current expression`
`variable.variable_name` `Store a value on the attached entity for later use`
`geometry.texture_name` `A reference to a texture named in the entity definition`
`material.texture_name` `A reference to a texture named in the entity definition`
`texture.texture_name` `A reference to a texture named in the entity definition`
`! && < <= >= > == !=` `逻辑运算符`
`* / + -` `基础数学运算符`
`test ? if true : if false` `条件运算符`
`this` `在执行脚本前的当前变量(上下文敏感)`
`return` `For complex expressions, this evaluates the following statement and stops execution of the script, returns the value computed`

数学函数[编辑 | 编辑源代码]

`

Parameters
Function Description
`math.abs(value)` `值的绝对值`
`math.sin(value)` `根据角度得出的正弦值`
`math.cos(value)` `根据角度得出的余弦值`
`math.exp(value)` `Calculates e to the value'th power`
`math.ln(value)` `Natural logarithm of value`
`math.pow(base, exponent)` `Elevates base to the exponent'th power`
`math.sqrt(value)` `Square root of value`
`math.random(low, high)` `Random value between low and high inclusive`
`math.ceil(value)` `Round value up to nearest integral number`
`math.round(value)` `Round value to nearest integral number`
`math.trunc(value)` `Round value towards zero`
`math.floor(value)` `Round value down to nearest integral number`
`math.mod(value, denominator)` `Return the remainder of value / denominator`
`math.min(A, B)` `Return lowest value of A or B`
`math.max(A, B)` `Return highest value of A or B`
`math.clamp(value, min, max)` `Clamp value to between min and max inclusive`
`math.lerp(start, end, 0_to_1)` `Lerp from start to end via 0_to_1`
`math.lerprotate(start, end, 0_to_1)` `Lerp the shortest direction around a circle from start degrees to end degrees via 0_to_1`

类型,值和变量[编辑 | 编辑源代码]

In general, all expression values are floats. In render controllers, some expressions result in a texture or material depending on the context. All array index expressions are processed as floats, and cast to integers when performing the final lookup into the array. Positive array indices wrap by the size of the array. Negative array indices clamp to 0.


简单与复杂表达式[编辑 | 编辑源代码]

A simple expression is a single statement, the value of which is returned to the system that evaluated the expression. eg:

math.sin(query.anim_time * 1.23)


A complex expression is one with multiple statements, each ending in a ';'. Each statement is evaluated in order. In the current implementation, the last statement requires the use of the return keyword and defines the resulting value of the expression. eg:

temp.my_temp_var = Math.sin(query.anim_time * 1.23);
temp.my_other_temp_var = Math.cos(query.life_time + 2.0);
return temp.my_temp_var * temp.my_temp_var + temp.my_other_temp_var;


Note that in a simple expression, ';' is not allowed, whereas in a complex expression, each statement requires a ';' including the last.




域示例[编辑 | 编辑源代码]

实体定义脚本[编辑 | 编辑源代码]

In the definition file there is a section for pre-computing values. These are executed immediately before animation and render controllers are processed, and stored in the entity. The purpose is to pre-compute any expensive and complex values you may want to reuse in your scripts, long-living index variable updates, or generally any one-off computation per render tick.

"scripts": {
    "pre_animation": [
      "variable.my_constant = (Math.cos(query.modified_distance_moved * 38.17) * query.modified_move_speed;",
      "variable.my_constant2 = Math.exp(1.5);",
    ]
  },


动画和动画控制器文件[编辑 | 编辑源代码]

These are always numerical operations to control which animations are playing and how to animate bones. "variable.variable_name" and "query.function_name" refers to the entity currently being rendered. They have access to everything in the language except material, texture, and geometry types.


渲染控制器[编辑 | 编辑源代码]

There are a few different kinds of expressions here, where context implies what is allowed. As with animations, the entity accessors refer to the current entity, however depending on the context one also has access to materials, textures, and geometries. There are two sections in a render controller:
-Array definitions (optional)
-Resource usage (required)
The array definition section allows you to create arrays of resources by resource type if you so desire. These can then be referenced in the resource usage section.

数组表达式

For each of the three resource types (materials, textures, and geometry), you can define an array of resources. The name of the resource is the nice-name from the definition file. Using materials as an example:

"arrays":
{
  "materials": {
    "array.my_array_1": ["material.a", "material.b", "material.c"],
    "array.my_array_2" : ["material.d", "material.e"],
    "array.my_array_3" : ["array.my_array_1", "material.my_array_2"],
    "array.my_array_4" : ["array.my_array_2", "material.my_array_3"],
    "array.my_array_5" : ["array.my_array_1", "material.my_array_1", "material.my_array_4"],
    "array.my_array_6" : ["array.my_array_1", "material.f"],
    ...
  },

Note that all elements of an array must be of the same type. eg: a texture array must only contain textures.

An array can reference any combination of zero or more arrays (including duplicates if desired) and/or zero or more materails (again, including duplicates if you like), and you can have as many arrays as you like, each with as many elements as you like. If an array includes arrays in its members, they do not need to be the same length. When indexing into an array in the resource usage section, you use numerical expressions. If the resulting number is negative, it will use zero as the index. Any non - negative index will converted to an integer, and will wrap based on the size of the array:

index = max(0, expression_result) % array_size



资源表达式

A resource expression must return a single resource of a specific type depending on the context.
For example, in the "geometry" section, you must produce an expression that will result in a single geometry. Some examples:

Always use a specific geometry
"geometry": "geometry.my_geo"
Cycle through an array of geometries at a rate of one per second
"geometry": "array.my_geometries[query.anim_time]"
Pick a geo based on an entity flag
"geometry": "query.is_sheared ? geometry.sheared : geometry.woolly"
Use specific geo when sleeping, otherwise flip through an array based on a cosine curve, using index zero for almost half the time while the cosine curve is negative
"geometry": "query.is_sleeping ? geometry.my_sleeping_geo : array.my_geos[math.cos(query.anim_time * 12.3 + 41.9) * 10 + 0.6]"


Resource Sections

Geometry 

The geometry section specifies which geometry to use when rendering. As you can specify as many render controllers as you like in the definition file, a single render controller is only concerned with how to render a single geometry. Note that a geometry can be arbitrarily complex using any number of bones and polygons.


Materials 

The materials section specifies how to map what material to what bone of the geometry. A single material is mapped to a whole bone. Material expressions are evaluated in the order listed. The first part of each statement is the name of the model part to apply the material to, and the second part is the material to use. The model part name can use * for wild - card matching of characters. For example :

  "materials": [
      { "*": "Material.default" },
      { "TailA": "array.hair_colors[variable.hair_color]" },
      { "Mane": "array.hair_colors[variable.hair_color]" },
      { "*Saddle*": "variable.is_leather_saddle ? material.leather_saddle : material.iron_saddle" }
    ],

- This will start by applying Material.default to all model parts.
- Next, it will set the material on a model part named "TailA" to the result of the expression "Array.hairColors[variable.hair_color]". This will look up some previously created variable on the entity named hair_color and use that to index into a material array called "array.hair_colors" defined in this render controller. This will overwrite the Material.default material set in the line above.
- Third, it will look up the same material as the expression is identical, and apply it to the "Mane" model part.
- Lastly, if will find any model part starting with, ending with, or containing "Saddle" (case sensitive) and change its material to either material.leather_saddle or material.iron_saddle depending on the previously set entity variable variable.is_leather_saddle.






查询函数[编辑 | 编辑源代码]

Query Functions are boolean expressions that allow you to query for values owned by the engine under different circumstances. They can be used in MoLang expressionsUseful for controlling things like changing positions, textures, animations, etc if a mob is a baby. For example:

"position": [ 0.0, "query.is_baby ? -8.0 : 0.0", "query.is_baby ? 4.0 : 0.0" ]

实体查询列表[编辑 | 编辑源代码]

Name Description
query.all_animations_finished Only valid in an animation controller. Returns 1.0 if all animations in the current animation controller state have played through at least once, else it returns 0.0
query.anim_time Returns the time in seconds since the current animation started, else 0.0 if not called within an animation
query.any_animation_finished Only valid in an animation controller. Returns 1.0 if any animation in the current animation controller state has played through at least once, else it returns 0.0
query.armor_color_slot Takes the armor slot index as a parameter, and returns the color of the armor in the requested slot
query.armor_material_slot Takes the armor slot index as a parameter, and returns the armor material type in the requested armor slot
query.armor_texture_slot Takes the armor slot index as a parameter, and returns the texture type of the requested slot
query.blocking Returns 1.0 if the entity is blocking, else it returns 0.0
query.can_climb Returns 1.0 if the entity can climb, else it returns 0.0
query.can_fly Returns 1.0 if the entity can fly, else it returns 0.0
query.can_power_jump Returns 1.0 if the entity can power jump, else it returns 0.0
query.can_swim Returns 1.0 if the entity can swim, else it returns 0.0
query.can_walk Returns 1.0 if the entity can walk, else it returns 0.0
query.current_squish_value Returns the squish value for the current entity, or 0.0 if this doesn't make sense
query.delta_time Returns the time in seconds since the previous frame
query.frame_alpha Returns the ratio (from 0 to 1) of how much between AI ticks this frame is being rendered
query.ground_speed Returns the ground speed of the entity in metres/second
query.has_armor_slot Takes the armor slot index as a parameter, and returns 1.0 if the entity has armor in the requested slot, else it returns 0.0
query.has_collision Returns 1.0 if the entity has collisions enabled, else it returns 0.0
query.has_gravity Returns 1.0 if the entity is affected by gravity, else it returns 0.0
query.has_rider Returns 1.0 if the entity has a rider, else it returns 0.0
query.has_target Returns 1.0 if the entity has a target, else it returns 0.0
query.head_x_rotation Takes one argument as a parameter. Returns the nth head x rotation of the entity if it makes sense, else it returns 0.0
query.head_y_rotation Takes one argument as a parameter. Returns the nth head y rotation of the entity if it makes sense, else it returns 0.0
query.invulnerable_ticks Returns the number of ticks of invulnerability the entity has left if it makes sense, else it returns 0.0
query.is_angry Returns 1.0 if the entity is angry, else it returns 0.0
query.is_baby Returns 1.0 if the entity is a baby, else it returns 0.0
query.is_breathing Returns 1.0 if the entity is breathing, else it returns 0.0
query.is_bribed Returns 1.0 if the entity has been bribed, else it returns 0.0
query.is_carrying_block Returns 1.0 if the entity is carrying a block, else it returns 0.0
query.is_casting Returns 1.0 if the entity is casting, else it returns 0.0
query.is_charged Returns 1.0 if the entity is charged, else it returns 0.0
query.is_charging Returns 1.0 if the entity is charging, else it returns 0.0
query.is_chested Returns 1.0 if the entity has chests attached to it, else it returns 0.0
query.is_critical Returns 1.0 if the entity is critical, else it returns 0.0
query.is_dancing Returns 1.0 if the entity is dancing, else it returns 0.0
query.is_eating Returns 1.0 if the entity is eating, else it returns 0.0
query.is_elder Returns 1.0 if the entity is an elder version of it, else it returns 0.0
query.is_enchanted Returns 1.0 if the entity is enchanted, else it returns 0.0
query.is_fire_immune Returns 1.0 if the entity is immune to fire, else it returns 0.0
query.is_gliding Returns 1.0 if the entity is gliding, else it returns 0.0
query.is_grazing Returns 1.0 if the entity is grazing, or 0.0 if not
query.is_idling Returns 1.0 if the entity is idling, else it returns 0.0
query.is_ignited Returns 1.0 if the entity is ignited, else it returns 0.0
query.is_in_love Returns 1.0 if the entity is in love, else it returns 0.0
query.is_in_water Returns 1.0 if the entity is in water, else it returns 0.0
query.is_in_water_or_rain Returns 1.0 if the entity is in water or rain, else it returns 0.0
query.is_interested Returns 1.0 if the entity is interested, else it returns 0.0
query.is_invisible Returns 1.0 if the entity is invisible, else it returns 0.0
query.is_jumping Returns 1.0 if the entity is in water or rain, else it returns 0.0
query.is_laying_down Returns 1.0 if the entity is laying down, else it returns 0.0
query.is_laying_egg Returns 1.0 if the entity is laying an egg, else it returns 0.0
query.is_leashed Returns 1.0 if the entity is leashed to something, else it returns 0.0
query.is_lingering Returns 1.0 if the entity is lingering, else it returns 0.0
query.is_moving Returns 1.0 if the entity is moving, else it returns 0.0
query.is_on_ground Returns 1.0 if the entity is on the ground, else it returns 0.0
query.is_onfire Returns 1.0 if the entity is on fire, else it returns 0.0
query.is_orphaned Returns 1.0 if the entity is orphaned, else it returns 0.0
query.is_powered Returns 1.0 if the entity is powered, else it returns 0.0
query.is_pregnant Returns 1.0 if the entity is pregnant, else it returns 0.0
query.is_resting Returns 1.0 if the entity is resting, else it returns 0.0
query.is_riding Returns 1.0 if the entity is riding, else it returns 0.0
query.is_rolling Returns 1.0 if the entity is rolling, else it returns 0.0
query.is_saddled Returns 1.0 if the entity has a saddle, else it returns 0.0
query.is_scared Returns 1.0 if the entity is scared, else it returns 0.0
query.is_shaking Returns 1.0 if the entity is casting, else it returns 0.0
query.is_sheared Returns 1.0 if the entity is able to be sheared and is sheared, else it returns 0.0
query.is_shield_powered Returns 1.0f if the entity has an active powered shield if it makes sense, else it returns 0.0
query.is_silent Returns 1.0 if the entity is silent, else it returns 0.0
query.is_sitting Returns 1.0 if the entity is sitting, else it returns 0.0
query.is_sleeping Returns 1.0 if the entity is sleeping, else it returns 0.0
query.is_sneaking Returns 1.0 if the entity is sneaking, else it returns 0.0
query.is_sneezing Returns 1.0 if the entity is sneezing, else it returns 0.0
query.is_sprinting Returns 1.0 if the entity is sprinting, else it returns 0.0
query.is_standing Returns 1.0 if the entity is standing, else it returns 0.0
query.is_swimming Returns 1.0 if the entity is swimming, else it returns 0.0
query.is_tamed Returns 1.0 if the entity is tamed, else it returns 0.0
query.is_transforming Returns 1.0 if the entity is transforming, else it returns 0.0
query.is_using_item Returns 1.0 if the entity is using an item, else it returns 0.0
query.is_wall_climbing Returns 1.0 if the entity is climbing a wall, else it returns 0.0
query.key_frame_lerp_time Returns the ratio between the previous and next key frames
query.lie_amount Returns the lie down amount for the entity
query.life_time Returns the time in seconds since the current animation started, else 0.0 if not called within an animation
query.mark_variant Returns the entity's mark variant
query.max_trade_tier Returns the maximum trade tier of the entity if it makes sense, else it returns 0.0
query.model_scale Returns the scale of the current entity
query.modified_distance_moved Returns the total distance the entity has moved horizontally in metres (since the entity was last loaded, not necessarily since it was originally created) modified along the way by status flags such as is_baby or on_fire
query.modified_move_speed Returns the current walk speed of the entity modified by status flags such as is_baby or on_fire
query.overlay_alpha Do not use - this function is deprecated and will be removed
query.previous_squish_value Returns the previous squish value for the current entity, or 0.0 if this doesn't make sense
query.roll_counter Returns the roll counter of the entity
query.sit_amount Returns the current sit amount of the entity
query.sneeze_counter Returns the sneeze counter of the entity
query.spellcolor.b Returns the blue colour channel of the current entity spell colour if it makes sense, else it returns 0.0
query.spellcolor.g Returns the green colour channel of the current entity spell colour if it makes sense, else it returns 0.0
query.spellcolor.r Returns the red colour channel of the current entity spell colour if it makes sense, else it returns 0.0
query.standing_scale Returns the scale of how standing up the entity is
query.swell_amount Returns how swollen the entity is
query.swelling_dir Returns the swelling direction of the entity if it makes sense, else it returns 0.0
query.target_x_rotation Returns the x rotation required to aim at the entity's current target if it has one, else it returns 0.0
query.target_y_rotation Returns the y rotation required to aim at the entity's current target if it has one, else it returns 0.0
query.time_stamp Returns the current time stamp of the level
query.trade_tier Returns the trade tier of the entity if it makes sense, else it returns 0.0
query.unhappy_counter Returns how unhappy the entity is
query.variant Returns the entity's variant index
query.wing_flap_position Returns the wing flap position of the entity, or 0.0 if this doesn't make sense
query.wing_flap_speed Returns the wing flap speed of the entity, or 0.0 if this doesn't make sense
query.yaw_speed Returns the entity's yaw speed