你可以幫助我們來翻譯此條目,但請勿使用機器翻譯。
你可以在這裏找到部分內容的原文,並幫助我們完善翻譯。
注意:基岩版開發者文件中可能存在低質量翻譯內容,在翻譯基岩版開發者文件時請檢查已翻譯內容的翻譯質量,並對照英文頁面進行修正。
Template:Bedrock Edition Developer Documentation 這是基岩版1.12.0的MoLang文件。
版本:1.12.0.28
為什麼要創造Molang?
MoLang是一種簡單的基於表達式的語言,為實時且快速地計算數值而設計。它的設計重點是在JavaScript無法大規模執行的需要更高效能的系統中啟用類似腳本的功能。我們需要這些底層系統中的腳本功能來支持使用者修改模組,自訂實體,繪製和動畫。
詞彙結構
MoLang語言結構主要基於簡單的C樣式語法,專於處理數學表達式。對於簡要情況,腳本由一個表達式組成;而在需要中間值,或有助於減少計算時間的情況下,則可由多個表達式組成。
所有情況下,腳本中最後一個表達式的值會提供該腳本的值。在多表達式腳本中,除最後一個表達式外,所有表達式都必須為變數分配一個值。最後一個表達式也可分配,然而並不需要,因為它的值被預設用作回傳值。
用途
- 值類型是被具體指定的,如果沒有指定,則預設為數值。
- 所有的數值都是浮點數。
- 對於false或true,布林值將分別轉換為浮點值0.0或1.0。
- 對於布林測試,等於0.0的浮點值為假,不等於0.0的值為真。
- For array indices, floats are C-style cast to ints, and clamped at zero for negative values or wrapped by the array size for large values.
- 其他支持的類型是有意義的紋理、材質和幾何圖形(比如繪製控制器).
- 錯誤回傳值為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" "數值常量值" "( )" "圓括號用於控制運算表達式的先後順序" "[ ]" "方括號用於陣列存取" "query.function_name" "訪問實體的屬性" "math.function_name" "各種數學方法(見下表)" "temp.variable_name" "在目前表達式中儲存一個臨時變數" "variable.variable_name" "在目前實體身上儲存一個值供未來使用" "geometry.texture_name" "在實體定義中指向一個貼圖" "material.texture_name" "在實體定義中指向一個貼圖" "texture.texture_name" "在實體定義中指向一個貼圖" "! && || < <= >= > == !=" "邏輯運算符" "* / + -" "基本的數學運算符" "test ? if true : if false" "條件運算符(三元表達式)" "this" "在執行這個表達式前的值(上下文相關)" "return" "在複雜的表達式中,這個關鍵字會在返回後面的表達式的值後停止運算"
數學函數
- 參數
函數 描述 "math.abs(value)" value的絕對值 "math.sin(value)" value的正弦值 "math.cos(value)" value的餘弦值 "math.exp(value)" value以e為底數的指數函數 "math.ln(value)" value以e為底數的對數函數 "math.pow(base, exponent)" 返回base的exponent次冪 "math.sqrt(value)" value的平方根 "math.random(low, high)" 在最小值到最大值之間的隨機數 "math.ceil(value)" 數字的向上取整 "math.round(value)" 數字四捨五入取整 "math.trunc(value)" 截短法取整,這種方式在處理負數時是向上取整 "math.floor(value)" 向下取整 "math.mod(value, denominator)" value 除以 denominator後的餘數 "math.min(A, B)" 返回A和B中的最小值 "math.max(A, B)" 返回A和B中的最大值 "math.clamp(value, min, max)" 把value限定在最小值和最大值之間 "math.lerp(start, end, 0_to_1)" 在start和end之間根據0~1取中間值,即start+(end-start)*0_to_1 "math.lerprotate(start, end, 0_to_1)" 作為角度,在start和end之間根據0~1取中間值,360度時會有跨越
類型、值和變數
通常,所有表達式值都是浮點數。在繪製控制器中,一些表達式根據上下文生成紋理或材質。所有陣列索引表達式都被處理為浮點數,並在對陣列執行最終查找時強制轉換為整數。正數的陣列下標會被限制到陣列的長度,負數的陣列小標會被轉換成0.
簡單的表達式 vs 複雜的表達式
A simple expression is a single statement, the value of which is returned to the system that evaluated the expression. e.g.:
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.
Render Controllers
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 materials (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 be 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]"
資源部分
- 幾何
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 geometry can be arbitrarily complex using any number of bones and polygons.
- 材料
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, it 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.
查詢功能
查詢函數是布林表達式,允許您查詢引擎在不同情況下擁有的值。它們可以用在MoLang的表達中。對於控制位置,紋理,動畫等。
例如:
"position": [ 0.0, "query.is_baby ? -8.0 : 0.0", "query.is_baby ? 4.0 : 0.0" ]
實體查詢清單
名稱 注釋 query.all_animations_finished 僅在動畫控制器中有效。如果目前動畫控制器狀態下的所有動畫至少播放了一次,則返回1.0,否則返回0.0。 query.anim_time 返回自目前動畫開始以來的時間(以秒為單位),如果未在動畫中調用則返回0.0 query.any_animation_finished 僅在動畫控制器中有效。如果目前動畫控制器狀態中的任何動畫至少播放了一次,則返回1.0,否則返回0.0。 query.armor_color_slot 將護甲欄編號作為參數,並返回請求的護甲欄中護甲的顏色 query.armor_material_slot 將護甲欄編號作為參數,並返回請求的護甲欄中護甲的類型 query.armor_texture_slot 將護甲欄編號作為參數,並返回請求的護甲欄中護甲的紋理類型 query.blocking Returns 1.0 if the entity is blocking, else it returns 0.0 query.body_y_rotation Returns the body yaw rotation if called on an actor, else it returns 0.0 query.can_climb 如果實體可以爬升,則返回1.0,否則返回0.0 query.can_fly 如果實體可以飛行,則返回1.0,否則返回0.0 query.can_power_jump Returns 1.0 if the entity can power jump, else it returns 0.0 query.can_swim 如果實體可以游泳則返回1.0,否則返回0.0 query.can_walk 如果實體可以行走,則返回1.0,否則返回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 返回自上一幀以來的時間(以秒為單位) query.frame_alpha Returns the ratio (from 0 to 1) of how much between AI ticks this frame is being rendered query.ground_speed 返回實體的地面速度,以米/秒為單位 query.has_armor_slot 將護甲欄編號作為參數,如果實體在請求的護甲欄中有護甲,則返回1.0,否則返回0.0 query.has_collision 如果實體有碰撞檢測,則返回1.0,否則返回0.0 query.has_gravity 如果實體受重力影響,則返回1.0,否則返回0.0 query.has_owner 如果實體有所有者ID,則返回true,否則返回false query.has_rider Returns 1.0 if the entity has a rider, else it returns 0.0 query.has_target 如果實體有它的目標,則返回1.0,否則返回0.0 query.head_roll_angle 返回狼頭部的側傾角 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 如果可行,則返回該實體剩餘的無敵刻,否則返回0.0 query.is_angry 如果實體生氣,則返回1.0,否則返回0.0 query.is_avoiding_mobs 如果實體正在逃離某實體,則返回1.0,否則返回0.0 query.is_baby 如果實體是幼兒,則返回1.0,否則返回0.0 query.is_breathing 如果實體正在呼吸,則返回1.0,否則返回0.0 query.is_bribed Returns 1.0 if the entity has been bribed, else it returns 0.0 query.is_carrying_block 如果實體攜帶方塊,則返回1.0,否則返回0.0 query.is_casting 如果實體正在轉換,則返回1.0,否則返回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 如果實體附有儲物箱,則返回1.0,否則返回0.0 query.is_critical Returns 1.0 if the entity is critical, else it returns 0.0 query.is_dancing 如果實體在跳舞,則返回1.0,否則返回0.0 query.is_delayed_attacking returns 1.0 if the entity is attacking using the delayed attack, else it returns 0.0 query.is_eating 如果實體正在食用食物,則返回1.0,否則返回0.0 query.is_elder 如果實體是成年的,則返回1.0,否則返回0.0 query.is_enchanted 如果實體已附魔,則返回1.0,否則返回0.0 query.is_fire_immune 如果實體免疫火焰傷害,則返回1.0,否則返回0.0 query.is_first_person Returns 1.0 if the entity is being rendered in first person mode, else it returns 0.0 query.is_gliding 如果實體滑動,則返回1.0,否則返回0.0 query.is_grazing 如果實體在放牧,則返回1.0;否則,則返回0.0 query.is_idling 如果實體閒置,則返回1.0,否則返回0.0 query.is_ignited 如果實體被點燃,則返回1.0,否則返回0.0 query.is_illager_captain 如果實體是突襲隊長,則返回1.0,否則返回0.0 query.is_in_love 如果實體戀愛了,則返回1.0,否則返回0.0 query.is_in_water 如果實體在水中,則返回1.0,否則返回0.0 query.is_in_water_or_rain 如果實體在水或雨中,則返回1.0,否則返回0.0 query.is_interested 如果實體感興趣,則返回1.0,否則返回0.0 query.is_invisible 如果實體已隱形,則返回1.0,否則返回0.0 query.is_jumping 如果實體在跳躍,則返回1.0,否則返回0.0 query.is_laying_down 如果實體躺下,則返回1.0,否則返回0.0 query.is_laying_egg 如果實體正在下蛋,則返回1.0,否則返回0.0 query.is_leashed 如果實體被拴住,則返回1.0,否則返回0.0 query.is_lingering 如果實體持續存在,則返回1.0,否則返回0.0 query.is_moving 如果實體正在移動,則返回1.0,否則返回0.0 query.is_on_ground 如果實體在地面上,則返回1.0,否則返回0.0 query.is_onfire 如果實體著火,則返回1.0,否則返回0.0 query.is_orphaned 如果實體是孤兒,則返回1.0,否則返回0.0 query.is_powered 如果實體已充能(如苦力怕),則返回1.0,否則返回0.0 query.is_pregnant 如果實體已懷孕,則返回1.0,否則返回0.0 query.is_resting 如果實體處於靜止狀態,則返回1.0,否則返回0.0 query.is_riding 如果實體在騎行,則返回1.0,否則返回0.0 query.is_roaring 如果實體目前正在咆哮,則返回1.0,否則返回0.0 query.is_rolling 如果實體正在捲動,則返回1.0,否則返回0.0 query.is_saddled 如果實體有鞍,則返回1.0,否則返回0.0 query.is_scared 如果該實體害怕,則返回1.0,否則返回0.0 query.is_shaking 如果實體正在發抖,則返回1.0,否則返回0.0 query.is_shaking_wetness 如果實體正在甩水,則返回true query.is_sheared 如果實體能夠被剪下並被剪下,則返回1.0,否則返回0.0 query.is_shield_powered 如果該實體有處於防禦狀態的盾,則返回1.0,否則返回0.0 query.is_silent 如果實體是靜默的,返回1.0,否則返回0.0 query.is_sitting 如果實體坐著,則返回1.0,否則返回0.0 query.is_sleeping 如果實體正在睡眠,則返回1.0,否則返回0.0 query.is_sneaking 如果實體正在潛行,則返回1.0,否則返回0.0 query.is_sneezing 如果實體在打噴嚏,則返回1.0,否則返回0.0 query.is_sprinting 如果實體正在衝刺,則返回1.0,否則返回0.0 query.is_stackable 如果實體是可堆疊的,則返回1.0,否則返回0.0 query.is_standing 如果實體站立則返回1.0,否則返回0.0 query.is_stunned 如果該實體目前處於眩暈狀態,則返回1.0,否則返回0.0 query.is_swimming 如果實體正在游泳,則返回1.0,否則返回0.0 query.is_tamed 如果已馴服實體,則返回1.0,否則返回0.0 query.is_transforming 如果實體正在變身,則返回1.0,否則返回0.0 query.is_using_item 如果實體正在使用物品,則返回1.0,否則返回0.0 query.is_wall_climbing 如果實體正在爬牆,則返回1.0,否則返回0.0 query.item_in_use_duration Returns the amount of time an item has been in use in seconds up to the maximum duration, else 0.0 if it doesn't make sense query.item_max_use_duration 返回該物品可以使用的最長時間,否則返回0.0。 query.item_remaining_use_duration 返回該物品可以使用的剩餘時間(以秒為單位),否則返回0.0。 query.key_frame_lerp_time 返回上一個和下一個關鍵幀之間的比值 query.lie_amount 返回實體的躺下次數 query.life_span 返回實體的總存活時間,如果實體永久存在,則返回0.0 query.life_time 返回自目前動畫開始以來的時間(以秒為單位),否則返回0.0(如果未在動畫中調用) query.log debug log a value query.mark_variant Returns the entity's mark variant query.max_trade_tier 如果有意義,則返回實體的最大交易階段,否則返回0.0 query.model_scale 返回目前實體的比例 query.modified_distance_moved 返回實體在水平方向上移動的總米數(自從上次載入以來,不一定是最初建立實體以來) 該狀態由狀態標記(例如is_baby或on_fire)修改
query.modified_move_speed 返回由狀態標誌(例如is_baby或on_fire)修改的實體的目前行走速度 query.overlay_alpha 請勿使用——此功能已棄用,將被刪除 query.previous_squish_value 返回目前實體的前一個壓縮值,如果沒有意義,則返回0.0 query.roll_counter 返回實體的捲動計數 query.shake_angle 返回狼實體的搖動角度 query.sit_amount 返回實體的坐下計數 query.skin_id 返回實體的外觀ID query.sneeze_counter 返回實體的打噴嚏計數 query.spellcolor.r query.spellcolor.g
query.spellcolor.b
如果可行,則返回目前實體拼寫顏色的紅/綠/藍色通道,否則返回0.0 query.standing_scale Returns the scale of how standing up the entity is query.swell_amount 返回實體的膨脹程度(漲水程度) query.swelling_dir Returns the swelling direction of the entity if it makes sense, else it returns 0.0 query.tail_angle 返回狼實體尾巴的角度,否則返回0.0 query.target_x_rotation 如果有意義,則返回瞄準實體目前指向所需轉動的x軸角度,否則返回0.0 query.target_y_rotation 如果有意義,則返回瞄準實體目前指向所需轉動的y軸角度,否則返回0.0 query.time_stamp Returns the current time stamp of the level query.trade_experience 如果有意義,則返回該實體的目前交易經驗,否則返回0.0 query.trade_tier 如果有意義,則返回實體的貿易階段,否則返回0.0 query.unhappy_counter 返回實體的不滿意程度 query.variant Returns the entity's variant index query.wing_flap_position 返回實體的鞘翅位置,如果沒有意義,則返回0.0 query.wing_flap_speed 返回實體的鞘翅速度,如果沒有意義,返回0.0 query.yaw_speed 返回實體轉向的程度數字