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 select blocks from own grid with a special string in name

Discussion in 'Programming Questions and Suggestions' started by ZnvBecUet, Feb 8, 2017.

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

    Messages:
    7
    Can someone give me working example code to select blocks from the own grid and with a special string in the block name?
     
  2. Georgik Apprentice Engineer

    Messages:
    215
    Code:
    void Main() {
    	var lst = new List<IMyTerminalBlock>();
    	GridTerminalSystem.SearchBlocksOfName("special string", lst);
    }
    Assuming you meant own grid like also everything connected to it through rotors/connectors, otherwise you can use this:
    Code:
    void Main() {
    	var lst = new List<IMyTerminalBlock>();
    	GridTerminalSystem.SearchBlocksOfName("special string", lst, b => b.CubeGrid == Me.CubeGrid);
    }
    Now, in List<IMyTerminalBlock> lst should be all blocks containing the special string in their name.
     
    • Friendly Friendly x 1
  3. ZnvBecUet Trainee Engineer

    Messages:
    7
    Thanks
     
  4. plaYer2k Master Engineer

    Messages:
    3,160
    Just a small addition. The above code gets blocks that have the exact same name while the initial request was "with string in its name".

    Thus i would suggest
    Code:
    // The tag you want to include
    const string tag = "someTag";
    List<IMyTerminalBlock> blocks = new List<IMyTerminalBlock>();
    void Main(string arg)
    {
      GridTerminalSystem.GetBlocksOfType<IMyTerminalBlock>(blocks, x => x.CubeGrid == Me.CubeGrid && x.CustomName.Contains(tag));
    
      // Do something with the blocks list
      ...
    
      // Free the block references again
      blocks.Clear();
    }
    
     
  5. Georgik Apprentice Engineer

    Messages:
    215
    @plaYer2k I disagree, SearchBlocksOfName is looking for parts of name. No need for this, although it has the same effect. :)
    Code:
            void ModAPI.IMyGridTerminalSystem.SearchBlocksOfName(string name, List<ModAPI.IMyTerminalBlock> blocks, Func<ModAPI.IMyTerminalBlock, bool> collect)
            {
                blocks.Clear();
                foreach (var block in m_blocks)
                {
                    if (block.CustomName.ToString().Contains(name, StringComparison.OrdinalIgnoreCase))
                    {
                        if (collect != null && collect(block) == false)
                        {
                            continue;
                        }
                        blocks.Add(block);
                    }
                }
            }
     
    • Like Like x 1
  6. plaYer2k Master Engineer

    Messages:
    3,160
    Eh, i was always of the impression that it searches for the exact name. Well the method is very badly named then because "blocks of name" is very different from "blocks containing".
    Apparently the methods with ...WithName search for exact matches and those with ...OfName are meant to check for blocks containing.

    Anyhow, the result is the same. You get a list of blocks containing the tag on the same grid.
     
  7. Wicorel Senior Engineer

    Messages:
    1,241
Thread Status:
This last post in this thread was made more than 31 days old.