Creating Your First Mod - Valheim-Modding/Wiki GitHub Wiki
Try modding they said, it will be fun they said! Here you are creating your first mod, welcome to the community! This page will walk you through basic setup, some code, and how to load your mod into Valheim. This tutorial was created and verified for game version 0.219.16.
You will need a C# code editor such as Rider or Visual Studio and the BepInEx Pack for Valheim. Start by installing your editor of choice, and manually downloading this pack.
The author of this article uses Visual Studio, and will explain some steps from that context going forward.
- Create a new C#
Class Library (.NET Framework)
project targeting.NET Framework 4.8
. - Find the folder created and add the unzipped .dll files from the BepInEx pack to a new folder in the solution (name it "Libraries", "libs" or whatever you want).
- In the Solution Explorer
- Right-click References
- Select browse...
- First: Add all the .dll files from the bepinex pack from step 2.
- Second: Find your game installation folder Valheim\valheim_Data\Managed and add assembly_valheim.dll, UnityEngine.dll, and UnityEngine.CoreModule.dll
- Open your auto-generated Class1.cs file and replace the contents with the below
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using System.Reflection;
namespace MyBepInExPlugin
{
[BepInPlugin(pluginGUID, pluginName, pluginVersion)]
public class Main : BaseUnityPlugin
{
const string pluginGUID = "com.example.GUID";
const string pluginName = "MyPlugin";
const string pluginVersion = "1.0.0";
private readonly Harmony HarmonyInstance = new Harmony(pluginGUID);
public static ManualLogSource logger = BepInEx.Logging.Logger.CreateLogSource(pluginName);
public void Awake()
{
Main.logger.LogInfo("Thank you for using my mod!");
Assembly assembly = Assembly.GetExecutingAssembly();
HarmonyInstance.PatchAll(assembly);
}
// More Code Here!
}
}
Now that you have your project set up it is time to actually make the mod! For this example we will give our player infinite stamina. Add this just below the // More Code Here
comment:
[HarmonyPatch(typeof(Player), nameof(Player.UseStamina))]
public static class Patch_Player_UseStamina
{
private static bool Prefix()
{
return false;
}
}
So what does this actually do? This is a special prefix harmony patch that tells the game to NOT run the Player.UseStamina
method. "Prefix" is a keyword that Harmony uses. A Prefix patch will run before the original code and a Postfix patch would run after it. You can learn more about this from the HarmonyX documentation.
Make sure to Save the file!
Caution
Do you see a red squiggly line under HarmonyPatch
?
This is because we added references to both 0Harmony
, and 0Harmony20
.
Tip
If you hover over this red line it will tell you this.
Fix it by...
- Going back to you "Solution Explorer"
- Right-click References
- Select browse...
- Uncheck the box for
0Harmony20
to remove it.
Easy fix. You will experience many such issues while modding, remember not to panic!
You can now build your project and test it in game.
- Find the Build tab and select
Build Solution
.- Depending on if your project is set up for Debug or Release mode you will get your output in different folders.
- By default the editor should be in "Debug" mode so your final .dll file will be under the "bin\Debug" folder.
- My project was named "ClassLibrary1" by default, so the file will be "ClassLibrary1.dll".
- Copy this file (ex: ClassLibrary1.dll) to the "BepInEx\Plugins" folder where you typically place your mods (your game folder or a mod manager folder).
- Time to launch the game!
- You will see the console window pop up and it should list a log line saying your plugin was loaded, and the example info log right after it.
[Info : BepInEx] Loading [MyPlugin 1.0.0]
[Info : MyPlugin] Thank you for using my mod!
- Now you can load a test character and world and RUN FOR MILES AND MILES AROUND!
Check out more guides and information in the wiki, this is a good second page to read when you are ready Best Practices. If you are going to be editing or adding assets to the game (Items, Creatures, etc) you will need to make a game rip to see how the vanilla objects are set up: Unity Project Guide.
More helpful external resources can be found on this page: Wiki Pages & Sites