Project

General

Profile

Bug #9865

Having a const field on a PartModule on the root part causes physics to break

Added by blowfish almost 8 years ago. Updated over 7 years ago.

Status:
Closed
Severity:
Low
Assignee:
-
Category:
Plugins/Add-Ons
Target version:
-
Start date:
06/07/2016
% Done:

100%

Version:
Platform:
Windows
Expansion:
Language:
English (US)
Mod Related:
No
Votes:
Arrow u r green
Arrow d r red

Description

Steps to Reproduce

  • Create a PartModule with a const field and add it to a part
  • Make a vessel with that part as the root part
  • Launch the vessel
  • Exit to the space center
  • Return to the vessel

Observed Error

An exception is thrown when initializing the vessel, which causes physics to break

[EXC 19:07:09.972] FieldAccessException: Cannot set a constant field
    System.Reflection.MonoField.SetValue (System.Object obj, System.Object val, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Globalization.CultureInfo culture)
    System.Reflection.FieldInfo.SetValue (System.Object obj, System.Object value)
    ProtoPartSnapshot.Load (.Vessel vesselRef, Boolean loadAsRootPart)
    ProtoVessel.LoadObjects ()
    Vessel.Load ()
    Vessel.MakeActive ()
    FlightGlobals.setActiveVessel (.Vessel v, Boolean force)
    FlightGlobals.SetActiveVessel (.Vessel v)
    FlightDriver.Start ()

This is caused by copying all the fields from one PartModule to another without first checking that the fields are instance using type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) should fix the problem

History

#1 Updated by blowfish almost 8 years ago

I guess it's not strictly necessary to use BindingFlags.NonPublic, since private and protected fields won't be copied as it is, but it would probably save a bit of headache in some cases, since you are really trying to duplicate the object.

#2 Updated by TriggerAu over 7 years ago

  • Status changed from New to Needs Clarification

#3 Updated by blowfish over 7 years ago

@TriggerAU

Is there any thing specific that needs clarifying? The issue is that having a constant defined on a PartModule, then using that module, causes a physics-breaking exception.

public class MyModule : PartModule
{
    public const int ConstInt = 5;

    // ...
}

Using this PartModule would cause the exception.

#5 Updated by blowfish over 7 years ago

  • Status changed from Needs Clarification to Closed
  • % Done changed from 0 to 100

Moving to Modders' Corner tracker and closing

Also available in: Atom PDF