In the Founders’ Fortune game code, there is a small class cladd Mod. At the time of writing, the complete source code is this:
[System.Serializable]
public abstract class Mod {
// Load is called right after the player indicates he wants to start a game.
public abstract void Load();
// Start is called when the game starts
public abstract void Start();
// Update is called once per frame
public abstract void Update();
}
This is the base class for all of your mods.
You are going to have a Visual Studio project referencing the game’s .dll file so you can make derived class from the Mod parent class. Once you compile your project, you’ll end up with a .dll file for your mod. You will put that in the game mod’s folder and the game will find it. Founders’ Fortune will load your .dll and search for any child classes derived from Mod. All of the found classes are then instantiated once, using the default constructor and the methods are called at the appropriate points in time.
Some additional notes to the order of execution:
When the player clicks on “New Game”, Load() is called before switching to the crew selection screen
When the player clicks on “Load” to load a saved game, Load() is called before switching to the loading screen
Don’t try to access any objects that are only available ingame, from Load(). For example WorldScripts.Instance.weatherManager.ChangeWeather(Weather.Foggy); is going to give you errors when called from the main menu.
Now that you understand the basics, I recommend reading and compiling the example script mod to see how to get your code in the game.
You can either create a new Visual Studio project or simply copy our example project from GitHub
If you make a new project, make sure to select Class Library (.NET Framework) and then the Framework version 4.7.2
Make sure your naming is correct
Your .dll file needs to start with the name of your mod. For example, if your mod is called “Strange Magic Mod”, you can call your dll Strange Magic Mod.dll or Strange Magic Mod_andSomeOtherStuff.dll.
Make sure you change the name of the dll file to begin with the name of your mod.
To do that, click on Properties in the C# project menu:
How to call private methods and access private fields
Sometimes, modders want to go a little deeper and need to access private methods and fields. While usually forbidden in the C# programming languages, you can circumvent it for your mods using reflection.
This is how you call a private method:
// Get a handle for the private method you want to call
// 'ConstructionManager' is the type of the class
// 'UpdateBuildableAtFloorLevel' is the name of the method
MethodInfo updateBuildableMethod = typeof(ConstructionManager).GetMethod("UpdateBuildableAtFloorLevel", BindingFlags.NonPublic | BindingFlags.Instance);
// Call the method
// 'WorldScripts.Instance.constructionManager' is the specific object you want to call this method on
// 'new object[] { 0 }' are the parameter of the method
// in this case, the parameter is just an int with the value 0
updateBuildableMethod.Invoke(WorldScripts.Instance.constructionManager, new object[] { 0 });
You can access and modify private fields with similar code.
Harmony is an amazing modding library giving you even more power about the internals of Founders’ Fortune. With Harmony you can inject your own code into Founders’ Fortune methods, giving you control over almost everything that happens in game.
1. Download Harmony
Get the newest release. Consider donating to @pardeike, the author of the repo.
2. Pick the correct dll
Go in the Harmony Folder → Release → net472 → 0Harmony.dll