1. This forum is obsolete and read-only. Feel free to contact us at support.keenswh.com

Foreach Statements / Indexing Lists - (Possibly?) And Groups

Discussion in 'Gameplay Help' started by Jerry Webb, Apr 7, 2018.

Thread Status:
This last post in this thread was made more than 31 days old.
  1. Jerry Webb

    Jerry Webb Trainee Engineer

    Messages:
    9
    I need some help. A lot of it. But for now this will help me greatly in trying to learn what is going wrong and helping me to fix many common issues I get in my programs.

    I am not new to programming, but fairly new the SE Programming, so I would appreciate it if you guys out there could try to explain what everything does so I can fully understand it. (Thanks in advance)

    I get this error:
    Error: Foreach statement cannot operate on variables of type 'Sandbox.ModAPI.Ingame.IMyBlockGroup' because 'Sandbox.ModAPI.Ingame.IMyBlockGroup' does not contain a public definition for 'GetEnumerator'

    This is in relation with this code:

    public void Main(string argument, UpdateType updateSource)
    {
    List<Color> colors = new List<Color>();

    colors.Add(new Color(0, 255, 0)); // Green
    colors.Add(new Color(255,0,0)); // Red
    colors.Add(new Color(0,0,255)); // Dark Blue
    colors.Add(new Color(0,255,255)); // Light Blue

    var Group = GridTerminalSystem.GetBlockGroupWithName("Lights") as IMyBlockGroup;

    List<IMyTerminalBlock> lights = new List<IMyTerminalBlock>();

    foreach (IMyLightingBlock Block in Group)
    {
    lights.Add(Block);
    }


    if (argument == "normal")
    {
    int numLights = 0;
    foreach (IMyLightingBlock Light in lights)
    {
    numLights ++;
    Light.SetValue<Color>("Color", colors[3]);
    Light.SetValue("Blink Interval", 0f);
    }
    }
    } // There is more code down here, but it is similar to the "normal" one above. (I checked that they were working first)


    The problem (I think) is occuring here:

    foreach (IMyLightingBlock Block in Group)

    And for your information, on my grid I also have some other lights that I do not want affected by this script, and as such I have not grouped them in the "Lights" group with the others.

    Thanks in advance for any assistance you can give.
     
  2. Dante Darkstar

    Dante Darkstar Trainee Engineer

    Messages:
    9
    It basically says that it can't iterate over IMyBlockGroup with foreach.

    Looking at wiki, at IMyBlockGroup API (https://www.spaceengineerswiki.com/API:Sandbox.ModAPI.Ingame.IMyBlockGroup) you can find that it has property Blocks, which is a list of blocks.
    So it should work if you put Group.Blocks instead of just Group in foreach loop.
    Basically you have to iterate over the list of blocks in the group instead over the group itself.

    Disclaimer: While I'm familiar with programming in general (and some similar languages) I never actually did programming in C# or inside the SE, so I may be in error too.
     
  3. Jerry Webb

    Jerry Webb Trainee Engineer

    Messages:
    9
    I think you may be right, however, the attribute ".Blocks" and method ".GetBlocks()" no longer function in the ingame programming, so I have to investigate other ways to do it.
     
  4. Malware

    Malware Master Engineer

    Messages:
    9,867
    The block group interface had to be changed for security and performance reasons. It now works the same way as the gridterminal system itself, in that it has GetBlocks(list), GetBlocksOfType etc. methods of its own.
     
  5. Jerry Webb

    Jerry Webb Trainee Engineer

    Messages:
    9
    Also, can I quickly ask:
    How can you extend the range of sensors?
    Because I have searched everywhere, and I have no idea (still) how to do it)
     
  6. Malware

    Malware Master Engineer

    Messages:
    9,867
    You cannot extend the range of sensors outside of their set limits from an ingame script. Only mods can do that. Ingame scripts have to follow the rules of the game.

    You can alter the ranges the same way you can from the control panel though.
     
  7. Jerry Webb

    Jerry Webb Trainee Engineer

    Messages:
    9
    Well.... that ruins my contingency plans for my cruiser then.... I was going to have a sensor grid that would activate shields and sel-regenerative armour seals. :( Thanks Anyway!
     
  8. Malware

    Malware Master Engineer

    Messages:
    9,867
    I'm sorry, but scripts are not mods. This is the very difference between ingame scripts and mod scripts: Ingame scripts are programs that the engineer writes and is running in a computer on his ship. Mod scripts are programs that a player writes and is running in their game. I hope you understand what I'm trying to say here :D The point is, extending sensors beyond their defined operational limits would be cheating so ingame scripts can't do it.
     
    • Agree Agree x 1
  9. Jerry Webb

    Jerry Webb Trainee Engineer

    Messages:
    9
    Then why don't the devs just add a Radar Sensor that goes further? It really bugs me that somethings are just not possible *now* no matter which method I turn to. At least in some other games, there are ways to do things that would be considered cheating. (Not saying that they're better or anything, but just saying...)
     
  10. Malware

    Malware Master Engineer

    Messages:
    9,867
    First of all because they don't consider the programmable block important at all. It has no priority at all. It never did. Secondly... Well, the game isn't finished yet. Although I must admit I doubt there will be a radar.
     
  11. Jerry Webb

    Jerry Webb Trainee Engineer

    Messages:
    9
    Programming is becoming a more important tool in the modern world than the hammer was centuries ago (/ 1900s) - developers should at least devote a small amount of time to furthering the capabilities of a programming engine, like for example in Minecraft, the Command Block got remade and functions got added and in Scrap Mechanic, Logic Gates got a rework. These are just a few examples of Developers listening to what is happening around the world; a revolution into a new era. (No offense meant if it was offensive)
     
  12. Malware

    Malware Master Engineer

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