1. Grid Terminal System

网格终端系统

  • 一、GridTerminalSystem,Docking, and Multipe Constructs
  • 二、网格终端系统和子类
  • 二、获取网格的方块
    • (一)、GetBlocks
    • (二)、GetBlocksOfType
      • 1. 自动推导
      • 2. 明确指定类型
      • 3. 获取指定类型和属性
    • (三)、SearchBlocksOfName
    • (四)、GetBlockWithName
    • (五)、GetBlockWithId
    • (六)、GetBlockGroups
      • 1. 获取所有groups
      • 2. 获取指定属性的Groups
    • (七)、GetBlockGroupWithName

一、GridTerminalSystem,Docking, and Multipe Constructs

默认情况下grid terminal system只是抓取它能访问的所有东西,无论它是否是你的飞船的一部分。从1.188版本开始,你将可以使用一个新的块方法,叫做IsSameConstructAs。这个方法将检查两个被比较的块是否属于同一个机械组,也就是由转子和活塞连接的任何一组网格。这个机械组被称为构造,通常是你的船。这个机械组被称为构造,通常是你的船。

请注意,你无法区分用合并块合并的网格,因为该块的目的是将两个网格永久地合并成一个。

List blocks = new List();
void Main()
{
    // Fill a list with all blocks in the current construct (ship, vehicle or station),
    // ignoring anything that might be docked to it. "Me" is the Program's reference to
    // the programmable block this script is running in.
    GridTerminalSystem.GetBlocksOfType(blocks, block => block.IsSameConstructAs(Me));
}

二、网格终端系统和子类

随着脚本复杂性的增加,您不可避免地需要将代码拆分为类。一旦你这样做了,你很快就会意识到你无法访问这些类中的程序内容。这是因为程序本身只是另一个类。然而,这一事实为您提供了解决此问题的方法。

在写自己的子类时,需要子类构造函数传入Program类的实例,并做为subclass的成员,以在自己的sub class中调用space engineer的脚本API。

public class SomeUsefulSubclass
{
    Program _program;

    public SomeUsefulSubclass(Program program) 
    {
        _program = program;
    }

    public void SomeUsefulMethod() 
    {
        var block = _program.GridTerminalSystem.GetBlockWithName("That Block I Want");
        // Do useful stuff with that block
    }
}

SomeUsefulSubclass _subclass;

public Program() 
{
    _subclass = new SomeUsefulSubclass(this);
}

public void Main() {
    _subclass.SomeUsefulMethod();
}

二、获取网格的方块

关键类GridTerminalystem保存了编程块所在网格的所有资源。通过该类的借口获取相关的方块,如:引擎,灯,门等;

(一)、GetBlocks

获取网格终端能够访问的所有可用方块。需要IMyTerminalBlock类型的参数。

List blocks = new List();
void Main()
{
    // Fill a list with all blocks in the system
    GridTerminalSystem.GetBlocks(blocks);
}

(二)、GetBlocksOfType

获取指定指定类型的所有方块,方块的类型可以通过传入的参数推导。
类型指定有两种方式

1. 自动推导

List lights = new List();
void Main()
{
    // Fill a list with all interior lights in the system
    GridTerminalSystem.GetBlocksOfType(lights);
}

Blocks的类型是从列表lights中推导出来的。

2. 明确指定类型

GridTerminalSystem.GetBlocksOfType(lights)。GetBlocksOfType的左右尖括号中指定了类型。

// Create a list containing all interior lights in the system, but store them
// in a generic list containing blocks of the IMyTerminalBlock type
List lights = new List();
GridTerminalSystem.GetBlocksOfType(lights);

3. 获取指定类型和属性

示例: 获取当前被打开的lights

List lights = new List();
void Main()
{
    // Fill a list with all interior lights that are currently on
    GridTerminalSystem.GetBlocksOfType(lights, light => light.Enabled);
}

(三)、SearchBlocksOfName

遍历所有的Blocks, 寻找名字中包含给定字符串Blocks。也提供了使用收集谓词的能力,就像GetBlocksOfType。不幸的是,这个方法只能接受一个基本的IMyTerminalBlock类型的目标列表。
这个方法的实现是相当低效的。你可以用更好的方式做这个方法能做的一切,只需使用GetBlocksOfType的过滤器。

List lights = new List();
void Main()
{
    // Fill a list with all lights in the system whose name contains the text "no"
    GridTerminalSystem.SearchBlocksOfName("no", lights, light => light is IMyInteriorLight);
}

(四)、GetBlockWithName

允许你检索具有特定名称的单个块。请注意,该名称必须准确,区分大小写,并包括任何空格。

// Find a block of a specific name, then check if it's a timer block. If it is, store it in the
// timer variable, otherwise store null there.
IMyTimerBlock timer = GridTerminalSystem.GetBlockWithName("Timer Block") as IMyTimerBlock;

(五)、GetBlockWithId

允许你通过它的EntityId来检索一个块。这是一个由游戏赋予区块的唯一ID,即使你重新命名区块,它也会保持不变。

(六)、GetBlockGroups

获取一个BlockGroups的列表,可以选择通过给定的收集谓词进行过滤。

1. 获取所有groups

List groups = new List();
void Main()
{
    // Fill a list with all the block groups in the system
    GridTerminalSystem.GetBlockGroups(groups);
}

2. 获取指定属性的Groups

List groups = new List();
void Main()
{
    // Fill a list with all the block groups whose name contains the text "no"
    GridTerminalSystem.GetBlockGroups(groups, group => group.Name.Contains("no"));
}

(七)、GetBlockGroupWithName

按名称返回一个Block组。注意,名称必须准确,区分大小写,包括任意空格。

// Retrieve the block group named "Interesting Blocks"
IMyBlockGroup group;
group = GridTerminalSystem.GetBlockGroupWithName("Interesting Blocks");

你可能感兴趣的:(游戏,c#,游戏,太空工程师,spaceEngineer)