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.

Programming Thrusters - Please help :)

Discussion in 'Programming Questions and Suggestions' started by Abyzmi, Jan 3, 2015.

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

    Messages:
    66
    Hey all!

    Several Questions if you please:

    I'm trying to increase the override for 2 of the 8 of my thrusters on my ship. These two thrusters are the forward thrusters of my ship. My programming experience is extremely limited. Would anyone mind helping me out with this?

    I got the thrust to override for a single thruster at the [0] index of the list of thrusters, which is cool, but not exactly what I was looking for.

    Here's my code:

    void Main()
    {
    List<IMyTerminalBlock> mainThrusters = new List<IMyTerminalBlock>();
    GridTerminalSystem.GetBlocksOfType<IMyThrust>(mainThrusters);

    int increaseCount = 20;
    for(int i = 0; i < increaseCount; i++)
    {
    ITerminalAction increaseThrust = mainThrusters[0].GetActionWithName("IncreaseOverride");
    increaseThrust.Apply(mainThrusters[0]);
    }
    }

    So I suppose what I'm wondering is this:
    Is there perhaps a way to make a sepearte list for just those two thrusters? Or perhaps a way to assign those two thrusters to a specific index within the list?

    I think once I get this figured out, I can maybe sort of figure out the rest.

    Thank you!!
     
    Last edited by a moderator: Jan 3, 2015
  2. sioxernic Senior Engineer

    Messages:
    2,535
    Use GetBlockWithName instead of GetBlocksOfType.
     
  3. Cuber Apprentice Engineer

    Messages:
    262
    You can
    A) Add a unique identifier to the names of those two thrusters (something that won't be found in any other names - personally I use something like %001) and use GridTerminalSystem.SearchBlocksOfName
    Example (assuming that the identifier is %001):
    Code:
    void Main()
    {
      var mainThrusters = new List<IMyTerminalBlock>();
      GridTerminalSystem.SearchBlocksOfName("%001", mainThrusters);
      for (int i = 0; i < mainThrusters.Count; i++)
      {
        mainThrusters[i].GetActionWithName("IncreaseOverride").Apply(mainThrusters[i]);
      }
    }
    
    B) Group those two thrusters, iterate through GridTerminalSystem.BlockGroups and grab the group with the correct name
    Code:
    void Main()
    {
        var list = GridTerminalSystem.BlockGroups;
        IMyBlockGroup group = null;
        for (int i = 0; i < list.Count; i++)
        {
            if (list[i].Name == "Batteries")
            {
                group = list[i];
                break;
            }
        }
        List<IMyTerminalBlock> mainThrusters = group.Blocks;
        for (int i = 0; i < mainThrusters.Count; i++)
        {
            (mainThrusters[i] as IMyFunctionalBlock).RequestEnable(false);
            // the one above is essentially the same as [SIZE= 11px]mainThrusters[/SIZE][SIZE= 11px][[/SIZE][SIZE= 11px]i[/SIZE][SIZE= 11px]].[/SIZE][SIZE= 11px]GetActionWithName[/SIZE][SIZE= 11px]([/SIZE][SIZE= 11px]"IncreaseOverride"[/SIZE][SIZE= 11px]).[/SIZE][SIZE= 11px]Apply[/SIZE][SIZE= 11px]([/SIZE][SIZE= 11px]mainThrusters[/SIZE][SIZE= 11px][[/SIZE][SIZE= 11px]i[/SIZE][SIZE= 11px]]);[/SIZE]
        }
    }
    
     
    Last edited by a moderator: Jan 3, 2015
  4. Abyzmi Trainee Engineer

    Messages:
    66
    Thanks for the reply!
    Will that retrieve all of the blocks with that specific name?
    Would you mind providing an example of how that line of code might look?
     
  5. Abyzmi Trainee Engineer

    Messages:
    66
    Also, Cuber, would you mind showing me what the BlockGroups line might look like? Just any random example would be great!
    I don't really know how to apply that to the list...
     
  6. Cuber Apprentice Engineer

    Messages:
    262
    I just edited my post above with working examples :thumb:
     
  7. Abyzmi Trainee Engineer

    Messages:
    66
    Thank youuu!
     
  8. Abyzmi Trainee Engineer

    Messages:
    66
    Cuber that was exactly what I needed. Thanks so much!
     
  9. Cuber Apprentice Engineer

    Messages:
    262
    No problem! Glad it worked!
     
  10. Abyzmi Trainee Engineer

    Messages:
    66
    Okay, new question, if you don't mind!

    Now, I'm trying to sort of link the action of overriding the accelleration with a timer block. In this case, the thrusters will use the DecreaseOverride method after the timer has finished counting.

    Here is my code (the part that i'm having trouble with is bolded and underlined):

    void Main()
    {

    //////////// Put the main thrusters into a list by grouping.

    var list = GridTerminalSystem.BlockGroups;
    IMyBlockGroup group = null;
    for (int i = 0; i < list.Count; i++)
    {
    if (list.Name == "Thrusters")
    {
    group = list;
    break;
    }
    }

    List<IMyTerminalBlock> mainThrusters = group.Blocks;

    //////////// Start the timer block
    var timer = GridTerminalSystem.GetBlockWithName("Timer Block");
    ITerminalAction startTimer = timer.GetActionWithName("Start");
    startTimer.Apply(timer);

    /////////// Increase the thrust of the thrusters while the timer is counting down.

    while(timer.IsCountingDown == true)
    {
    for(int i = 0; i < mainThrusters.Count; i++)
    {
    for(int j = 0; j < 20; j++)
    {
    mainThrusters.GetActionWithName("IncreaseOverride").Apply(mainThrusters);
    }
    }
    break;
    }


    ////////// Decrease the thrust after the timer is done counting.
    for (int i = 0; i < mainThrusters.Count; i++)
    {
    for(int j = 0; j < 20; j++)
    {
    mainThrusters.GetActionWithName("DecreaseOverride").Apply(mainThrusters);
    }
    }
    }



    I'm more used to java than I am used to C#. They are quite similar, but there are some subtle differences.

    Here is what I'm looking at.

    <div>Timer Block
    Interface name: IMyTimerBlock
    Parent: IMyFunctionalBlock
    Fields:
    bool IsCountingDown
    float TriggerDelay
    Actions:
    OnOff -> Toggle block On/Off
    OnOff_On -> Toggle block On
    OnOff_Off -> Toggle block Off
    IncreaseTriggerDelay -> Increase Delay
    DecreaseTriggerDelay -> Decrease Delay
    TriggerNow -> Trigger now
    Start -> Start
    Stop -> Stop

    I assume that the bool IsCountingDown under the fields there is something that I can use like a boolean variable. I'm trying to use it in that while-loop that I have in my code, and it's giving me an error.

    Obviously, i'm doing something wrong. Am I wrong to assume that the bool is a usable variable in this context?

    How do I make these thrusters fire only while the timer is counting?

    Thanks for any help I can get!</div>
     
  11. pipakin Apprentice Engineer

    Messages:
    182
    Referring to the new question re: timers.

    Two problems, one, it's going to loop so many times that it will hit the complexity limit. You can't delay in the loop.

    Two, you need to cast timer to IMyTimerBlock.

    Code:
    var theTimer = (IMyTimerBlock)timer;
    
     
  12. Abyzmi Trainee Engineer

    Messages:
    66
    Thanks Pipakin. I'll cast the timer block.

    Hoping I can find a way to time this thruster. I suppose I could always make a seperate programmable block, but I'd rather do all of this in one shot. The in-game structure that i'm trying to apply this script to is a small drone, and I'd like to keep it small if I can.

    Should I write it into a seperate method, and then call that method down into the main method when the timer is finished, and then have the thrusters fire before I begin the timer, excluding the while-loop? Is that even possible? :woot:
     
  13. Abyzmi Trainee Engineer

    Messages:
    66
    But actually, the timer block starts and runs just fine... what's the point of casting the timer to IMyTimerBlock?
     
  14. pipakin Apprentice Engineer

    Messages:
    182
    To access the IsCountingDown member it needs to be of type IMyTimerBlock.

    The problem is you can't really do it that way. The way I would do it is:

    Timer block at resolution 1 sec.
    Private variable shouldIncrease (default to true).
    Private Variable numSeconds (default to time you want to increase).
    Private variable currentSeconds (default to 0)

    every time through main method:
    if shouldIncrease is true, increase. otherwise decrease.
    increment currentSeconds
    if(currentSeconds >= numSeconds):
    - if shouldIncrease is true, set shouldIncrease to false and currentSeconds to 0
    - otherwise set shouldIncrease to true, currentSeconds to 0 and stop the timer.

    set the timer actions to: 1 - start iself, 2 - run your script.

    to initiate script, start the timer.
     
  15. Abyzmi Trainee Engineer

    Messages:
    66
    Thanks Pipa. I'll give it another look.
     
  16. Abyzmi Trainee Engineer

    Messages:
    66
    Thanks again for all of your help! However, once again, I'm sort of at a loss, here. I've almost got it, but i'm just not quite there...
    Here's what we've got:

    void Main()
    {

    //////////// Put the main thrusters into a list by grouping.

    var list = GridTerminalSystem.BlockGroups;
    IMyBlockGroup group = null;
    for (int i = 0; i &lt; list.Count; i++)
    {
    if (list.Name == "Thrusters")
    {
    group = list;
    break;
    }
    }

    List&lt;IMyTerminalBlock> mainThrusters = group.Blocks;

    //////////// Initialize the timer block and set up actions

    var timer = GridTerminalSystem.GetBlockWithName("Timer Block");
    ITerminalAction startTimer = timer.GetActionWithName("Start");
    ITerminalAction stopTimer = timer.GetActionWithName("Stop");

    /////////// Allow the timer block to operate for 20 seconds.

    bool shouldIncrease = true;
    int numSeconds = 20;
    int currentSeconds = 0;


    if (shouldIncrease == true)
    {
    for(int i = 0; i &lt; mainThrusters.Count; i++)
    {
    for(int j = 0; j &lt; 20; j++)
    {
    mainThrusters.GetActionWithName("IncreaseOverride").Apply(mainThrusters);
    }
    }
    }
    else
    {
    for (int i = 0; i &lt; mainThrusters.Count; i++)
    {
    for(int j = 0; j &lt; 20; j++)
    {
    mainThrusters.GetActionWithName("DecreaseOverride").Apply(mainThrusters);
    }
    }
    }
    currentSeconds++;

    if (currentSeconds >= numSeconds)
    {
    if (shouldIncrease == true)
    {
    shouldIncrease = false;
    currentSeconds = 0;
    }
    else
    {
    shouldIncrease = true;
    currentSeconds = 0;
    stopTimer.Apply(timer);
    }
    }
    }

    The code runs, the timer begins counting, and the thrusters flare up. The only problem is that they do not stop after 20 seconds.
    Any help is much appreciated! :D
     
  17. Abyzmi Trainee Engineer

    Messages:
    66
    Alright, so!

    http://steamcommunity.com/sharedfiles/filedetails/?id=368878242

    This is the end-result of what I was building.

    I couldn't figure it out the way I was doing it so I just used multiple programmables and some extra timer blocks.

    Seems to work now!

    Thanks again for all the help!
     
  18. Icedmarker21 Trainee Engineer

    Messages:
    13
    Hey guys!
    I'm also trying to use a programmable block to control thrusters but i'm having issues.
    My objective is simply to set a Thrust Override value while displaying it on a text panel with this code:

    void Main(string argument)
    {
    IMyTextPanel display = (IMyTextPanel)GridTerminalSystem.GetBlockWithName("LCD Panel");
    display.WritePublicText("...");

    List<IMyTerminalBlock> Fthrusters = new List<IMyTerminalBlock>();
    GridTerminalSystem.SearchBlocksOfName("%00F", Fthrusters);
    display.WritePublicText("" + Fthrusters.Count + "\n");

    for (int i = 0; i < Fthrusters.Count; i++) {
    var thruster = Fthrusters as IMyThrust;
    display.WritePublicText(" " + thruster.ThrustOverride, true);
    thruster.SetValueFloat("Override", 7200.0F);
    }

    }​

    It seems to be working, but the code seems to be overriding to the max value of the thrusters
     
  19. n1ghty Trainee Engineer

    Messages:
    34
    Im pretty sure Override is set in % of max Thrust now. So you'll need to set it to a value in [0;100]
     
  20. Icedmarker21 Trainee Engineer

    Messages:
    13
    Woooow, I'd never figure that out by myself! When you display the value of ThrustOverride it shows in newtons.
    It worked great! Thanks!!
     
Thread Status:
This last post in this thread was made more than 31 days old.