Create-A-Crate: Soft Dependencies
Updated a week agoUsage
Soft Dependencies
In case you want to create a mod that optionally adds your crates, for example if the mod mainly does something else but has special crates as hidden secrets, I recommend implementing a soft dependency. This will allow people to play your mod even if they don't have LCCrashBandicootCratesMod installed, so people can have some more creative freedom.
To do this:
- Make sure to still reference the assembly, as told under step 2 in Basic Pipeline
- But, in your main Plugin Class, tell BepInEx to keep working even if it can't find LCCrashBandicootCratesMod, by adding a DependencyFlag
[BepInPlugin(PluginInfo.PLUGIN_GUID, PluginInfo.PLUGIN_NAME, PluginInfo.PLUGIN_VERSION)][BepInDependency("local.SimonTendo.LCCrashBandicootCratesMod", BepInDependency.DependencyFlags.SoftDependency)]
- Then, on Awake(), set a public boolean to check if you have the game running with LCCrashBandicootCratesMod installed, with this block of code:
public static bool CrashBandicootCratesCompatible = false;
void Awake()
{
CrashBandicootCratesCompatible = CheckForPlugin("LCCrashBandicootCratesMod");
}
private static bool CheckForPlugin(string pluginName, bool printDebug = true)
{
foreach (var plugin in Chainloader.PluginInfos.Values)
{
if (plugin.Metadata.Name == pluginName)
{
if (printDebug) Logger.LogInfo($"Successfully found {pluginName}");
return true;
}
}
if (printDebug) Logger.LogWarning($"Failed to find {pluginName}");
return false;
}
- This will go through all mods that BepInEx has already loaded, and by having a SoftDependency on LCCrashBandicootCratesMod, we ensure that 1) LCCrashBandicootCratesMod needs to load first so it shows up as "already loaded", and 2) your own mod will still work, even if LCCrashBandicootCratesMod is not currently installed.
- Then, on your Plugin's Awake(), check whether bool CrashBandicootCratesCompatible is true, and fire CreateACrate as a separate method
void Awake()
{
if (CrashBandicootCratesCompatible)
{
CreateYourOwnCrate();
}
}
IMPORTANT!! Every line of code that uses LCCrashBandicootCratesMod methods as a soft dependency MUST be in a DIFFERENT method than for example your patches, since methods and variables seem to get pre-loaded once the method holding them starts. And if they aren't present because you don't have the mod installed, your game will most likely stop working!
- Having it be in a different method that only gets called if CrashBandicootCratesCompatible is true, ensures that the LCCrashBandicootCratesMod data only gets loaded if the mod's code is actually available to load.
- And then, in this separate method, execute whatever you want to do using the above Provided Methods, and you'll have the same capabilities as with a normal dependency.
- Additionally, you can remove the dependency on LCCrashBandicootCratesMod from your own mod's manifest, listed at the end of Basic Pipeline's step 3.