Accessing All Blocks in a Grid 5/5 (1)

posted in: Snippets | 0

Each station or ship is a separate grid. You’ll see a lot of conversation on the Keen forums on how you cannot access, in your program on your programmable block, any blocks that don’t belong to the same grid.

What this means, in practice, is that if you have a programmable block on a ship, it can only “see” the other blocks that are part of that ship.

The exception to this is when ships are docked via connectors. If you have a programmable block on one ship and dock it with another, that programmable block can access any blocks on either of the ships.

So… basics… How do I retrieve the blocks?

  1. var blocks = GridTerminalSystem.Blocks;

After this command executes, you will have an object: “blocks” that will contain a list of IMyTerminalBlock objects.

If you wanted to perform some operation on all the blocks, you use a for loop:

for (int x = 0; x < blocks.Count; x++) { // Retrieve the single block from the block list, and put it in my "b" object. IMyTerminalBlock b = blocks[x]; // Do something with the "b" object b.SomeMethod(); } [/sourcecode] A quick explanation of the for loop:

int x = 0;
Defines “x” as our index variable and starts it at 0;
x < blocks.Count;
Tells the computer that this loop will continue to execute as long as this condition is true; So as long as “x” is less than the number of blocks in our “block” list, continue processing the loop;
Tells the loop what should happen at the end of every iteration. For example if we only wanted to process every other block in the list, we would put x+=2

So as the loop is processed, it executes all the instructions the first time with “x” equal to 0, the second time “x” is equal to 1, and so on until “x” is equal to blocks.Count (the number of blocks in the “blocks” list object).

Hope this helps!

Please rate this post

Leave a Reply