Jump to content

String-to-Numeric in .cfg file


Recommended Posts

I am trying to write a custom .cfg that uses B9PartSwitch to allow changing all the stock LFO fuel tanks to liquid fuel only. I cannot find documentation for how to change a previously-loaded .cfg (such as when to use @, +, |, etc), and if you can point me to documentation I would appreciate it, but I think I have enough of that figured out for my purposes.

The problem I am having that I cannot seem to work around comes when I go to get the amounts of liquid fuel and oxidizer in the part, then add them together. The way I am accessing that data it is returned as a string, and trying to add the two strings together or divide them by 0.45 or whatever causes a System.FormatException that forces the game to quit while it is loading when it gets to the first part I've specified.

A minimum working (or failing, rather) example follows:

@PART[fuelTankSmallFlat]:AFTER[B9PartSwitch]
{
  MODULE {
    // superfluous information redacted
    LFamount = #$/RESOURCE[LiquidFuel]/amount$
    Oxamount = #$/RESOURCE[Oxidizer]/amount$
    baseVolume = LFamount / .45 // this crashes because LFamount is a string
    baseVolume = LFamount + Oxamount // this crashes too because both are strings
    // superfluous information redacted
  }
}

I don't even know what language this is in (is it C++?), but what I think I need is a way to coerce a data type change, like I would in R, 

as.numeric(LFamount)

Thank you for your assistance.

(And yes, I am aware of the existence of Interstellar Fuel Switch and Simple Fuel Switch. I want a) something that works with B9PartSwitch, which I already have installed, and b) to learn how to do this myself.)

Link to comment
Share on other sites

I got it to work, but I don't why it works when the other method didn't. I'm sure it has something to do with type conversion in C#. Any explanation would be appreciated.

Full .cfg is reproduced below, should anyone want to use it themselves.

//// Allows switching between LFO and LF-only for all stock, Making History, and Missing History fuel tanks
////   Requires B9PartSwitch
////   (c) 2019 by Chad Tucker under the MIT Licence

// stock parts of type LFO
//  note: Size3To2Adapter_v2 is a version 1.6 part
@PART[fuelTankSmallFlat|fuelTankSmall|fuelTank|fuelTank_long|Rockomax8BW|Rockomax16_BW|Rockomax32_BW|
Rockomax64_BW|Size3SmallTank|Size3MediumTank|Size3LargeTank|fuelTank3-2|miniFuelTank|toroidalFuelTank|
fuelTank4-2|fuelTank2-2|fuelTank1-2|adapterMk3-Mk2|adapterMk3-Size2Slant|adapterSize2-Mk2|adapterSize2-
Size1|adapterSize2-Size1Slant|adapterSize3-Mk3|toroidalFuelTank|externalTankCapsule|externalTankRound|
externalTankToroid|mk2_1m_Bicoupler|mk2_1m_AdapterLong|mk2SpacePlaneAdapter|Size3To2Adapter_v2]:AFTER
[B9PartSwitch]
{
  MODULE {
    name=ModuleB9PartSwitch
    moduleID=fuelSwitch
    baseVolume = #$/RESOURCE[LiquidFuel]/amount$
    @baseVolume += #$/RESOURCE[Oxidizer]/amount$
    switcherDescription = Fuel Type
    switcherDescriptionPlural = Fuel Types
    switchInFlight = FALSE

    SUBTYPE {
      name = LF/Ox
      tankType = LFO
      allowSwitchInFlight = FALSE
    }

    SUBTYPE {
      name = Liquid Fuel
      tankType = LF
      allowSwitchInFlight = FALSE
    }  
  }
}

// stock parts of type LF-only (could add spaceplane parts if desired)
@PART[miniFuselage]:AFTER[B9PartSwitch]
{
  MODULE {
    name=ModuleB9PartSwitch
    moduleID=fuelSwitch
    baseVolume = #$/RESOURCE[LiquidFuel]/amount$
    switcherDescription = Fuel Type
    switcherDescriptionPlural = Fuel Types
    switchInFlight = FALSE

    SUBTYPE {
      name = Liquid Fuel
      tankType = LF
      allowSwitchInFlight = FALSE
    }  

    SUBTYPE {
      name = LF/Ox
      tankType = LFO
      allowSwitchInFlight = FALSE
    }
  }
}

