|
| 1 | +--- |
| 2 | +title: Traits |
| 3 | +description: Documentation on how to add a custom trait using Cult of the Lamb API |
| 4 | +--- |
| 5 | + |
| 6 | +## Creating Traits |
| 7 | + |
| 8 | +To create an trait, you first need to make a class overriding `CustomTrait`. |
| 9 | +Example: |
| 10 | + |
| 11 | +```csharp |
| 12 | +using COTL_API.CustomTraits; |
| 13 | +using UnityEngine; |
| 14 | +using System.IO; |
| 15 | +``` |
| 16 | + |
| 17 | +```csharp |
| 18 | +[HarmonyPatch] |
| 19 | +internal class ExampleTrait : CustomTrait |
| 20 | + { |
| 21 | + public override string InternalName => "ExampleTrait"; |
| 22 | + |
| 23 | + public override bool Positive => true; |
| 24 | + |
| 25 | + // exclusive traits are traits that can't appear along with this trait! |
| 26 | + // if they are also custom defined trait, you only need to exclude it on |
| 27 | + // one of the traits. |
| 28 | + public override List<FollowerTrait.TraitType> ExclusiveTraits => |
| 29 | + [ |
| 30 | + FollowerTrait.TraitType.RoyalPooper |
| 31 | + ]; |
| 32 | + |
| 33 | + public override TraitFlags TraitFlags => TraitFlags.RareStartingTrait; |
| 34 | + |
| 35 | + public override string LocalizedTitle() => "Example Trait"; |
| 36 | + |
| 37 | + public override string LocalizedDescription() => "this trait is just an example :)."; |
| 38 | + |
| 39 | + public override Sprite Icon => TextureHelper.CreateSpriteFromPath(Path.Combine(Plugin.PluginPath, "Assets", "ExampleTrait.png")); |
| 40 | + |
| 41 | + // by default, no behaviour for custom traits is added. use patches check in |
| 42 | + // your own code for the presence of the trait, and change the game's |
| 43 | + // behvaiour accordingly. |
| 44 | + [HarmonyPatch(typeof(FollowerBrain), nameof(FollowerBrain.GetPoopType))] |
| 45 | + [HarmonyPrefix] |
| 46 | + private static bool FollowerBrain_GetPoopType(ref FollowerBrain __instance, ref StructureBrain.TYPES __result) |
| 47 | + { |
| 48 | + if (!__instance.Info.Traits.Contains(Plugin.ExampleTrait)) return true; |
| 49 | + |
| 50 | + __result = StructureBrain.TYPES.POOP_RAINBOW; |
| 51 | + DataManager.Instance.DaySinceLastSpecialPoop = TimeManager.CurrentDay; |
| 52 | + return false; |
| 53 | + |
| 54 | + } |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +There is no diffrence between cult traits and regular traits. Cult traits are added by `FollowerTrait.AddCultTrait(FollowerTrait.TraitType);` and regular ones by `followerBrain.AddTrait(FollowerTrait.TraitType);` on a follower's brain. |
| 59 | + |
| 60 | +`CustomTrait` supports the following overrides: |
| 61 | +| Type | Name | Default | |
| 62 | +|-|-|-| |
| 63 | +| string | InternalName | \[REQUIRED\] | |
| 64 | +|bool|Positive| true| |
| 65 | +|bool|IsTraitUnavailable()|false| |
| 66 | +|Sprite|Icon|TextureHelper.CreateSpriteFromPath(PluginPaths.ResolveAssetPath("placeholder.png"));| |
| 67 | +|List<FollowerTrait.TraitType>| ExclusiveTraits|[]| |
| 68 | +TraitFlags|TraitFlags|TraitFlags.None| |
| 69 | +|string|LocalizedTitle()|LocalizationManager.GetTranslation($"Traits/{ModPrefix}.{InternalName}")| |
| 70 | +|string|LocalizedDescription()|LocalizationManager.GetTranslation($"Traits/{InternalName}.description")| |
| 71 | + |
| 72 | +## Adding Traits |
| 73 | + |
| 74 | +To add a trait to the game, simply use `CustomTraitManager.Add()`. |
| 75 | +Example: |
| 76 | + |
| 77 | +```csharp |
| 78 | +using COTL_API.CustomTraits; |
| 79 | +public static FollowerTrait.TraitType ExampleTrait { get; private set; } |
| 80 | +``` |
| 81 | + |
| 82 | +```csharp |
| 83 | +private void Awake() |
| 84 | +{ |
| 85 | + ExampleTrait = CustomTraitManager.Add(new ExampleTrait()); |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +Assigning the result of `CustomTraitManager.Add()` allows you to reference that trait elsewhere in your code using `Plugin.ExampleTrait`. |
| 90 | + |
| 91 | +## Final Steps |
| 92 | + |
| 93 | +For the icon to load, you need to put it in the appropriate location. For the example, this would be `/Assets/ExampleTrait.png` relative to the root folder containing the .dll |
| 94 | +Directory structure: |
| 95 | + |
| 96 | +``` |
| 97 | +📂plugins |
| 98 | + ┣📂Assets |
| 99 | + ┃ ┗🖼️ExampleTrait.png |
| 100 | + ┗📜mod_name.dll |
| 101 | +``` |
0 commit comments