Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions Source/Modules/ModuleCoPFollowTransform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,42 @@ public class ModuleCoPFollowTransform : PartModule
[KSPField]
public string transformName;

[SerializeField]
private Transform followTransform;

public override void OnLoad(ConfigNode node)
{
base.OnLoad(node);
if (HighLogic.LoadedScene != GameScenes.LOADING)
if (followTransform == null || followTransform.name != transformName)
{
if (transformName != null) followTransform = part.FindModelTransform(transformName);
if (followTransform == null) Debug.LogError($"[{MODULENAME}] transformName was empty or does not exist.");
if (followTransform == null) Debug.LogError($"[{MODULENAME}] transformName '{transformName}' was empty or does not exist.");
}

if (followTransform == null)
{
// this may be important if someone is swapping out versions of this module with B9PS
part.CoPOffset = Vector3.zero;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes sense but I feel like we might not want to set it to zero but to whatever it was in the part config itself, assuming there's a good way to get this at this point.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This actually isn’t a KSPField so can’t be configured in the cfg. But maybe grabbing the value from the prefab is better

Copy link
Collaborator Author

@SofieBrink SofieBrink Oct 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh i swear i’ve seen it configured in the partconfig before, but the prefab should be exactly that i guess.

edit: yeah there’s like a bajillion parts on github here that define a CoPOffset in the part cfg. I assume that works.

Copy link
Contributor

@JonnyOThan JonnyOThan Oct 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see anything in Assembly-CSharp that would set it from the cfg. Maybe it's in FirstPass? Maybe it's done by reflection somehow.

Oooh yeah it is reflection. Didn't realize parts did that. Any top-level key in the PART cfg will bind to a public field on the Part class even if it doesn't have [KSPField] or [Persistent]

}

// NOTE: isEnabled will be persisted to the save file, but we want to treat it purely as runtime state
this.isEnabled = followTransform != null;
this.enabled = followTransform != null && HighLogic.LoadedSceneIsFlight;
}

public void FixedUpdate()
{
if (followTransform != null) part.CoPOffset = part.transform.InverseTransformPoint(followTransform.position);
// Note that we shouldn't ever get here if the transform just didn't exist
// But it's certainly possible for *something* to delete it later
if (followTransform == null)
{
isEnabled = false;
enabled = false;
// should we reset CoPOffset here? Not sure.
}
else
{
part.CoPOffset = part.transform.InverseTransformPoint(followTransform.position);
}
}
}
}