// Making History parts of type LFO
@PART[Size1p5_Size0_Adapter_01|Size1p5_Size1_Adapter_01|Size1p5_Size1_Adapter_02|
Size1p5_Size2_Adapter_01|Size1p5_Tank_01|Size1p5_Tank_02|Size1p5_Tank_03|Size1p5_Tank_04|
Size1p5_Tank_05|Size3_Size4_Adapter_01|Size4_EngineAdapter_01|Size4_Tank_01|Size4_Tank_02|Size4_Tank_03|
Size4_Tank_04]:NEEDS[SquadExpansion/MakingHistory]:AFTER[B9PartSwitch]
{
  MODULE {
    name=ModuleB9PartSwitch
    moduleID=fuelSwitch
    baseVolume = #$/RESOURCE[LiquidFuel]/amount$
    @baseVolume += #$/RESOURCE[Oxidizer]/amount$
    switcherDescription = Fuel Type
    switcherDescriptionPlural = Fuel Types
    switchInFlight = FALSE

    SUBTYPE {
      name = LF/Ox
      tankType = LFO
      allowSwitchInFlight = FALSE
    }

    SUBTYPE {
      name = Liquid Fuel
      tankType = LF
      allowSwitchInFlight = FALSE
    }  
  }
}

// Missing History parts
@PART[Size1p5_Size2_Adapter_02|adapterSmallMiniTall|Size1p5_Size2_Adapter_02|Size3_Size2_Tank]:NEEDS
[MissingHistory]:AFTER[B9PartSwitch]
{
  MODULE {
    name=ModuleB9PartSwitch
    moduleID=fuelSwitch
    baseVolume = #$/RESOURCE[LiquidFuel]/amount$
    @baseVolume += #$/RESOURCE[Oxidizer]/amount$
    switcherDescription = Fuel Type
    switcherDescriptionPlural = Fuel Types
    switchInFlight = FALSE

    SUBTYPE {
      name = LF/Ox
      tankType = LFO
      allowSwitchInFlight = FALSE
    }

    SUBTYPE {
      name = Liquid Fuel
      tankType = LF
      allowSwitchInFlight = FALSE
    }  
  }
}

 

Link to comment
Share on other sites

11 minutes ago, MrSystems said:

I don't why it works when the other method didn't

Well, your initial example said "MODULE" but never specified "name=ModuleB9PartSwitch", might that be it?  Or is that in the redacted superfluous stuff?

Also, your initial config specified "baseVolume" twice, which is a no-no.  You can have it once, but then after that you need the @ notation to indicate "and now I'm editing an already-existing value rather than inserting a new one".

Also you can't say "baseVolume = LFamount", because you're trying to set it equal to the literal string "LFamount".  What you're trying to do is to set it to the value of the LFAmount field, which means you should set it to "#$LFamount$" instead-- that way MM knows you have a variable reference rather than a literal string.

Link to comment
Share on other sites

1 minute ago, Snark said:

Also you can't say "baseVolume = LFamount", because you're trying to set it equal to the literal string "LFamount".  What you're trying to do is to set it to the value of the LFAmount field, which means you should set it to "#$LFamount$" instead-- that way MM knows you have a variable reference rather than a literal string.

That was the explanation I needed. Thank you.

Link to comment
Share on other sites

If anyone wants to try this at home, I suggest the following two modifications to the code I posted above:

The default settings for B9PartSwitch add additional mass and cost to parts based on their volume, but the stock part configs already account for this. With the default B9 configs, a Rockomax X200-16 ends up with a wet mass of 9.4t and a wet cost of $2,674.40 instead of the stock stats of 9.0t and $1,550.

So, 1) add this to the top of the file:

B9_TANK_TYPE{
  name=stockLFO
  tankMass=0 // this adds to part mass, which is why it was heavier with default configs
  tankCost=0 // this adds to part cost, which is why it was more expensive with default configs
  RESOURCE {
    name = LiquidFuel
    unitsPerVolume = 0.55
  }
  RESOURCE {
    name = Oxidizer
    unitsPerVolume = 0.45
  }
B9_TANK_TYPE{
  name=stockLF
  tankMass=0 
  tankCost=0 
  RESOURCE {
    name = LiquidFuel
    unitsPerVolume = 1
  }
}

And 2) in each of the SUBTYPE calls below, change `tankType = LF` to `tankType = stockLF` and `tankType = LFO` to `tankType = stockLFO`.  This way it won't break the stats on the stock parts or any mods you're running.

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...