Bug #2422
Fix within: Incorrect copying of fields in ProtoPartSnapshot.Load(Vessel vesselRef, bool loadAsRootPart).
100%
Description
When initiating a vessel this method copies the root part with all it's modules and contents to the vessel gameObject
.
There's an error in the way the fields are copied for both the Part
and all PartModule
enclosed within.
Currently it looks like this:
Part part = vesselRef.gameObject.AddComponent(this.partRef.GetType()) as Part; FieldInfo[] fields = this.partRef.GetType().GetFields(); for (int i = 0; i < fields.Length; i++) { FieldInfo fieldInfo = fields[i]; if (fieldInfo.IsPublic) { fieldInfo.SetValue(part, fieldInfo.GetValue(this.partRef)); } }
Which isn't correct. It will copy static fields, and attempt to copy constant fields too resulting in exceptions. It also misses out on non-public fields which really should get copied.
A much better way would be:
Part part = vesselRef.gameObject.AddComponent(this.partRef.GetType()) as Part; FieldInfo[] fields = this.partRef.GetType().GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); for (int i = 0; i < fields.Length; i++) { FieldInfo fieldInfo = fields[i]; fieldInfo.SetValue(part, fieldInfo.GetValue(this.partRef)); }
Note this same code block is also used for PartModule
as well as Part
, so there's two bits to fix.
History
#1 Updated by TriggerAu almost 8 years ago
- Status changed from New to Needs Clarification
#2 Updated by TriggerAu over 7 years ago
- Status changed from Needs Clarification to Closed
- % Done changed from 0 to 100
Closing this report out for now. If you find it is still occuring in the latest version of KSP please open a new report (and this one can be linked to it.) For best results, the wiki contains really useful info for when creating a report http://bugs.kerbalspaceprogram.com/projects/ksp/wiki.
You can also ask questions about the bug cleanup in the forum here: http://forum.kerbalspaceprogram.com/index.php?/topic/143980-time-to-clean-up-the-bug-tracker/ and tag @TriggerAu to get my attention