ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、视频、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
## 方块的概念 游戏中的方块,实际上都是实例化出来的类。所以你会发现相同类型的方块有相似的功能。 在SE中,方块是一级一级继承下来的 最基础的是物理类,它定义了一些object物体共有的属性,比如坐标、体积、速度等 然后是方块类,方块类继承物理类,也有物理属性。同时还多了一些属性,例如可以破坏,可以放置。 然后是功能块类,功能块类又继承了方块类,它也有物理属性,也有可放置可破坏的属性,同时还可以在K键菜单(K表)中访问,有名字、是否显示在HUD上之类的属性。**[IMyTerminalBlock](https://gitee.com/zzc1996/space-engineers-api/blob/master/SE-2/Sandbox.Common/Sandbox/ModAPI/Ingame/IMyTerminalBlock.cs)正是处在这一级** 再往下是功能更多的工作类,它继承了上方的功能类,这个类叫 **[IMyFunctionBlock](https://gitee.com/zzc1996/space-engineers-api/blob/master/SE-2/Sandbox.Common/Sandbox/ModAPI/Ingame/IMyFunctionalBlock.cs),其中提供了一个bool Enable属性可以用来判断是否开启。** 再往下就是不同的具体方块了,例如[IMyTextPanel](https://gitee.com/zzc1996/space-engineers-api/blob/master/SE-2/Sandbox.Common/Sandbox/ModAPI/Ingame/IMyTextPanel.cs)、[IMyThrust](https://gitee.com/zzc1996/space-engineers-api/blob/master/SE-2/Sandbox.Common/Sandbox/ModAPI/Ingame/IMyThrust.cs)。它们包含了自己特有的方法。 >[danger] 总结 >所有的方块都是由最基础的类一级一级继承下来的,所有的方块都有共通的成员变量和成员方法,也有自己特殊的成员变量和成员方法。 >[info] **注意** >在编程块中,我们可以控制几乎所有的其他类型方块。但首先要获取它,K社给了一些特定的方法去获取它,具体操作我会单独开一章来讲。 >你需要知道的是,每个方块其实都是一个类。 举个例子吧 ~~~ IMyTerminalBlock Block; //这就是一个类,而且是所有功能性方块的基类,所有的方块类型都继承了它。 IMyTextPanel LCD; //这是文本面板,就是LCD IMyThrust Thrust; //这个是推进器 ~~~ 是不是很神奇,前面讲的内容一个没浪费,全用上了。编程块里的代码怎么控制方块的? 例如推进器,首先你知道了它是 IMyThrust 类的,然后去查它的[API](https://gitee.com/zzc1996/space-engineers-api/tree/master/SE-2/Sandbox.Common/Sandbox/ModAPI/Ingame) ~~~ namespace Sandbox.ModAPI.Ingame { using System; using VRage.Game.ModAPI.Ingame; using VRageMath; //下面这一行是不是很眼熟,它声明了IMyThrust类是继承了IMyTerminalBlock、IMyCubeBlock、IMyEntity这些类。 //如果有兴趣,你也可以在API文档中查看这些父类所包含的成员属性或方法 public interface IMyThrust : IMyFunctionalBlock, IMyTerminalBlock, IMyCubeBlock, IMyEntity { float CurrentThrust { get; } //推进器的当前推力 Vector3I GridThrustDirection { get; } //推进器的朝向 float MaxEffectiveThrust { get; } //推进器的最大推实际力值(例如空气推进器的最大实际推力是和海拔高度有关的) float MaxThrust { get; } //推进器的最大推力(理论值) float ThrustOverride { get; set; } //推进器越级推力值,0表示关闭越级 float ThrustOverridePercentage { get; set; } //推进器越级推力值的百分比,0表示关闭,100表示最大 } } ~~~ 我把它的API复制过来了,贴上了注释。 注意,这些都是成员变量,这个类并没有成员函数。为什么这些成员变量后面有个{}。 其实有这个写法的,你只要记住,写了get的表示这个变量你可以用,写了set的表示这个变量你可以改。 ~~~ IMyThrust thrust; float a = thrust.CurrentThrust; //你调用了get方法,将它的值赋值给其他变量 thrust.CurrentThrust = 0; //你试图调用set方法改变它的值,报错。因为API里只给了你get方法没有给set方法 ~~~ 再举个例子,如果我们想设置某个方块的名字。 首先思考一下,方块的名字应该是个属性,而且是所有K表中可以看到的功能性方块都包含的一个属性,它肯定是写在了某个父类中。而IMyTerminalBLock刚好是所有功能性方块的父类。所以我们可以直接去查看API中的IMyTerminalBlock.cs文件,文件内容如下: ~~~ namespace Sandbox.ModAPI.Ingame { using Sandbox.ModAPI.Interfaces; using System; using System.Collections.Generic; using System.Runtime.InteropServices; using System.Text; using VRage.Game.ModAPI.Ingame; public interface IMyTerminalBlock : IMyCubeBlock, IMyEntity { void GetActions(List<ITerminalAction> resultList, Func<ITerminalAction, bool> collect = null); ITerminalAction GetActionWithName(string name); void GetProperties(List<ITerminalProperty> resultList, Func<ITerminalProperty, bool> collect = null); ITerminalProperty GetProperty(string id); bool HasLocalPlayerAccess(); bool HasPlayerAccess(long playerId); void SearchActionsOfName(string name, List<ITerminalAction> resultList, Func<ITerminalAction, bool> collect = null); [Obsolete("Use the setter of Customname")] void SetCustomName(string text); [Obsolete("Use the setter of Customname")] void SetCustomName(StringBuilder text); string CustomData { get; set; } string CustomInfo { get; } string CustomName { get; set; } string CustomNameWithFaction { get; } string DetailedInfo { get; } bool ShowInInventory { get; set; } bool ShowInTerminal { get; set; } bool ShowInToolbarConfig { get; set; } bool ShowOnHUD { get; set; } } } ~~~ 很容易猜到,这个string CustomName就是方块的名字,并且它包含了get和set方法。在编程块中,如果我们获取到某个方块,例如IMyTerminalBlock Block;,只需要对它使用Block.CustomName = "方块"; 即可修改它的名字。 接下来我们看看,如何获取方块~