Skip to content

Instantly share code, notes, and snippets.

@seronis
Created May 13, 2019 22:54
Show Gist options
  • Save seronis/8d3665318768bb6add321fd3e7a17a6c to your computer and use it in GitHub Desktop.
Save seronis/8d3665318768bb6add321fd3e7a17a6c to your computer and use it in GitHub Desktop.
Space Engineers Scripting Tutorial Scenario - Updated Code for 2nd Tut
// In space oxygen is a very valuable ressource
// keeping a large place supplied with oxygen
// when ressources are getting low can be a waste
// therefore it can be relevant if the case happen to
// reduce the oxygenated area and bring back
// oxygen where needed.
// This script ensure that when the oxygen generator
// ice ressources become low oxygen gets sucked
// out and then reserved for the medical room.
/* PS:
* Try to understand the code. If you cant, focus on what is
* missing in the damaged programs. In this series of exercices
* The code is always mirrored in a way so that working
* examples can help you complete the non working ones
* by changing very few things.
*/
void DepressurizeAllAirVents()
{
var myAirVents = new List<IMyAirVent>();
GridTerminalSystem.GetBlocksOfType<IMyAirVent>(myAirVents);
for (int i = 0; i < myAirVents.Count; i++)
{
//We exclude the Airvent of the sas of course
if (myAirVents[i].CustomName != "SasAirVent")
myAirVents[i].ApplyAction("Depressurize_On");
}
}
/* PS:
* This function code is here to show you how you can access
* Inventories of your blocks. It is yet not relevant to fix the actual
* issue as it does not have to be changed from one code to the other.
*/
float GetGeneratorRemainingIceAmount()
{
float IceAmount = 0.0f;
// To access the inventory of a block you can slice it to IMyInventoryOwner.
var myOxygenGenerator = GridTerminalSystem.GetBlockWithName("OxygenGenerator") as IMyGasGenerator;
if (myOxygenGenerator != null)
{
// Searching every slots in the inventory is done this way
// in Space engineers an inventory is a slot that contains
// items of a given type. So basicly when you open the
// inventory tab what you see is in fact a list of inventories
// characters and blocks possess thus a list of inventories.
for (var i = 0; i < myOxygenGenerator.InventoryCount; i++)
{
var slot = myOxygenGenerator.GetInventory(i);
var items = new List<MyInventoryItem>();
slot.GetItems(items);
for (int j = 0; j < items.Count; j++)
{
IceAmount += (float)items[j].Amount;
Echo(items[j].Amount.ToString());
}
}
}
return IceAmount;
}
void ChangeOxygenRoomLightsColor()
{
var myLights = new List<IMyTerminalBlock>();
GridTerminalSystem.SearchBlocksOfName("Light_OxygenRoom", myLights);
var myColor = new Color(255, 0, 0);
for (int i =0; i< myLights.Count; i++)
{
var light = myLights[i] as IMyInteriorLight;
light.SetValue("Color", myColor);
}
}
void Main(string argument)
{
if (GetGeneratorRemainingIceAmount() < 100)
{
DepressurizeAllAirVents();
ChangeOxygenRoomLightsColor();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment