Scripts

Harmony Mods ››
Parent Previous Next

Scripts are C# scripts which adds new classes to the game, and can be referenced from either other scripts or through XML hooks.


This example script adds a new <requirement> for the buffs.xml file. DMT will compile this script into the Mods.dll.

using System;

using System.Xml;

using UnityEngine;


//        <requirement name="RequirementSameFactionSDX, Mods" faction="animalsCows" />


public class RequirementSameFactionSDX : RequirementBase

{

   public string strFaction = "";


   public override bool ParamsValid(MinEventParams _params)

   {

       Faction myFaction = FactionManager.Instance.GetFaction(_params.Self.factionId);

       if (myFaction.Name == strFaction)

           return true;


       return false;

   }


   public override bool ParseXmlAttribute(XmlAttribute _attribute)

   {

       string name = _attribute.Name;

       if (name != null)

       {

           if (name == "faction")

           {

               strFaction = _attribute.Value.ToString();

               return true;

           }

       }

       return base.ParseXmlAttribute(_attribute);

   }

}



While DMT will take care of loading that script into memory, you will need to add an XML XPath file to enable it.


               <requirement name="RequirementSameFactionSDX, Mods" faction="animalsCows" />  <!-- Requirement will only pass if the entity belongs to this faction -->


The name,"RequirementSameFactionSDX, Mods" is the class name along with the Assembly it's compiled into. For nearly all scripts, this assembly will be Mods, which gets created in the Mods.dll file.


There are a few things to note here.


The loading of custom classes is different depending on the context in which you are trying to change. For requirements, the class reference is in the "name" attribute. In others, it may be "action", or "value".


The "name" value may be different depending on what kind of class you are trying to load.  The game will do a GetType() on the name to find the appropriate class name. However, some hooks in vanilla make assumptions about what the class is called.


In this example, our class is called MinEventActionPlayerLevelSDX:

using System.Xml;

using UnityEngine;

public class MinEventActionPlayerLevelSDX : MinEventActionRemoveBuff

{

   

   //  <triggered_effect trigger="onSelfBuffStart" action="PlayerLevelSDX, Mods" target="self"  />

   public override void Execute(MinEventParams _params)

   {

       for (int i = 0; i < this.targets.Count; i++)

       {

           EntityPlayerLocal entity = this.targets[i] as EntityPlayerLocal;

           if (entity != null)

           {

               if (entity.Progression.Level < Progression.MaxLevel)

               {

                   entity.Progression.Level++;

                   GameManager.ShowTooltipWithAlert(entity, string.Format(Localization.Get("ttLevelUp", string.Empty), entity.Progression.Level.ToString(), entity.Progression.SkillPoints), "levelupplayer");

               }

           }

       }

   }

}



In MinEventActionBase, which is the class that loads the MinEventAction calls from the buffs.xml, we look for the GetType() call:


Type type = Type.GetType("MinEventAction" + _element.GetAttribute("action"));



Notice it adds "MinEventAction" to the start of the "action" attribute. We need to include that as part of the class name (MinEventActionPlayerLevelSDX ), but since the game is appending it, we do not add it as a reference to the XML:


 <triggered_effect trigger="onSelfBuffStart" action="PlayerLevelSDX, Mods" target="self"  />



Created with the Personal Edition of HelpNDoc: Easily create PDF Help documents