Author Topic: altering inline function  (Read 1819 times)

Incurable-Rash

  • Full Member
  • ***
  • Posts: 181
altering inline function
« on: May 01, 2020, 01:27:36 PM »
I keep getting a syntax error, can't figure out what I am missing.

using System;

public class VAInline
{
   [Pips]
   enum status : uint
   {
       Balanced Power = 444,
   }
   
   public void main()
   {
      string statusInfo = VA.GetText("statusPips") ?? "";
      
      int pipsSectionStart = statusInfo.IndexOf("\"Pips)"+ 8;
      if (pipsSectionStart == 7)
      {
         VA.WriteToLog("Error getting status pips: '\"Pips:\"' section not found", "red");
         VA.SetText("statusPips", "");
         return;
      }
      int pipsSectionEnd = statusInfo.IndexOf(',', pipsSectionStart);
      
      string pipsNumber = statusInfo.Substring(pipsSectionStart, pipsSectionEnd - pipsSectionStart);
      
      int pipsInt;
      Int32.TryParse(pipsNumber, out pipsInt);
      status pips = (status)pipsInt;
      
      VA.SetText("statusPips", pips.ToString());
   }
}

Gary

  • Administrator
  • Hero Member
  • *****
  • Posts: 2832
Re: altering inline function
« Reply #1 on: May 01, 2020, 02:16:48 PM »
I'm not going to give it away - starting at the top and going down, do you see anything out of place?

Incurable-Rash

  • Full Member
  • ***
  • Posts: 181
Re: altering inline function
« Reply #2 on: May 01, 2020, 10:22:49 PM »
After 2 hours of looking,
I did find a error, but it looks like the compiler is still saying it needs a ", " on line 8 but it already has one and I can't figure out what it needs or is missing. The Pips flag in the ED status.json file has the numbers separated by commas ie 4,4,4, do the commas need to be entered as well.
One other question if the compiler has some of the lines in red does that mean there is a problem in those lines as well?
This is like trying to understand Greek and Chinese all at the same time.

Gary

  • Administrator
  • Hero Member
  • *****
  • Posts: 2832
Re: altering inline function
« Reply #3 on: May 01, 2020, 10:33:17 PM »
Remove '[Pips]'

'Balanced Power' should be 'BalancedPower' (no space)

int pipsSectionStart = statusInfo.IndexOf("\"Pips)"+ 8;
should be
int pipsSectionStart = statusInfo.IndexOf("\"Pips")+ 8;

It compiles like this - not sure if it does what you want it to do - I'll leave that up to you ;)

Also, if you double-click on the log message, it will take you to the line where the error is located (just a tip).



Code: [Select]
public class VAInline
{
   enum status : uint
   {
       BalancedPower = 444,
   }
   
   public void main()
   {
      string statusInfo = VA.GetText("statusPips") ?? "";
     
      int pipsSectionStart = statusInfo.IndexOf("\"Pips") + 8;
     
      if (pipsSectionStart == 7)
      {
         VA.WriteToLog("Error getting status pips: '\"Pips:\"' section not found", "red");
         VA.SetText("statusPips", "");
         return;
      }
      int pipsSectionEnd = statusInfo.IndexOf(',', pipsSectionStart);
     
      string pipsNumber = statusInfo.Substring(pipsSectionStart, pipsSectionEnd - pipsSectionStart);
     
      int pipsInt;
      Int32.TryParse(pipsNumber, out pipsInt);
      status pips = (status)pipsInt;
     
      VA.SetText("statusPips", pips.ToString());
   }
}

Pfeil

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4782
  • RTFM
Re: altering inline function
« Reply #4 on: May 01, 2020, 10:36:14 PM »
Note that you'll likely want to replace "[Pips]" (which is meaningless) with "[Flags]", as it was in the original code, which indicates to the compiler that you want the list of flag names when "ToString()" is called, rather than the underlying number.

Incurable-Rash

  • Full Member
  • ***
  • Posts: 181
Re: altering inline function
« Reply #5 on: May 02, 2020, 08:59:25 AM »
This is what is in the status.json file

{ "timestamp":"2020-05-02T11:40:00Z", "event":"Status", "Flags":153157645, "Pips":[2,8,2], "FireGroup":0, "GuiFocus":5, "Fuel":{ "FuelMain":64.000000, "FuelReservoir":1.160000 }, "Cargo":728.000000, "LegalState":"Clean", "Latitude":-69.186638, "Longitude":86.546265, "Heading":0, "Altitude":0 }

if the original code uses Flags, to find where to match from the list, if I were to expand the list to indicate what the different number string mean would that met the requirement?
I am trying to have it know which system is using what pips and how many are being used? Or am I still missing what you are trying to tell me.
Maybe this is not the best system to use for what I want?

and nt pipsSectionStart = statusInfo.IndexOf("\"Pips")+ 8; is the error I did find in the 2 hours.

Either way thanks, I am trying to learn, albeit in a backwards way, but at my age its not easy.

 

Pfeil

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4782
  • RTFM
Re: altering inline function
« Reply #6 on: May 02, 2020, 03:27:14 PM »
As "Pips" is apparently an array of integer values, rather than an integer value representing enumerated flags, enum is not likely to be the correct way to decode that information.

If you want the individual values in that group, aside from implementing a proper JSON deserializer, you could use the demonstrated technique for getting part of a string to get the section between square brackets containing the data, then use String.Split() to put the individual values into a string array, after which you can convert the string values to integer in a foreach() loop.

Incurable-Rash

  • Full Member
  • ***
  • Posts: 181
Re: altering inline function
« Reply #7 on: May 02, 2020, 07:36:56 PM »
I am off the see if I can figure out a JSON deserializer

Is there a demonstrated technique for getting part of a string to get the section between square brackets containing the data
That I don't remember?

Pfeil

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4782
  • RTFM
Re: altering inline function
« Reply #8 on: May 02, 2020, 07:40:51 PM »
Is there a demonstrated technique for getting part of a string to get the section between square brackets containing the data
That I don't remember?

...It's in the example you copied, getting the position of a starting and ending string/character using IndexOf(), then using SubString() to get the text between those two positions.