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.

Need a second set of eyes - Coast/Cruise Control

Discussion in 'Programming (In-game)' started by Pontiac, May 2, 2020.

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

    Messages:
    109
    When I'm out in space, to conserve fuel and maintain a speed, I typically turn off all of my front thrusters so they don't slow me down. I don't like turning off the dampeners and cruise like that, because if I get too off course, I end up blowing through too much steam/fuel to slow down and change where I'm pointing.

    What I I thought I'd do is use some code that's provided by http://dco.pe/vsb/ and modify it. However, I'm having a bad time with the modifications.

    The code I'm given from that site:
    //x3,1,,52[7]0[5]%,y,,1.1.1
    // Above is your LOAD LINE. Copy it into Visual Script Builder to load your script.
    // dco.pe/vsb

    void Main(string argument)
    {
    // block declarations
    string ERR_TXT = "";
    List<IMyTerminalBlock> v0 = new List<IMyTerminalBlock>();
    GridTerminalSystem.GetBlocksOfType<IMyThrust>(v0, filterThis);
    if(v0.Count == 0) {
    ERR_TXT += "no Small Hydrogen Thruster blocks found\n";
    }

    // display errors
    if(ERR_TXT != "") {
    Echo("Script Errors:\n"+ERR_TXT+"(make sure block ownership is set correctly)");
    return;
    }
    else {Echo("");}

    // logic
    for(int i = 0; i < v0.Count; i++) {
    v0.ApplyAction("OnOff");
    }
    }

    bool filterThis(IMyTerminalBlock block) {
    return block.CubeGrid == Me.CubeGrid;
    }


    This works. ALL the engines turn on and off, both large and small atmos and hydrogen because this script isn't exactly depicting the correct type I want. Regardless, working off that script, I start adding some additional logic. Only select thrusters pointing forward, show me what the first valid thruster is, tell me what its changing... well.. you'll see... here's the script:


    void Main(string argument) {
    // block declarations
    string ERR_TXT = "";
    List<IMyTerminalBlock> v0 = new List<IMyTerminalBlock>();
    GridTerminalSystem.GetBlocksOfType<IMyThrust>(v0, filterThis);
    if(v0.Count == 0) {
    ERR_TXT += "no Small Hydrogen Thruster blocks found\n";
    }

    // user variable declarations

    // display errors
    if(ERR_TXT != "") {
    Echo("Script Errors:\n"+ERR_TXT+"(make sure block ownership is set correctly)");
    return;
    }
    else {Echo("");}

    // logic
    int ThrustIdx = -1;
    bool ThrustOn = false;
    string ThrustState = "";
    for(int i = 0; i < v0.Count; i++) {
    if (v0.Orientation.Forward.ToString() == "Backward") {
    if (ThrustIdx == -1) {
    Echo("Setting IDX to "+i.ToString());
    ThrustIdx = i;
    ThrustOn = (bool)((IMyThrust)v0[ThrustIdx]).Enabled;
    ThrustOn = !ThrustOn;
    }
    Echo ("Index "+i.ToString()+" - "+v0.DefinitionDisplayNameText.ToString());
    Echo ("Toggling Thrust to "+ThrustOn.ToString());
    if (ThrustOn == false) {
    ThrustState="OnOff_Off";
    } else {
    ThrustState="OnOff_On";
    }
    //((IMyThrust)v0).Enabled=ThrustOn;
    v0.ApplyAction(ThrustState);
    Echo (ThrustState.ToString()+"-"+((IMyThrust)v0).Enabled.ToString());
    }
    }
    }

    bool filterThis(IMyTerminalBlock block) {
    return block.CubeGrid == Me.CubeGrid;
    }


    Whats happening for me is that when I'm in the control panel, on the programmable block, when I run the script I see that the objects get toggled. The ApplyAction does take, seemingly, because the Echo blow shows what variable I'm passing in, PLUS what the actual thrusters running state is. The Enabled line above the ApplyAction produces the same results. I see the details change, but, the ship doesn't react. The engines don't shut down, it still things its got the momentum. Anyone have any suggestions on how to get this thing to work? I don't think it's the SE engine, just something I'm not seeing....
     
  2. Wicorel Senior Engineer

    Messages:
    1,262
    Why it doesn't work:

    You're using grid orientation. This is, effectively, random.

    You need to find the thrusters that match the orientation of a reference block (usually a cockpit)

    The code I use is here:
    https://github.com/Wicorel/WicoSpac...dular/WicoThrust/WicoThrust/WicoThrust.cs#L87


    Optimizations:
    1) Don't use Actions unless there is no other option. (use .Enabled=true/false)
    2) Move the 'getting' of the lists of blocks to the constructor
    https://github.com/malware-dev/MDK-SE/wiki/Do's-and-Don'ts
     
  3. Pontiac Apprentice Engineer

    Messages:
    109
    Thanks for the links. I'll take a look over your code to get some better understanding.

    That said, in the 2nd version of my script, when I used the .Enabled=true/false (And not the ApplyAction), the result is exactly the same. The engines just do not toggle on or off, they maintain state. If I set half the reverse thrust engines off manually, they stay in the same state and the half that are on still burn. This is the part that's confusing me. The debug statement shows the activity that's supposed to be happening AFTER either the .Enabled or .ApplyAction is performed, and it does show the correct toggle, but the engines itself don't take that reported state.

    Up front, I did just read the Dos and Don'ts, and I understand it. However.....

    Outside the theory of best practices (And without reading your code yet), I can't see how a dynamic grid can cope with a set of running engines when the scan on the grid is done only at the PG initialization stage (Game Load/Recompile). Any time I'd add or remove an engine (By my grinder, or some drone bullets), I'd have to go back to the PG and recompile to get the list updated. If I add engines, and don't compile, then I'd still end up having forward engines powered on.

    What am I missing in my understanding of compiling the list of blocks?
     
  4. Wicorel Senior Engineer

    Messages:
    1,262
    You can build your code to get lists and call it from the constructor.

    You can then call this code at any time; like if you 'detect' grid changes or receive an argument telling you to...
     
Thread Status:
This last post in this thread was made more than 31 days old.