2. Making your mod - LionShield/Kingdoms-and-Castles-Toolkit GitHub Wiki
To get started you should read the official guide here.
Below are some notes, common pitfalls and suggestions for each section.
Quick reference
Another good C# editor is JetBrains Rider. It doesn't really matter what you use to code in but a good editor such as Visual Studio will make it easier to find errors!
When adding references you can just add (Ctrl+a in the file picker) everything in the \Managed
folder if you want. It doesn't make a difference and may save you going back later and adding more references you need.
Making mods for Kingdoms and Castles
If you open all of the logs inside Notepad++ they will update automatically (Just click on a different log and then back to the log you want to update). It will save you from having to close and open log files all of the time! Especially when the game is running.
Another way to see logs in-game is the mod DevMode by ArchieV1 which adds an in-game log. (NOTE: It only shows logs from after it was loaded so if it was loaded late it may miss the first few logs from other mods)
In the example code it shows var harmony = HarmonyInstance.Create("harmony");
though this is bad practice. You should instead do var harmony = HarmonyInstance.Create("myModName");
so that you can see which mod has caused issues easily.
Making a mod with AssetBundles
Having a basic understanding of what an AssetBundle is will be useful. See the unity wiki for some basics.
The name you see in the bottom right of this image is the name of the AssetBundle. You need to write this down! Capitals DO matter!
The left side (Assets>Workspace
) is the location of the prefab. This is important! Capitals do NOT matter. It will be converted to lower case (Assets/Workspace
> assets/workspace
).
Assets/Workspace is just an example. Yours can be anywhere and may be different. This is not related to your actual filesystem. It is just an internal file structure inside of the AssetBundle you create.
Both of the following will return null
if they fail to find the AssetBundle or Asset respectively. This can be used to check that they have worked correctly
var assetBundle = helper.LoadAssetBundle();
var myPrefab = assetBundle.LoadAsset();
If you are struggling to find the prefabs/assets inside of your asset bundle you can do Helper.Log(string.Join("\n", assetBundle.GetAllAssetNames()));
to list all of the assets inside of your asset bundle in your mod's log.
The following code is a very good idea. Checking that the assets have been loaded correctly before creating the harmony patch may stop the game from crashing.
if(leftSneakerPrefab != null && rightSneakerPrefab != null)
{
var harmony = HarmonyInstance.Create("myModName");
harmony.PatchAll(Assembly.GetExecutingAssembly());
}