Jump to content

How to mod a constant variable?


Recommended Posts

I'm getting acquainted with modding for this game by starting with what I thought would be an easy change, but is proving difficult.  I have read Cairath's guide, I'm familiar with coding (Java, Python), reviewed the Harmony docs, and exported a number of existing mods to learn more, but I still can't find a clear example of how to change a simple constant value.  And my own test continues to crash the game on startup.  Here's what I have right now:

The goal of this test mod is to alter one value of the Decor Material Mod, namely change the +50% to +30%.  This variable is located in Assembly-CSharp.TUNING.BUILDINGS.DECOR_MATERIAL_MOD as follows:


namespace TUNING
{
  public class BUILDINGS
  {
[...]
    public class DECOR_MATERIAL_MOD
    {
      public const float NORMAL = 0.0f;
      public const float HIGH_1 = 0.1f;
      public const float HIGH_2 = 0.2f;
      public const float HIGH_3 = 0.5f;
      public const float HIGH_4 = 1f;
    }
[...]
  }
}

 

So, I have tried a number of approaches to alter this.  I've currently focused on a Postfix, targeted at the variable directly as follows:

namespace BuildingMods
{
    [HarmonyPatch(typeof(TUNING.BUILDINGS.DECOR_MATERIAL_MOD))]

    public static class ChangeDecorMaterialMod
    {
        public static void Postfix(ref float HIGH_3)
        {
            HIGH_3 = 0.3f;
        }
    }
}

I have tried using __result, __instance, ___HIGH_3 in the ref statement, all of which fail just like this one in the same way.  The traceback in the log says:

HarmonyLib.HarmonyException: Patching exception in method null ---> System.ArgumentException: Undefined target method for patch method static System.Void BuildingMods.ChangeDecorMaterialMod::Postfix(System.Single& HIGH_3)

A search for this error doesn't yield any clear answer.  I guess I'm missing something about how to use HarmonyPatch or Postfix.  I've tried setting typeof at a higher level class and referring to the inset class with a string arg, I've tried referring to the variable as a string.  I think I'm missing something fundamental, but I can't find documentation on it.

 

What is it I am doing wrong here?  What is the correct way to change a constant variable?  Thanks in advance.

 

Link to comment
Share on other sites

Well, after discussing this topic on the Discord group, I learned that it is not possible to modify constants.

 

The fundamental something I was missing is that constants are replaced with their actual value when the code is compiled.  This means that all the DLLs already don't reference the original variable anymore, so it wouldn't make a difference even if I could replace it.  So Harmony focuses on modifying methods.

 

In order to "replace" a constant, each use of the constant in a method needs to be found and modified.  When you export to a project, the decompiler tries to find all the original uses of a variable, but doesn't always succeed.  In the case of BUILDINGS.DECOR_MATERIAL_MOD, I couldn't find references to it, and that's probably why.  In the case of some of the constants in the BUILDINGS class (used in each individual building class), it can be an extensive modification (403 references for BUILDINGS.DECOR).  It seems I did not pick an easy place to start in modding. :D

 

For DECOR_MATERIAL_MOD and OVERHEAT_MATERIAL_MOD, It appears that Aki has found them used in LegacyModMain.ConfigElements.  See this code here.

 

Link to comment
Share on other sites

I also wanted to change the constants in tuning ...

 

Change:

public const float MIN_BUILD_TEMPERATURE = 288.15f;

public const float MAX_BUILD_TEMPERATURE = 318.15f;

 

This is more difficult than making your first mod )

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

Please be aware that the content of this thread may be outdated and no longer applicable.

×
  • Create New...