组件:发电机 =================== 此组件由[[item:generator_upgrade:zh|发电机升级]]提供给机器人使用。发电机的内部有物品栏,可以存储一个燃料物品堆。此API可用于与此物品栏交互。 组件名:`generator`。 回调函数: - `count(): number` 当前发电机内部的燃料物品数。 \\ - `insert([count: number]): boolean[, string]` 从当前选定的物品栏槽位中转移燃料物品到发电机的物品栏,至多指定数量个。若至少有一个物品被移动到发电机的物品栏中则返回`true`,其他情况下返回`false`以及一条报错信息。 可能的报错信息有: `selected slot does not contain fuel`(所选槽位没有燃料)代表着选中的槽位中没有可燃烧的物品。 `different fuel type already queued`(队列中已有不同种燃料)代表发电机中已经有另一种燃料物品存在 `queue is full`(队列已满)代表着发电机中已有64个此种物品 \\ - `remove([count: number]): boolean` 从发电机物品栏中移除燃料物品,并放置到当前选定的物品栏槽位或其后首个空闲槽位中,至多指定数量个。若至少有一个物品被从发电机的物品栏中移除则返回`true`,其他情况下返回`false`。 使用例: ```lua local component = require("component") --假设发电机刚被安装到机器人上,且选定槽位有一组煤炭: local g = component.generator --获取首选发电机组件 print(g.count()) -- 0 g.insert(1) -- true print(g.count()) --返回0,因为物品立刻被消耗,发电机现在开始运行 g.insert(5) print(g.count()) -- 5 g.remove() --从发电机中移除所有物品 print(g.count()) -- 0 g.insert() --将所有物品添加到发电机中 print(g.count()) -- 63(去掉一开始消耗掉的一个) ``` 目录 ---- {{page>component:contents:zh&noheader&noeditbutton&nouser&nofooter}}