Jump to content

(SOLVED) Part field value in editor


Recommended Posts

Hi everyone.

I'm trying to retrieve a value of a part, used in an edited ship

For example, i'm trying here to retrieve CURRENT 'deployAltitude' value of the 'parachuteSingle' part, used in the currently edited ship (VAB):

Notes: GetVesselPart and DebugFunctions.SendLog are working.

public static string GetAttribute(string partName, string parameterName) {
    Part part = GetVesselPart(partName);
    string value;
    foreach ( PartModule module in part.Modules ) {
        foreach ( BaseField field in module.Fields ) {
            DebugFunctions.SendLog("Checking field " + field.name);
            if ( field.name == parameterName ) {
                value = (string)field.GetValue(parameterName);
                DebugFunctions.SendLog("Parameter found : actual string -> " + value);
                return value;
            }
        }
    }
    DebugFunctions.SendLog("Parameter " + parameterName + " NOT found");
    return null;
}

Used values in this example:

partName = "parachuteSingle"

parameterName = "deployAltitude"

Here's produced log:

[LOG 16:03:29.929] [7]Checking field invertCanopy.
[LOG 16:03:29.929] [7]Checking field semiDeployedAnimation.
[LOG 16:03:29.930] [7]Checking field fullyDeployedAnimation.
[LOG 16:03:29.930] [7]Checking field autoCutSpeed.
[LOG 16:03:29.930] [7]Checking field rotationSpeedDPS.
[LOG 16:03:29.931] [7]Checking field capName.
[LOG 16:03:29.931] [7]Checking field canopyName.
[LOG 16:03:29.931] [7]Checking field persistentState.
[LOG 16:03:29.932] [7]Checking field stowedDrag.
[LOG 16:03:29.932] [7]Checking field semiDeployedDrag.
[LOG 16:03:29.932] [7]Checking field fullyDeployedDrag.
[LOG 16:03:29.933] [7]Checking field animTime.
[LOG 16:03:29.933] [7]Checking field clampMinAirPressure.
[LOG 16:03:29.933] [7]Checking field minAirPressureToOpen.
[LOG 16:03:29.934] [7]Checking field deployAltitude.
[ERR 16:03:29.934] Value could not be retrieved from field 'deployAltitude'

[LOG 16:03:29.934] [7]Parameter found : actual string -> .
[EXC 16:03:29.935] ArgumentNullException: Argument cannot be null.

I can't find the good method to get value of this parameter.

I tried field.GetStringValue without success.

Have you any idea how to do this?

Thank you

Edited by jim1240
Solved topic
Link to comment
Share on other sites

I can't find another method to get actual parameter of a part in editor.

This method is working but i'm thinking it's actually a bit too complicated.

If you've a simpler way to do this, i'll use it with enthusiasm.

I tried:

  • Part.partInfo (not actual parameter)
  • Part.internalModel.internalConfig (internal config)

Actual code:

public static string GetAttribute(string partName, string parameterName) {
    Part part = VesselFunctions.GetVesselPart(partName);
    string value;
    foreach ( PartModule module in part.Modules ) {
    	value = module.Fields.GetValue(parameterName).ToString();
        DebugFunctions.SendLog("Parameter " + parameterName + " found: " + value);
        return value;
    }
    DebugFunctions.SendLog("Parameter " + parameterName + " NOT found");
	return null;
}

Thanks you for answer

Link to comment
Share on other sites

If you're just querying stock modules or other classes you have a hard reference to

ModuleParachute parachute = part.Modules.GetModule<ModuleParachute>();
if (parachute != null)
{
  Debug.Log($"parachute deployment altitude is: {parachute.deployAltitude}");
}

as for getting a Part reference from AvailablePart (the thing you normally have in the editor), you want partPrefab

Edited by Crzyrndm
Link to comment
Share on other sites

It's for stock modules for the moment, but it will be also for custom modules and parameters. So effectively, i can give module name to the function for stock parts, it will be simpler. I'll try partPrefab too, maybe interesting.

Thanks you

Link to comment
Share on other sites

The way I've started doing this is using the Dictionary function of the Fields list. I suspect it is simply passing to GetValue and SetValue in the background anyway, but it seems to be how Squad set things up for us. Note this is essentially what was posted already, just compressed down into one line.

Part.Modules["MyPartModule"].Fields.SetValue("MyField", true);

This finds the first partModule named "MyPartModule" on the part and sets the value of "MyField" to true. The hard limitation is that this will only work if there is only one module of that type on the part, it can't handle multiple modules.

If you have multiple modules of the same type, I do the following, again pretty much the same as posted already but the best way I've found to handle things:

foreach (PartModule pm2 in part.Modules)
{
 if (pm2.moduleName == "FARControllableSurface")
 {
   float curVal = (float)pm2.Fields.GetValue("yawaxis");
   sp = curVal + sp;
   pm2.Fields.SetValue("yawaxis", sp);
 }
}

Here I am interfacing with FAR and changing the Yaw axis limit on the control surface by adding a float value "sp" to the current yawaxis limit.

In neither case do I need a reference set, these methods work for both stock and other mods.

D.

Edited by Diazo
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...