切换语言: English

Tutorial:Script interfaces/zh: Difference between revisions

From Official Factorio Wiki
Jump to navigation Jump to search
Cardinal (talk | contribs)
Init translation.
 
Cardinal (talk | contribs)
m Fixed category.
 
Line 95: Line 95:
* selected_prototype :: [https://lua-api.factorio.com/latest/concepts/SelectedPrototypeData.html SelectedPrototypeData](可选):当 [https://lua-api.factorio.com/latest/prototypes/CustomInputPrototype.html#include_selected_prototype include_selected_prototype] 的值为“true”时显示选中原型的信息。
* selected_prototype :: [https://lua-api.factorio.com/latest/concepts/SelectedPrototypeData.html SelectedPrototypeData](可选):当 [https://lua-api.factorio.com/latest/prototypes/CustomInputPrototype.html#include_selected_prototype include_selected_prototype] 的值为“true”时显示选中原型的信息。


[[Category:Modding]]
{{C|Modding}}

Latest revision as of 03:44, 9 December 2025

脚本接口(Lua远程调试)

脚本接口允许在同时运行的脚本之间直接进行通信,此功能通过定义一个具有给定函数的公共接口来完成。所有关于接口通信的代码都位于命名空间remote 中。更多信息请参阅官方API文档。(基于Lua远程通讯模式)

定义接口

接口定义如下:

-- 假定 interface_name 和 table_of_functions 已被定义
remote.add_interface(interface_name, table_of_functions)

-- 可以在调用过程内部定义 name 和 table 
remote.add_interface("my_interface", {
    my_getter = function()
        -- 可以从脚本中返回一个或多个变量
        return "foo"
    end,
  
    -- 值的类型只能是基本类型或(嵌套)表
    my_setter = function(foo, bar)
        global.foo = foo
        global.bar = bar
    end
})

接口函数不接受函数指针或闭包函数。基本类型,Lua对象和数据表均可正常工作。

调用接口函数

接口可用于从不同脚本间调用函数。限制:带有元素的数据表在传递给远程模组时,其中的元素不会被保留(但被传递的游戏对象会被完整保留)。远程模组接收到的数据表是原始表的副本。两个模组不能通过远程调用“共享”同一个数据表。

示例(在上述不同脚本中):

-- 调用上述脚本中的 my_interface.my_getter 并输出返回的值
print(remote.call("my_interface", "my_getter"))
-- 远程调用获取接口名称、函数名称和变量参数
remote.call("my_interface", "my_setter", 5, {bar=baz})

发现接口

脚本可以通过 remote.interfaces 数据表检查所需的接口及其功能。这是一个按接口名称索引的数据表,其中的值是特定接口的函数集。

示例:

-- 检查 "my_interface" 接口是否存在,以及该接口是否包含 "my_getter" 函数
if remote.interfaces.my_interface and remote.interfaces.my_interface.my_getter then
    -- 该函数可以安全地远程调用
end

自定义输入

在脚本中也可以创建按键绑定。首先需要在数据阶段定义案件绑定,详情可参见 CustomInputPrototype(官方文档,英文):

local button={
    type = "custom-input",
    name = "my-custom-input",
    key_sequence = "SHIFT + G",
    consuming = "none"
}
data:extend{button}

其中,“consuming”的可用选项有:

  • none:若未定义,则使用默认值
  • game-only:使用相同的按键序列阻止游戏输入,但允许其它自定义输入使用相同的按键序列进行触发。
参见 ConsumingType(官方文档,英文)

区域定义:

[controls] -- “游戏菜单 -> 按键 -> 模组”菜单显示的文字
my-custom-input=Potato controls

使用 __CONTROL__my-custom-input__ 获取其它区域中的按键绑定,例如

this-is-some-locale=Potato controls are bound to "__CONTROL__my-custom-input__"

在游戏中显示 Potato controls are bound to "SHIFT + G"


然后即可通过订阅对应自定义输入名称的事件,在运行时进行调用:

script.on_event("my-custom-input", function(event)
    game.print("Ran on tick: " .. tostring(event.tick))
end)

事件包含以下内容:

  • player_index :: uint(无符号整数)
  • tick :: uint(无符号整数)
  • input_name :: string: 自定义输入的名称。
  • cursor_position :: MapPosition:输入被激活时鼠标指针的位置。
  • cursor_direction :: defines.direction (可选):当鼠标指针包含可旋转实体时的指针方向。
  • cursor_display_location :: GuiLocation:自定义输入被激活时,显示鼠标指针位置。
  • selected_prototype :: SelectedPrototypeData(可选):当 include_selected_prototype 的值为“true”时显示选中原型的信息。