Dynamically adding variables/if statement to scripts

Hey everyone:smile:,

I am trying to come up with a system for damageTypes which designers can easily extend.

I have 3 scripts:
DamageTypes.cs - Is currently just an Enum with the available damageTypes.
Projectile.cs - Designers should be able to select via dropdown menu which damageType this projectile has.
Health.cs - Designers should be able to fill in float values for each damageType.

Current workflow to add new DamageType:

DamageTypes.cs
Add Fire to enum

public enum damageTypes {

	flesh,
	armor,
	fire // new option 
	
}

Projectile.cs
Checks what options are in the DamageTypes.cs and shows it as public variable.

public damageTypes damageType;

1027008--38115--$projectilePrefab.png

Health.cs
Add float fireDamage

public float armorDamage;
public float fleshDamage;
public float fireDamage; // public float so designers can edit

Add if check with new damageType and adjust adj value with new fireDamage float

public void adjustHealth(float adj, damageTypes dmgType){
		
		if(dmgType == damageTypes.armor){
			
			adj *= armorDamage;	
		}
		
		else if (dmgType == damageTypes.flesh){

			adj *= fleshDamage;	
		}
		
		else if (dmgType == damageTypes.fire){ // have to add if check for new damageType
			adj *= fireDamage; // adjust adj value with new float fireDamage
		}
	}

So I want the stuff I have to add to the code now to be generated whenever a designer adds a damageType in an editor window or custom inspector.

Picture to clarify stuff:

Any ideas on this would be greatly appreciated.

Well, in theory, you could use basic file IO to open the cs files search for specific text like “//@new damage types”, insert your new line of code, then save it out. I wouldn’t suggest it.

I would suggest making your classes a little more modular.

[System.Serializable]
public class DamageTypeClass
{
    public string damageType;
    public float damageMultiplier;
}

[System.Serializable]
public class ResistDamageClass
{
    public string resistType;
    public float resistMultiplier;
}

//Then over in Health.CS
public class Health
{
    public List<ResistDamageClass> resistances;

    //pass in all of the damageTypes of the Projectile
    public void adjustHealth(List<DamageTypeClass> damageTypes)
    {
        foreach(DamageTypeClass damaetype in damageTypes)
        {
            //TODO:check for resistance, if so reduce damage
            damage *= damaetype.damageMultiplier;
        }
    }
}

public class Projectile
{
    public List<DamageTypeClass> damaeTypes;
}

Designing it this way will prevent you from corrupting your class files.

Thanks man! Really had no idea how to solve this but this certainly helps.

The resistance part:

public void adjustHealth(float adj, List<DamageTypeClass> damageTypes) {

	foreach(DamageTypeClass damagetype in damageTypes){
		
		 foreach(ResistDamageClass resist in resistances){
			
			if(resist.resistType== damagetype.damageType){
				adj *= (damagetype.damageMultiplier / resist.resistMultiplier);
			}
			
			else {
				adj *= damagetype.damageMultiplier;
			}
		}
	}
}

No problem. Glad it helped.