GreenHell Modding
  • Welcome to the GreenHell Modding docs!
  • TUTORIALS
    • How to install GreenHellModLoader
      • Troubleshooting
    • How to install a mod
    • Mods in multiplayer
  • MODDING TUTORIALS
    • How to create a mod project
      • The modinfo.json file
    • How to create an AssetBundle
  • CLIENT - CODE EXAMPLES
    • Reading private variables
    • Modifying private variables
    • Accessing the player instance
    • Giving items to a player
    • Modifying the inventory max weight
    • Unlocking everything in the Notepad
    • Disabling player movement
    • Toggling the mouse/cursor
    • Printing to the console
  • WEBSITE
    • Slugs
Powered by GitBook
On this page

Was this helpful?

  1. CLIENT - CODE EXAMPLES

Reading private variables

PreviousHow to create an AssetBundleNextModifying private variables

Last updated 5 years ago

Was this helpful?

Reading private variables is clearly necessary when modding raft. Let's see how easy it is!

To access private variables we need to use Harmony. You can visit the Harmony wiki by clicking . Harmony is a library for patching, replacing and decorating .NET and Mono methods during runtime. To use harmony you simply need to add the Harmony namespace by adding using Harmony; at the top of your class.

To access a non-static private variable you use Traverse.Create() with the object instance and .Field() with the field name.

string myvalue = Traverse.Create(ScriptInstance).Field("fieldname").GetValue() as string;

// For example to get the value "m_MinFallingHeight" of the class Player we can do that :
float val = Traverse.Create(Player.Get()).Field("m_MinFallingHeight").GetValue() as float;

To access a static private variable you also use Traverse.Create() but with the class type and .Field() with the field name.

string myvalue = Traverse.Create(typeof(classname)).Field("fieldname").GetValue() as string;

// For example to get the value "s_Initialized" of the class ItemIDHelpers we can do that :
bool myvalue = Traverse.Create(typeof(ItemIDHelpers)).Field("s_Initialized").GetValue() as bool;

here