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.

Help with "MyTextPanel"

Discussion in 'Programming Questions and Suggestions' started by Spaceman Spiff, Mar 22, 2020.

Thread Status:
This last post in this thread was made more than 31 days old.
  1. Spaceman Spiff Senior Engineer

    Messages:
    2,007
    I have an LCD that displays text from one of my planetary miners (after getting help from someone quite some time ago). Here's the now-offending line of code:

    IMyTextPanel Display = GridTerminalSystem.GetBlockWithName("BCM - Corner LCD Top") as IMyTextPanel;

    Evidently "MyTextPanel" is now obsolete. Would someone please help me with its replacement?
     
  2. Malware Master Engineer

    Messages:
    9,862
    That's not the obsolete part. Most likely it's WritePublicText, which you need to replace with simply WriteText.
     
  3. Spaceman Spiff Senior Engineer

    Messages:
    2,007
    Thanks. I did what you said and it said it compiled OK, but running the script throws an exception. Here's the entire script:

    Code:
    void Main()
    {
      IMyTextPanel Display = GridTerminalSystem.GetBlockWithName("BCM - Corner LCD Top") as IMyTextPanel;
      IMyMotorStator Rotor = GridTerminalSystem.GetBlockWithName("BCM - Advanced Rotor Left") as IMyMotorStator;
    
      float AngleInRadians = Rotor.Angle;
      float AngleInDegrees = MathHelper.ToDegrees(AngleInRadians);
      string AngleAsString = string.Format("{0:0}", AngleInDegrees);
    
      if(AngleInDegrees > 180)
    	  { 
    		  AngleInDegrees = AngleInDegrees - 360;
    		  AngleAsString = string.Format("{0:0}", AngleInDegrees);
    	  } 
    Display.WriteText(" Drill Angle: "+AngleAsString+"ยบ");
    }
    The script it used with my Blue Crab Miner, a planetary rover with drill arms on rotors. Drill arm angle feedback helps the player determine the best descent angle when mining for ore.

    I really need to learn how to code...
     
  4. Malware Master Engineer

    Messages:
    9,862
    What exception?
     
  5. Spaceman Spiff Senior Engineer

    Messages:
    2,007
    I knew you were going to ask that, so why didn't I show you to begin with? {Slaps head!}

    I read it over three times to capture it verbatim. Here's what it says:

    Code:
    Caught exception during execution of script:Object reference not set to an instance of an object.
    
      at Program.Main()
      at Sandbox.Game.Entities.Blocks.MyProgrammableBlock.<>c__DisplayClass46_0.<ExecuteCode>b__0(IMyGridProgram
      program
    
      at Sandbox.Game.Entities.Blocks.MyProgrammableBlock.RunSandboxedProgramAction(Action`1
      action,
      String&
      response)
     
  6. Malware Master Engineer

    Messages:
    9,862
    Most likely you have a typo or something and the script isn't finding one or more of the blocks. The name is case sensitive, and you even gotta watch leading and trailing whitespace.
     
  7. Spaceman Spiff Senior Engineer

    Messages:
    2,007
    OK, I'll check again. But I spawned in what used to work just fine. Gr-r-r-r-r-r...
    --- Automerge ---
    Dang! Yeah, I had started renaming components because it was a new version, but never finished. As fate would have it, of the two names given in that little snippet of code, one was new (and therefore wrong in the code) an the other was old and correct per the code. When I fixed everything, it worked. Go figure.

    Thanks for helping dummies like me.
     
  8. Malware Master Engineer

    Messages:
    9,862
    Add a null-check to your code:

    Code:
    IMyTextPanel Display = GridTerminalSystem.GetBlockWithName("BCM - Corner LCD Top") as IMyTextPanel;
    if (Display == null)
    	throw new Exception("Couldn't find the display");
    
    IMyMotorStator Rotor = GridTerminalSystem.GetBlockWithName("BCM - Advanced Rotor Left") as IMyMotorStator;
    if (Rotor == null)
    	throw new Exception("Couldn't find the rotor");
    
     
  9. Spaceman Spiff Senior Engineer

    Messages:
    2,007
    Great suggestion. Thanks!
     
Thread Status:
This last post in this thread was made more than 31 days old.