Skip to content
Denadan edited this page Jun 30, 2018 · 25 revisions

About

CustomComponents - is a library that allows you to create custom ComponentsDef, that can contain any custom data and load them from JSON definitions.

How it works

Loading each type of resource controled by apopriate DataManager.%%DefLoadRequest class. after loading Def file stored in DataManager and can be retrieved by id. its unchanged throughout the life time. So library hooks to

  1. DataManager.%%DefLoadRequest method OnLoadedWithJSON(string json), check if loading json have "CustomType" tag and then load custom component according this tag.
  2. %%Def.ToJSON(), where check if component is cutomized and send customized object to serialization.

What you need to create own definition

Inherite it from apopriate Custom%%Def class. There is implemented 5 types of custom ressources based on default component types.

  • CustomAmmunitionBoxDef : BattleTech.AmmunitionBoxDef
  • CustomJumpJetDef : BattleTech.JumpJetDef
  • CustomHeatSinkDef : BattleTech.HeatSinkDef
  • CustomUpgradeDef : BattleTech.UpgradeDef
  • CustomWeaponDef : BattleTech.WeaponDef

add CustomAttribute with unique name for type add any fields/properties to class

[Custom("Engine")]
public class EngineDef : CustomHeatSinkDef<EngineDef>
{
   public int EngineRating {get;set;}
   public String SomeValue;
}

and now you can take HeatSinkTemplate.json from game, add your parameters to main scope and change standard parameters (at least Description.Id) for your needs

{
"CustomType" : "Engine",
"EngineRating" : 200,
"SomeValue" : "Custom String",
// rest of heatsink deffinition
}

now you can check if component is Engine and cast it to Engine using standart C# operators is and as

if (componentDef is EngineDef)
{
   var engine = componentDef as EngineDef;
   movementSpeed = engine.EngineRating / mechTonnage
}

Generalization

You can create and implement interfaces to create some general component behaviour. For example provided with library IColorComponent interface, that change color of component in component list or mech equipment

    interface IColorComponent
    {
        UIColor Color { get; }
    }

for example to make Engine orange all you need is add to it definition

[Custom("Engine")]
public class EngineDef : CustomHeatSinkDef<MyComponentDef>, IColorComponent
{
   UIColor Color {get;set;}
...
}

and add color to JSon

"Color" : "Orange"

note: UIColor is buildin enum with pretty limited set of colors and most of them are unusable, it has element UIColor.Custom, but i dont found how to define custom colors. also it have Half variants (e.g. UIColor.RedHalf) with semitransparent alpha

Validators

under construction

Included interfaces

ICustomColor

Allow to change color of component

IWeightLimit

Limit usage of component to Mech tonnage and filter it in mechlab like JumpJets

Expand to other resource type

under construction

Clone this wiki locally