Space Engineers, figuring out the API
This is a short note on how we can develop scripts for use in the game Space Engineers.
A friend suggested Space Engineers to me, so I bought it and after a while of scavenging, mining and dying - I tried out the Programmable Block. I was in awe - it was C#.
There was one caveat though, the documentation was lacking so far behind the actual state of the game, that it was unusable. I was able to pack together small scripts from checking out posts on the official wiki, the official programming guide, and various reddit posts.
Then it dawned on me - why not go to the source, and disassemble the original game content?
Using the DotPeek tool from Jetbrains (the guys behind R#), I could quickly decompile the entire game directory (in the steamapps folder) and simply search for objects I knew the name of.
Tip: Got an object you don''t know what is?
Get the name of an object in a script by throwing its name in an exception, this way you can find specifically what you''re working with - searching in DotPeek takes care of the rest:
So in the game, with the object you don''t know what is (TheObject):
throw new Exception(TheObject.GetType().FullName);
Alternatively, you can use the
Echo() action to output the name to the detail area of the programmable block.
Echo takes a string:
Say we want to work with a door, and we know it''s type name is
IMyDoor, we search for it in Dotpeek. It quickly shows two namespaces in one assembly,
Sandbox.Common, containing an interface named that:
Checking out both reveals how they link up. The interface
Sandbox.ModAPI.IMyDoor inherits from
Sandbox.ModAPI.Ingame.IMyDoor - and indeed, it is
Sandbox.ModAPI.Ingame.IMyDoor that has the interesting parts such as
OpenDoor() etc. Notice in the image below that we can see that
Open is obsolete and we should use
Status instead. We would be told ingame that
Open is obsolete, but now how to use
Status. We''re simply in the blind as to what
Status is. In Dotpeek, we can quickly find that
DoorStatus is an enum, by placing our cursor on it and pressing F12 (Go To Declaration).
In the declaration for
DoorStatus, we see this, which resolves any questions we might have about what it is, and what it can do.