2. Creating a simple mod - robot9706/FSSE GitHub Wiki
Creating a simple mod
Every assembly (class library) can hold as many mods as you like. Each mod is a class with some additional info, FSLoader will find those classes and load them by hooking FalloutShelter methods to mod methods. Every time the game calls a hooked method instead of the original code the FSLoader code will be executed to run mods, this way game functions can be changed.
Creating an empty mod class
- Create an empty public class and add the ModInfo attribute to it:
...
[ModInfo(...)]
public class ModClassName : Mod
{
...
- Fill the ModInfo with data, this includes a unique ID, the name, author, version and an optinonal description of the mod.
...
[ModInfo("unique_mod_id", "My own mod", "ME", 1, 0)]
public class ModClassName : Mod
{
...
- The next step is to start hooking to different methods, in this example we'll override the "CanDestroyRoom" function so we'll be able to remove any room (no restrictions). The method is located in the "ConstructionMgr" class. Use the "Signatures" tab in the FSModTool to find the signature for that hook, which is "ConstructionMgr::CanDestroyRoom(Room)".
- Create a hook method.
...
[Hook("ConstructionMgr::CanDestroyRoom(Room)")]
public void Hook_CanDestroyRoom(CallContext context, Room room)
{
...
The first parameter of a Hook method is always a "CallContext" which contains the object which has the hooked method, tells if the hook event is handled and the return object of the hooked method.
- Overwrite the game method using the CallContext: Always return "true" so every room could be destroyed.
[Hook("ConstructionMgr::CanDestroyRoom(Room)")]
public void Hook_CanDestroyRoom(CallContext context, Room room)
{
context.IsHandled = true; //No game code will be executed
context.ReturnValue = true; //The method will return "true"
}
The event is handled ("IsHandled = true") so no FalloutShelter code will be executed, if it's "false" after all the hooks are called the hooked method will continue doing it's thing so both mod methods and the original FalloutShelter method is executed. Because the method is handled the hooked method will return the value of "ReturnValue", so the "CanDestroyRoom" method will always return "true".
- Build the project.
The complete example mod:
using FSLoader;
namespace ModPack
{
[ModInfo("unique_mod_id", "My own mod", "ME", 1, 0)]
public class ModDestroyEverything : Mod
{
[Hook("ConstructionMgr::CanDestroyRoom(Room)")]
public void Hook_CanDestroyRoom(CallContext context, Room room)
{
context.IsHandled = true;
context.ReturnValue = true;
}
}
}
Useful links |
---|
FSModTool guide |
FSLoader API |
Mod pack source code |