Script Mods can make powerful modifications to the way the game plays because they can access the data directly.
I recommend the following steps to get started:
- Read the basic intro below and have the Example Mod source code open on the side to compare
- Compile the Example Mod yourself to see how it works using the steps outlined in the ISModKit Readme
-
Decompile InfraSpace to dive into the game code
- Make your own mods and have fun!
Basic Info about Script modding
In the InfraSpace 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 when the mod is loaded just before the level is created
public abstract void Load();
// Start is called when the game starts
public abstract void Start();
// FrameUpdate is called once per frame
public abstract void FrameUpdate();
// SimulationUdpate is called when other simulations are run
public abstract void SimulationUdpate();
}
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. InfraSpace 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 during the loading screen, before the map is generated or other resources are set up
- When the player clicks on “Load” to load a saved game,
Load()
is called during the loading screen, after your save file has been read and loaded
- Don’t try to access any objects that are only available ingame, from
Load()
. For example WorldScripts.Inst.resourceManager.GetResource(Resource.Get("concrete"))
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.
Important notes about making script mods
How to make a new project
- 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 “UpkeepMod”, you can call your dll
UpkeepMod.dll
or
UpkeepMod_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:
and change the dll name here (by default it’s the project name itself):
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
// 'BuildingConstructionManager' is the type of the class
// 'CanBuy' is the name of the method
MethodInfo canBuyMethod = typeof(BuildingConstructionManager).GetMethod("CanBuy", BindingFlags.NonPublic | BindingFlags.Instance);
// Call the method
// 'WorldScripts.Inst.buildingConstructionManager' is the specific object you want to call this method on
// 'new object[] { 1 }' are the parameter of the method
// in this case, the parameter is just an int with the value 1
canBuyMethod.Invoke(WorldScripts.Inst.buildingConstructionManager, new object[] { 1 });
You can access and modify private fields with similar code.
How to use Harmony for advanced mods
Harmony is an amazing modding library giving you even more power about the internals of InfraSpace. With Harmony you can inject your own code into InfraSpace 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
3. Add it as a dependency in Visual Studio
Select “Add Assembly Reference…” and then using the “Browse…” button, select the correct Harmony dll from the file browser.
4. Make your mod
Check our example mod on GitHub for a start.
Check out the official Harmony Documentation for all the juicy details.
5. Distribute your mod
Simply add the Harmony dll to your mod folder:
Be sure not to change its name!
6. Playtest
Check out if it works and let us know about the cool stuff you made!