Skip to content

CbItem Patching

PrimeSonic edited this page Jul 7, 2021 · 11 revisions

Creating Custom Batteries has never been easier

Setting up your project

You will need to add a reference to CustomBatteries.dll.
Once that's done, everything you will need from Custombatteries will be in the CustomBatteries.API namespace.
Just add this line among your using statements.

using CustomBatteries.API;

Intro to the core classes

There are only 2 classes to keep track of: CbBattery and CbPowerCell.
Both of these classes function exactly the same and expose the same members.
Here is a full breakdown of what's in them:

Properties to set Type _ Notes
EnergyCapacity int Required This determines the maximum energy capacity of the item
Value must be greater than zero
ID string Required This is the internal ID for the item and will be used to assign a TechType using SMLHelper
Name string Required This is the name of the item as seen in-game
FlavorText string Required This is the description of the item as seen in-game
CraftingMaterials List<TechType> Optional This is the list of materials required to craft the item
This list can be added to or replaced before patching
An empty list will result in a crafting recipe of 1 Titanium
UnlocksWith TechType Optional Determines if the item is unlocked at the start of the game or if the blueprints only unlock after obtaining/scanning a specific item
By default, items will be unlocked automatically
CustomIcon Atlas.Sprite(SN1)
UnityEngine.Sprite(BZ)
Optional You can provide a custom sprite for your item here
You can use SMLHelper's ImageUtils (SMLHelper.V2.Utility) to convert simple png files to sprites
CBModelData CBModelData
(CustomBatteries.API)
Optional This class contains additional properties for providing custom textures for your item
More on this later
EnhanceGameObject Action<GameObject> Optional This is an optional delegate that you can use to make further alterations to your item as it is being spawned
This method will be executed after SMLHelper's GetGameObject completes
AddToFabricator bool Optional If set to false, then the item will not be automatically added to the Fabricator crafting tree. Defaults to true.

After that, there are two more members of the CbBattery and CbPowerCell classes you should know about:

Member _ Notes
Patch() Method This submits all the data you've set in the properties to be patched
Once this method has been called, no more changes can be made to that item
TechType get-only Property This property gives you access to the TechType assigned to your new item
WARNING: This property can only be accessed after the Patch() method has been called.

Customizing your Textures

To keep things tidy, everything involving custom textures is contained in the CBModelData class.
You will need to use a tool like Perfare's AssetStudio to extract the original texture files so you can create your own custome versions.

The class offers these properties you can set.
All properties are optional. If you don't set one, then the default texture for the chosen model will be used instead.

Property to set Type Notes
CustomTexture Texture2D The main texture
CustomNormalMap Texture2D The normal map
CustomSpecMap Texture2D The specular map
CustomIllumMap Texture2D The illumination map
CustomIllumStrength float Change this value to set the illumination strength
Uses 1.0f by default
UseIonModelsAsBase bool Set this to true if you want to use the ion battery and power cell models for your items
The standard battery and power cell models are used by default

Patching your items

This couldn't be simpler. The basic steps are:

  1. Instantiate a new instance of CbBattery or CbPowerCell
  2. Populate all required properties (and any optional properties)
  3. Invoke the Patch() method on the instance And that's it! You're done and have the TechType value of your new item ready to use elsewhere in your mod.

Patching a simple battery and power cell will look something like this:

namespace CustomBatteries.Packs
{
    using CustomBatteries.API;
    using QModManager.API.ModLoading;

    [QModCore]
    public static class ExampleCbItemMod
    {
        [QModPatch]
        public static void MainPatch()
        {
            // Always start with your battery first
            var myBattery = new CbBattery
            {
                EnergyCapacity = 200,
                ID = "MyBatteryID",
                Name = "My Cool Battery",
                FlavorText = "Hey, I made a battery!",
                UnlocksWith = TechType.Lithium,
                CraftingMaterials =
                {
                    TechType.Lithium, TechType.Lithium,
                    TechType.Titanium, TechType.Copper,
                    TechType.WhiteMushroom
                }
            };

            // Once patched, you can start accessing the TechType for this battery
            myBattery.Patch();

            // Set up your power cell later
            var myPowerCell = new CbPowerCell
            {
                EnergyCapacity = myBattery.EnergyCapacity * 2,
                ID = "MyBatteryID",
                Name = "My Cool Battery",
                FlavorText = "Hey, I made a power cell!",
                UnlocksWith = TechType.Lithium,
                CraftingMaterials =
                {
                    // You can have your power cell require your battery as a crafting material
                    myBattery.TechType, myBattery.TechType,
                    TechType.Silicone
                    // Normally all power cells require two batteries and a Silicone Rubber,
                    // but you are required to do that using this API
                }
            };

            // Once patched, you're done
            myPowerCell.Patch();
        }
    }
}
Clone this wiki locally