My first script

Discussion in 'Programming' started by CyberVic, Jan 2, 2015.

  1. CyberVic

    CyberVic Well-Known Member VIP

    Something which just turns off / on all the lights on your ship.
    Has some other useful sub-methods which can be used for other purposes.

    Code:
    void Main()
    {
        List<IMyTerminalBlock> blocks;
    
        blocks = new List<IMyTerminalBlock>();
        GridTerminalSystem.GetBlocksOfType<IMyRadioAntenna>(blocks);
        if (blocks.Count == 0) return;
        IMyRadioAntenna antenna = blocks[0] as IMyRadioAntenna;
    
        //All this works
        blocks.Clear();
        GridTerminalSystem.GetBlocksOfType<IMyLightingBlock>(blocks);
        for (int i = 0; i < blocks.Count; i++)
        {
            BlockToggle((IMyFunctionalBlock)blocks[i]);
        }
    
        antenna.SetCustomName("Hello Galaxy!");
    
        Beep();
    }
    
    
    void BlockToggle(IMyFunctionalBlock block)
    {
        BlockAction(block, "OnOff");
    }
    void BlockEnable(IMyFunctionalBlock block)
    {
        BlockAction(block, "OnOff_On");
    }
    void BlockDisable(IMyFunctionalBlock block)
    {
        BlockAction(block, "OnOff_Off");
    }
    void Beep()
    {
        IMySoundBlock speaker = GridTerminalSystem.GetBlockWithName("Test Speaker") as IMySoundBlock;
        if (speaker == null) return;
        BlockAction(speaker, "PlaySound");
    }
    void BlockAction(IMyFunctionalBlock block, string action)
    {
        var foundAction = block.GetActionWithName(action);
        if (foundAction != null)
        {
            foundAction.Apply(block);
        }
    }
    
     
    Last edited: Jan 2, 2015
  2. BurningIce2

    BurningIce2 Active Member VIP

    I'd love to understand this, I might be learning this in the future.

    In an avalanche, not one snowflake feels responsable.
    -Voltaire, during the French Revolution.