Welcome to Keen Software House Forums! Log in or Sign up to interact with the KSH community.
  1. You are currently browsing our forum as a guest. Create your own forum account to access all forum functionality.

How to create optional blocks?

Discussion in 'Programming Questions and Suggestions' started by Sticky32, Feb 5, 2017.

Thread Status:
This last post in this thread was made more than 31 days old.
  1. Sticky32 Trainee Engineer

    Messages:
    6
    So I am trying to write a simple script to figure out how to check if a block with a specified name is present and if so do something, if not then ignore that part of the script and complete the rest of the script. To test this I tried to write a test script to turn on a light if an artificial mass block is found, but I'm not very familiar with programming yet... Any guidance, scripts, or links to similar topics is greatly appreciated.

    Ideally this would be used for an optional lcd panel or for optional extra rooms for an air-system.

    //Test Light Script

    IMyTerminalBlock massBlock = null;

    const string strlightblock = "Test Light";

    const string strmassblock = "Test Mass";

    lightBlock = GridTerminalSystem.GetBlockWithName(strlightblock); //This line seems to have issues

    massBlock = GridTerminalSystem.GetBlockWithName(strmassblock); // As does this one


    void Main
    {
    // logic I think this part needs a "for" command somewhere?
    if(strmassblock == null) {
    strlightblock.ApplyAction("OnOff_On");
    }
    else {
    strlightblock.ApplyAction("OnOff_Off");
    }
    }
     
    Last edited: Feb 5, 2017
  2. Sticky32 Trainee Engineer

    Messages:
    6
    I was able to figure it out.

    //Test Light Script



    IMyTerminalBlock massBlock = null;


    void Main(string argument)
    {
    var lightBlock = GridTerminalSystem.GetBlockWithName("Test Light");

    var massBlock = GridTerminalSystem.GetBlockWithName("Test Mass");


    // logic
    if(massBlock == null) {
    lightBlock.ApplyAction("OnOff_On");
    }
    else {
    lightBlock.ApplyAction("OnOff_Off");
    }
    }
     
  3. Wicorel Senior Engineer

    Messages:
    1,240
    Code will get an error if "test light" is not present
     
  4. Sticky32 Trainee Engineer

    Messages:
    6
    That's true, it was set up this way. I wanted some blocks to be optional while others are mandatory. Now I need to figure out how to make a block group or a list of blocks in a group optional, since that is apparently different.
     
  5. cheerkin Trainee Engineer

    Messages:
    60
    For mandatory blocks I recommend initializing them in ctr using something like this:
    Code:
            private T GetNamedSingleBlock<T>(string name) where T : class
            {
                T block = GridTerminalSystem.GetBlockWithName(name) as T;
                if (block == null)
                    throw new Exception("Cant find the block named'" + name + "'");
                return block;
            }
    If there is no block then you'll know its name at script first run, instead of the general null reference with no clue.

    For optional blocks just do the null check whenever you use them, thats really all.
     
Thread Status:
This last post in this thread was made more than 31 days old.