[Solved] C# change max hitpoints and cost?

Started by Napoleonite, April 22, 2019, 08:54:33 AM

Previous topic - Next topic

Napoleonite

How do I set the MAX hitpoints of a building through C#? I have a mod-setting (HugsLib) with the max-hp value but I do not know how to assign it because I couldn't find the maxHP property anywhere... Where is it?

Also how would I change the building cost through C#? I know how to access it but not how/where to change it.


    public class Indoor_Turret : Building_TurretGun
    {
        public ThingDef_IndoorTurret Def { get { return this.def as ThingDef_IndoorTurret; } }
     
        public Indoor_Turret()
        {
            //base.MaxHitPoints = ... Damn it is a getter...
            HitPoints = ThingDef_IndoorTurret.MaxHitPoints; // Only sets the current HP, not the max hp...
            Log.Message(HitPoints.ToString());
            // def.costList[0].count = 999; // Does not work. Maybe it needs to be set from the ThingDef_IndoorTurret class somewhere?
        }
    }

LWM

You sure you cannot change this setting thru XML?

Did you read the getter for MaxHitPoints function in Thing.cs?  You can use Harmony to patch that function (get_MaxHitPoints) if you want to change it for an arbitrary building.  (Something like a Prefix of "if (!__instance is Building_I_Care_About) return true;  return ...;")  And of course, you cannot just override it because it's not virtual :p

If you want to set the MaxHitPoints for EVERY instance of a certain type of building, you can change the def: there are ways to find a def, and then you could do something like SetStatBaseValue(StatDefOf.MaxHitPoints,newMaxHP).  I think.

Hope that helps.

--LWM


Napoleonite

QuoteIf you want to set the MaxHitPoints for EVERY instance
Yes this is what I would like (and no not through XML). The code you gave threw really weird errors but I figured out that I had to move the code from the constructor to the SpawnSetup() and then it worked:

        public override void SpawnSetup(Map map, bool respawningAfterLoad)
        {
            base.SpawnSetup(map, respawningAfterLoad);

            if (StatDefOf.MaxHitPoints == null)
            {
                Log.Warning("StatDefOf.MaxHitPoints is NULL.");
                return;
            }

            Def.SetStatBaseValue(StatDefOf.MaxHitPoints, ThingDef_IndoorTurret.MaxHitPoints);
            if (HitPoints > MaxHitPoints) { HitPoints = MaxHitPoints; }
        }

The only downside is that this code is run anytime a new turret is created but okay at least it works and doesn't cause performance issues.

LWM

Quote from: Napoleonite on April 23, 2019, 10:55:04 AM
The only downside is that this code is run anytime a new turret is created but okay at least it works and doesn't cause performance issues.

I mean, MY pawns can't create hundreds and hundreds of turrets every second, I don't know about YOURS  ;D ;D ;D

No, unless it's something happening every tick or every screen update, you're unlikely to hit too many performance issues.  Heh.  CE does some stuff, and the Avoid Friendly Fire mod has caused problems for a few people, but in general, if you aren't being crazy, you're fine.  Which isn't to say you shouldn't pay attention :)  So I'm *very* glad you are thinking about it!

Good job on figuring out what you needed!

--LWM