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.

[Suggestion] Identify which programmable block code is running on

Discussion in 'Programming Questions and Suggestions' started by Sirhan Blixt, Jan 9, 2015.

Thread Status:
This last post in this thread was made more than 31 days old.
  1. Sirhan Blixt Apprentice Engineer

    Messages:
    451
    PROBLEM: it is not possible to determine which programming block in a grid a script is running on. There are many use cases involving grids joined together and/or which are going to be separated (docked via connector or landing gear, or built from projector and not yet sawn free). Being able to identify the "owning" programmable block is merely the first step in discovering what other modules belong (or will belong) to that grid.

    SOLUTION: provide another built-in variable that identifies the programmable block that the program is running on, like
    Code:
    IMyProgrammableBlock Me;  // or something similar
    
    DO NOT override the "this" keyword, as it already has a very specific definition that is only slightly congruent to what I've proposed.

    (If you're a Visual Basic nerd wanting to point out that "Me" means basically the same thing as "this" in VB, know that your anger and tears only make me grow stronger.)
     
    Last edited by a moderator: Jan 9, 2015
  2. Digi Senior Engineer

    Messages:
    2,377
    Me, Self, whatever its name will be, we need it! :}
     
    Last edited by a moderator: Jan 9, 2015
  3. Kuu Lightwing Senior Engineer

    Messages:
    1,503
    IMyProgrammableBlock Meesa.

    Good idea, though.
     
  4. Sirhan Blixt Apprentice Engineer

    Messages:
    451
    It also occurs to me that it'd be good to know who "called" the prog block. That is, have a way to discover which block applied this prog block's "Run" action.

    Add to this Suggestion, or create a new one? Time to flip a coin ...
     
  5. AutoMcD Senior Engineer

    Messages:
    2,369
    would be nice even if at a bare minimum it had some blinky lights or something like the timers do. (unless they do and i missed it)
     
  6. szkuwa Trainee Engineer

    Messages:
    7
    Instead of introducing new global variable I would suggest
    Code:
    GridTerminalSystem.CurrentGrid
    which would point to something like BlockGroup (then we would be able to simply iterate over it and find blocks that we're interested in)
     
    Last edited by a moderator: Jan 12, 2015
  7. Malware Master Engineer

    Messages:
    9,511
    You couldn't possibly override the "this" keyword if you wanted to :)
    szkuwa it wouldn't be a global variable, it would be a property of the current script - which is a class itself. So it would be perfectly reasonable in my opinion.
     
  8. Phoera Senior Engineer

    Messages:
    1,713
    Devs can set this to be the our block.
    then our code will be

    Code:
    class Program:IMyProgrammableBlock
    
    But i found this:
    Code:
    IMyProgrammableBlock GetThis()
    {
        var blocks=new List<IMyTerminalBlock>();
        GridTerminalSystem.GetBlocksOfType<IMyProgrammableBlock>(blocks,GetThisFilter);
        if(blocks.Count>1)  
            throw new Exception("More than one blocks is running");
        return blocks[0] as IMyProgrammableBlock;
    }
    bool GetThisFilter(IMyTerminalBlock block)
    {
        return (block as IMyProgrammableBlock).IsRunning;
    }
    Tested a bit and it works.
     
    Last edited by a moderator: Jan 12, 2015
  9. Malware Master Engineer

    Messages:
    9,511
    "this" will always be the class your code is running in, it's just the way C# works - the devs cannot do anything about that. It's intrinsic to the language, a keyword that cannot be altered.

    [Edit]
    Aaand maybe I should actually read what you write before responding. I am sorry.

    But I still don't think it would work, because the IMyProgrammable block is an actual block within the game, and the program is within a dynamic assembly which needs to be recompiled time and again. They would have to replace the block every time the script is rebuilt.
     
    Last edited by a moderator: Jan 12, 2015
  10. Phoera Senior Engineer

    Messages:
    1,713
    yes, yes, it is not possible, i know(i just a bit tired and take wrong inherirance order)

    Better just give us new variable, as i looked how it works now, need change one line in one method(Compiler) and add one line in initialization of our class.

    Devs, please do this:)
     
  11. Me 10 Jin Apprentice Engineer

    Messages:
    463
    I would prefer it if the devs would add two optional parameters to the 'Main' block, the first would be type IMyTerminalBlock and would be passed the block that triggered the script. The second would be IMyProgrammableBlock and would be passed the block that's actually running the script.

    As parameters we would be free to name them however we may. E.g:
    Code:
    void Main( IMyTerminalBlock instigatorBlock, IMyProgrammableBlock scriptHostBlock )
    ...
    
     
    Last edited by a moderator: Jan 13, 2015
  12. Digi Senior Engineer

    Messages:
    2,377
    Why would you prefer it to be a parameter of Main() ? You'll be forced to store it again in the class if you want to use it outside of the Main() method :p

    I think a variable like GridTerminalSystem would be better.


    If we can get the programmable block that runs the script then we can just use that block's CubeGrid variable to get the grid that it's on.
     
    Last edited by a moderator: Jan 13, 2015
  13. Znurf Trainee Engineer

    Messages:
    14
    If it can/could call itself, wouldnt that make it possible to create (infinite)loops where the program never stops? And isnt that something the devs doesnt want? :)
     
  14. Malware Master Engineer

    Messages:
    9,511
    If you do I'd guess you'll get the "script too complex" error. Haven't tried though :)
     
  15. Phoera Senior Engineer

    Messages:
    1,713
    you will get complex error.

    counter works on restrict manner, if it cannot count something it will throw error.(this is why script give error at lambdas, some switches, some foreach)
    simple calls it can count, counter resets outside of our class, so we cannot reset it.

    also infinite loops(if allowed) will freeze the game out.
     
  16. Sirhan Blixt Apprentice Engineer

    Messages:
    451
    I suppose that depends on what you mean by "call itself." I have no idea what you mean by "call itself."
     
Thread Status:
This last post in this thread was made more than 31 days old.