Block Switcher 4.75/5 (4)

posted in: Control, Operations, Scripts | 1

I was originally asked to write this script by someone who wanted to use it to switch from one decoy to another to “confuse” turrets that were trying to target your ship.

It works, because as one decoy turns off and another one turns on, the enemy turret has to track and lock on to the new target before it can fire. The idea being that before it manages to lock on the new target, the decoy switches again. So the turrets keep tracking and trying to lock on to an ever “moving” target.

Main problem with this idea is that the further away you are from the turret, the less the turret has to track, therefore it is fairly quick to reacquire a target and fire. It works nicely at close quarters, but the further away you are, the less effective.

Also, the distance between your decoys also influences the efficiency of switching between them.

Since then, other people have asked for a script that switches gravity generators off and on, randomly (no idea why).

So I give you the generic “Block Switcher” script.

Change the “ChangeMe” string to the string that identifies all the blocks you want it to randomly switch. Don’t create a group, name all the blocks the same. Set up a timer that runs the script and triggers itself. This means the script gets run constantly, very fast.

You can change the variable at the beginning: “changeFrequency” to make the switching faster or slower. The higher the value, the longer between switches (it’s basically, how many times does the script have to be run for 1 switch).

The script never activates the same block twice in succession.

This has any number of uses, from the ones described above to creating a disco effect using lighting.

Hope you find this useful!

If you would like a version of this script that uses a group of blocks instead of blocks all named the same, let me know and I’ll see what I can do.


int changeFrequency = 20;
    int counter = 0;
    List blocks = new List();    

    void Main()       
    {    
        counter++;    
        if (counter > changeFrequency) {
           counter = 0;
           GridTerminalSystem.SearchBlocksOfName("ChangeMe", blocks);  
           int oldTarget = turnOffBlocks();
           randomizeBlock(oldTarget);   
        }
    }    

    int turnOffBlocks() {  
        int retVal = 0;
        for (int i = 0; i < blocks.Count; i++)   
        {   
            var d = blocks[i];    
            if (d.IsWorking) { retVal = i; }
            d.GetActionWithName("OnOff_Off").Apply(d);
        }
        return retVal;
    }

    void randomizeBlock(int oldTarget) { 

        Random rnd = new Random(); 
        int newTarget = rnd.Next(blocks.Count);
        while (newTarget == oldTarget) 
        {
            newTarget = rnd.Next(blocks.Count);
        }
        var d = blocks[newTarget];    
        d.GetActionWithName("OnOff_On").Apply(d);

    }

Please rate this post

One Response

  1. Dope. Works like a charm. I put changeFrequency at 0 and put the timer block on a trigger now loop. It works like a charm. BTW the angled brackets, and the IMyTerminalBlock, aren’t showing on this one either after List. But I know that’s not your fault, it’s the stupid html crap having a hay day with angle brackets. Just wanted to give you a heads up. And once again, thank you for the awesome scripts.

Leave a Reply