Skip to content

Mod APIs

To allow for easy interoperability between mods, OWML provides an API system where mods can provide and consume APIs from eachother easily.

To create an API start by making an interface with all the methods your API will have.

public interface IMyCoolApi {
    public string Echo(string input);
}

Now create a class that implements the API

public class MyCoolApi : IMyCoolApi {
    public string Echo(string input) => input;
}

Finally, override the GetApi method and have it return an instance of your API

public class MyCoolMod : ModBehaviour {
    public override object GetApi() {
        return new MyCoolApi();
    }
}

Your mod now provides the API to consumers!

First, define the interface for the API, usually the mod will have it available somewhere to copy and paste.

public interface IAnotherModApi {
    public string Echo(string input);
}

Now, use ModHelper.Interaction.TryGetModApi to obtain the API for the mod. For example if I wanted to get the API for the mod Bwc9876.MyCoolMod, I would make the following call to TryGetModApi.

public class MyCoolMod : ModBehaviour {
    public void Start() {
        var myApi = ModHelper.Interaction.TryGetModApi<IAnotherModApi>("Bwc9876.MyCoolMod");
        ModHelper.Console.WriteLine(myApi.Echo("Hello, World!"));
    }
}