LogoLogo
Back to raftmodding.com
  • Welcome to the RaftModding docs!
  • ⬇️Getting started
    • Installing Raft Mod Loader
      • Troubleshooting
        • An error occured while fetching for updates
          • Disabling IPV6
          • Changing the DNS
        • The menu inside the game doesn't show up
        • The game crashes on startup
        • There are error notifications in-game
      • Linux or Steam Deck installation
        • Using Bottles
        • Using Wine
      • Configuring your antivirus
        • Windows Defender
        • Malwarebytes
        • Bitdefender
        • Avast
        • Norton
        • AVG
    • Installing a mod
      • Mods in multiplayer
    • How to run multiple raft instances
  • ⚙️MODDING TUTORIALS
    • How to create a mod project
      • The modinfo.json file
    • How to create an AssetBundle
    • How to create console commands
    • Harmony basics
    • Getting access to the modding repositories
  • ⚒️Modding API
    • RAPI
    • Mod
  • 📚Modding Examples
    • Accessing the player instance
    • Adding private variables
    • Spawning dropped items
    • Get selected hotbar item
    • Get the current SteamID
    • Get the current username
    • Giving items to a player
    • Modifying private variables
  • 🌏WEBSITE
    • Mod Slugs (Unique Identifier)
  • 🖥️RAFT DEDICATED SERVER
    • Raft Dedicated Server
Powered by GitBook
On this page

Was this helpful?

  1. Modding Examples

Modifying private variables

PreviousGiving items to a playerNextMod Slugs (Unique Identifier)

Last updated 1 year ago

Was this helpful?

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

To modify 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 HarmonyLib namespace by adding using HarmonyLib; at the top of your class.

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

Traverse.Create(ScriptInstance).Field("fieldname").SetValue(newvalue);

// For example to set the value "stats" of the class Network_Player we can do that :
Traverse.Create(RAPI.getLocalPlayer()).Field("stats").SetValue(mynewstats);

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

Traverse.Create(typeof(classname)).Field("fieldname").SetValue(newvalue);

// For example to set the value "allAvailableItems" of the class ItemManager we can do that :
Traverse.Create(typeof(ItemManager)).Field("allAvailableItems").SetValue(mynewitemlist);
📚
here