【原创】可编程块API辅助代码
太空工程师吧
全部回复
仅看楼主
level 6
songyx0 楼主
Github 项目地址:https://github.com/BeMxself/SpaceEngineers_ProgrammableBlock_Helper
通用物块操作函数
IMyTerminalBlock GetBlock(string name) 传递物块名,返回物块对象
T GetBlock<T>(string name) 功能同上,泛型版
void RunAction(string blockName, string actionName) 传递物块名,执行 Action
void RunAction(IMyTerminalBlock block, string actionName) 传递物块对象,执行 Action
void BatchRunAction(IList<string> blockNames, string actionName) 通过物块名列表(如数组),指定多个物块执行 Action
void BatchRunAction(IList<IMyTerminalBlock> blocks, string actionName) 多个物块执行 Action
Var类
实现了通用的变量存储功能
SE 编程部分没有能够存储状态的全局变量之类的东西,所以这里是通过把值存在某个物块的名字里实现的。
构造函数
Var(IMyGridTerminalSystem gts)
Var(IMyGridTerminalSystem gts, string prefix)
Var(IMyGridTerminalSystem gts, string prefix, string separator)
参数说明
gts 传递GridTerminalSystem
prefix 用来存变量的物块名称前缀,不指定默认是 “$var”
要注意的查找物块时只要包含$var就会返回,也就是说如果你同时有一个叫$var1的物块用来存其他变量,很可能本来你要存在$var上的值会存到$var1上。
spearator 变量间的分隔符,不指定默认是 “|”(竖线)
方法
T GetVar<T>() 返回指定类型的值
T GetVar<T>(uint index) 同上,不过可以指定索引位置(从0开始),也就是说一个Var对象可以存储多个值
void SetVar(object value) 设置值
void SetVar(uint index, object value) 同上,可以指定索引位置,同样也是0开始
2015年01月18日 09点01分 1
level 6
songyx0 楼主
有的同学访问不了GitHub,这里把代码贴出来
不过度娘会删除空格,破坏缩进,凑合看吧
IMyTerminalBlock GetBlock(string name){
IMyTerminalBlock block = GridTerminalSystem.GetBlockWithName(name);
if (block==null)
throw new Exception(String.Format("\"{0}\" Not Found"));
return block;
}
T GetBlock<T>(string name){
IMyTerminalBlock block = GetBlock(name);
return (T)block;
}
void RunAction(IMyTerminalBlock block, string actionName){
block.GetActionWithName(actionName).Apply(block);
}
void RunAction(string blockName, string actionName){
RunAction(GetBlock(blockName), actionName);
}
void BatchRunAction(IList<string> blockNames, string actionName){
for(int i = 0; i < blockNames.Count; ++i)
RunAction(blockNames[i], actionName);
}
void BatchRunAction(IList<IMyTerminalBlock> blocks, string actionName){
for(int i = 0; i < blocks.Count; ++i)
RunAction(blocks[i], actionName);
}
class Var{
private IMyGridTerminalSystem gts;
private IMyTerminalBlock varBlock;
private string[] varArray;
private string separator;
public Var(IMyGridTerminalSystem gts){
this.gts = gts;
InitVar("$var", "|");
}
public Var(IMyGridTerminalSystem gts,string prefix){
this.gts = gts;
InitVar(prefix, "|");
}
public Var(IMyGridTerminalSystem gts,string prefix, string separator){
this.gts = gts;
InitVar(prefix, separator);
}
private void InitVar(string prefix, string separator){
this.separator = separator;
varBlock = FirstBlockWithPrefix(prefix);
varArray = varBlock.CustomName.Split(new string[]{separator}, StringSplitOptions.None);
}
private IMyTerminalBlock FirstBlockWithPrefix(string prefix){
List<IMyTerminalBlock> allBlocks = gts.Blocks;
for (int i = 0; i < allBlocks.Count; ++i){
if (allBlocks[i].CustomName.StartsWith(prefix))
return allBlocks[i];
}
throw new Exception(String.Format("FirstBlockWithPrefix: prefix \"{0}\" not found", prefix));
}
public T GetVar<T>(){
return GetVar<T>(0);
}
public T GetVar<T>(uint index){
return (T)(Object)varArray[index+1];
}
public T GetVarWithDefault<T>(object defaultValue){
return GetVarWithDefault<T>(0, defaultValue);
}
public T GetVarWithDefault<T>(uint index, object defaultValue){
try{
return GetVar<T>(index);
}
catch(Exception e){
return (T)defaultValue;
}
}
public void SetVar(object value){
SetVar(0, value);
}
public void SetVar(uint index, object value){
if (index+2 > varArray.Length)
Array.Resize(ref varArray, (int)index+2);
varArray[index+1] = value == null ? "" : value.ToString();
varBlock.SetCustomName(String.Join(separator, varArray));
}
}
2015年01月18日 09点01分 2
level 6
songyx0 楼主
Var类漏了一组方法:
T GetVarWithDefault<T>(object default) 返回指定类型的值,如果获取失败则返回指定的默认值
T GetVarWithDefault<T>(uint index, object default) 同上,不过可以指定索引位置(从0开始)
2015年01月18日 09点01分 4
level 7
数据存在名字里……点赞
顺便请教个问题,rotor如何获取当前旋转的角度?
我看文档里貌似只能获取上下限和速度什么的…
2015年01月18日 09点01分 5
我也没找到获取角度的方法,应该根本还没开放这个接口
2015年01月18日 09点01分
level 6
songyx0 楼主
刚发现泛型版的GetBlock暂时有问题
2015年01月18日 09点01分 6
level 6
songyx0 楼主
刚才在官方论坛里逛了逛看到有好多手册里没有的API
关于Rotor角度有下面解决方案,两个都是同样的原理,就是获取 DetailedInfo 字符串,从中截取角度数字
1.
var rotorStator = GridTerminalSystem.GetBlockWithName("Rotor") as IMyMotorStator;
string info = rotorStator.DetailedInfo
string CAngString = System.Text.RegularExpressions.Regex.Replace(info, "[^.0-9]", "")
int CAng = Convert.ToInt32(CAngString);
2. 不知道汉化版会不会汉化 DetailedInfo 里的文字,如果汉化了的话这个方法可能会有问题
IMyMotorStator rotor =GridTerminalSystem.GetBlockWithName("Rotor 1") as IMyMotorStator;
double rotorAng = Convert.ToDouble((rotor.DetailedInfo.Remove(0,15)).TrimEnd('°'));
@firzencode
2015年01月18日 13点01分 7
请问哪里有官方给的模块调用的接口?
2015年01月18日 14点01分
@darkpursuer 官方的接口太坑了,可以在官方wiki里 http://www.spaceengineerswiki.com/ 搜索Programming Guid 或者 Programmable Block;更多公开的接口可以在这里看 http://www.spaceengineerswiki.com/ https://jcuber.github.io/space-engineers-doc-collab 这个网址是我今晚才发现的
2015年01月18日 14点01分
卧槽,这个厉害
2015年01月18日 15点01分
level 6
songyx0 楼主
官方wiki里 http://www.spaceengineerswiki.com 搜索 Programming Guid 或者 Programmable Block 可以看接口和 Action 等
更多公开的接口可以在这里看 https://jcuber.github.io/space-engineers-doc-collab
ps:这个网址是我今晚才发现的,另外官方的论坛真是宝藏,好多好东西
2015年01月18日 14点01分 8
level 6
songyx0 楼主
还有一个官方正式渠道没有公开的接口就是 DataStorage 变量,这是一个String类型的变量,这个变量在脚本执行结束后不会清空。
但是这个变量不能在不同的 Programmable Block 间共享,而且据说过一段时间会自动销毁(应该是一直不使用的话会自动销毁吧)
2015年01月18日 14点01分 9
level 9
我们FPX群现在需要一个太空程序猿!1660517!来吧来吧!就是你了!
可以帮你改进timerblock的效率问题!
(当然了,不进来我也是会告诉你方法的)
2015年01月18日 16点01分 10
level 13
滋兹滋兹
2015年01月18日 16点01分 11
level 11
[汗]明明我搬运过一次
2015年01月18日 17点01分 12
明明是我昨天刚写的……
2015年01月18日 23点01分
@songyx0 ...我说网址和API
2015年01月19日 03点01分
回复
��������
:哦哦,搜嘎
2015年01月19日 04点01分
回复
��������
:刚接触游戏没几天,贴吧论坛什么的还没怎么看过
2015年01月19日 04点01分
level 7
太空程序员
2015年01月19日 06点01分 13
level 6
songyx0 楼主
在github上看到有人写的helper类:
https://github.com/mwinkler/SpaceEngineers.BlockHelper
用到了c#语言的一些其他特性,使用的时候可以很优雅,但是直接考到游戏里不能运行,有空修改一下合并到自己的库里
2015年01月19日 10点01分 14
啊啊啊,SE对C#语言特性的支持真是太差了,泛型不支持,扩展方法不支持
2015年01月20日 03点01分
这个helper是ModApi用的不是IngameScript用的 另外你仔细观察会发现在脚本里定义的全局变量都是static的,执行脚本结束后不会消失 但是貌似有生存周期,可能和编程块本身被刷新了有关
2015年01月20日 07点01分
@songyx0 [滑稽]任何可能会在解释器里产生不可预测分支的代码都不能用,调试了1周我算是放弃写复杂脚本了,实现一些可能随时调参数的简单功能就行了
2015年01月20日 07点01分
回复
����������
:经过测试,我发现全局变量(不用static关键字也行)居然可以一直保存,我擦枉费我半天时间写各种存储功能……
2015年01月20日 07点01分
1