Space Engineers

Space Engineers

EasyAPI
EasyAPI By examples (01): Automatic doors
This is a simple example to show us how EasyAPI (code name ESY) is powerful. What we want here is when an outer door is open, we close all the other outer and inner doors. To keep O2 inside the whole ship. And when an inner door is open, we want to be sure that all the other doors are also closed. For sure you can add pieces of code to lock them, start warnings etc (in case of attack thru sensors) but it is not the point here.

Code will follow.
LKS
Terakhir diedit oleh LukeStrike; 24 Jun 2016 @ 2:50pm
< >
Menampilkan 1-5 dari 5 komentar
LukeStrike 12 Jun 2016 @ 9:17pm 
Tested with:

/************************************************************************************
EasyAPI - Documentation: https://steamhost.cn/steamcommunity_com/sharedfiles/filedetails/?id=381043
*************************************************************************************/
// This script was made for the LKS_Hydros but can be applied to similar ships
// That means just one group for outer doors, one for inner doors, and one for all the doors
// Feel free to rename the groups accordingly.
//

public static string AIRLOCK_DOORS = "HY 1- Doors"; // your doors group
public static string AIRLOCK_OUTER_DOORS = "HY 1- Doors (Outer)"; // your outer doors group
public static string AIRLOCK_INNER_DOORS = "HY 1- Doors (Inner)"; // your inner doors group
public static ♥♥♥♥ AIRLOCK_AUTOCLOSE = true; // set it to true if you want the door closing automaticaly after a delay
public static long AIRLOCK_AUTOCLOSEDELAY = 3 * EasyAPI.Seconds; // autoclose delay set to 3 seconds, change it if you want

public class Example : EasyAPI
{
public Example(IMyGridTerminalSystem grid, IMyProgrammableBlock me, Action<string> echo, TimeSpan elapsedTime) : base(grid, me, echo, elapsedTime)
{
// if an outer door is open we close all the other (outer/inner) doors
Blocks.InGroupsNamed(AIRLOCK_OUTER_DOORS).AddEvent(
delegate(EasyBlock thisdoor)
{
return thisdoor.Open();
},
delegate(EasyBlock thisdoor)
{
Blocks.InGroupsNamed(AIRLOCK_DOORS).Minus(thisdoor).ApplyAction("Open_Off");
if(AIRLOCK_AUTOCLOSE) // autoclose the outer door that was open, if any
{
In(AIRLOCK_AUTOCLOSEDELAY, delegate()
{
thisdoor.ApplyAction("Open_Off");
});
}

return true;
},
true
);

// if an inner door is open we close all the other (outer/inner) doors
Blocks.InGroupsNamed(AIRLOCK_INNER_DOORS).AddEvent(
delegate(EasyBlock thisdoor)
{
return thisdoor.Open();
},
delegate(EasyBlock thisdoor)
{
Blocks.InGroupsNamed(AIRLOCK_DOORS).Minus(thisdoor).ApplyAction("Open_Off");
if(AIRLOCK_AUTOCLOSE) // autoclose the inner door that was open, if any
{
In(AIRLOCK_AUTOCLOSEDELAY, delegate()
{
thisdoor.ApplyAction("Open_Off");
});
}

return true;
},
true
);
}
}

/*********************************************/
/*** Advanced users only beyond this point ***/
/*********************************************/
...
Terakhir diedit oleh LukeStrike; 7 Nov 2018 @ 5:19am
LukeStrike 12 Jun 2016 @ 9:19pm 
As you can see the door you just opened can be also automaticaly closed behind you after a delay. Just change the AIRLOCK_AUTOCLOSE and AIRLOCK_AUTOCLOSEDELAY the way you want.

Enjoy,
LKS
LukeStrike 12 Jun 2016 @ 9:32pm 
Of course this is just an example. Maybe you do not want to close all the outer doors when opening an inner door (because outer doors have their own airlock system for example). Up to you to adapt this code to your needs ;)

LKS
Kri 13 Jun 2016 @ 6:04am 
Here's another spin at the airlock doors script. It is based on the example door close script on EasyAPI GitHub page.

By default it will auto close any door after 2-3sec.
If you name two or more doors with exactly same name this script will make sure only one door of the same name can be open at one time by turning off the other(s).

public class Airlocks : EasyAPI { EasyBlocks doors; public Airlocks(IMyGridTerminalSystem grid, IMyProgrammableBlock me, Action<string> echo, TimeSpan elapsedTime) : base(grid, me, echo, elapsedTime) { DoorAutoClose(); } public void DoorAutoClose() { doors = Blocks.OfTypeLike("Door").NotOfType("Airtight Hangar Door"); In(1 * EasyAPI.Seconds, delegate() { // In one second, create the events. doors.AddEvent( delegate(EasyBlock block) { // When a door is opened return block.Open(); }, doorOpened, true // only trigger event when the condition (door open) goes from false to true ).AddEvent( delegate(EasyBlock block) { return !block.Open(); }, doorClosed, true ); }); } public ♥♥♥♥ doorOpened(EasyBlock block) { // Do the following In(2 * EasyAPI.Seconds, delegate() { // In 2 seconds block.ApplyAction("Open_Off"); // close the door }); EasyBlocks paired_doors = doors.Named(block.Name()) - block; paired_doors.Off(); return true; } public ♥♥♥♥ doorClosed(EasyBlock block) { EasyBlocks paired_doors = doors.Named(block.Name()) - block; paired_doors.On(); return true; } } Airlocks state; void Main(string argument) { if(state == null) { state = new Airlocks(GridTerminalSystem, Me, Echo, ElapsedTime); } state.Tick(100 * EasyAPI.Milliseconds, argument); }
Terakhir diedit oleh Kri; 13 Jun 2016 @ 6:53am
In my example this is the same function for either inner/outer doors ... of course you can simplify the code and apply it to the whole group AIRLOCK_DOORS but I decided to split inner/outer doors in case of you want to add some functions to them (activate inner turrets, trigger an alarm, etc)

This is your code, not mine ;)
LKS
< >
Menampilkan 1-5 dari 5 komentar
Per halaman: 1530 50